Guest User

Untitled

a guest
Dec 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.21 KB | None | 0 0
  1. procedure       _Pos{ substr : ShortString; s : ShortString ) : Integer};
  2. asm
  3. {     ->EAX     Pointer to substr               }
  4. {       EDX     Pointer to string               }
  5. {     <-EAX     Position of substr in s or 0    }
  6.  
  7.         PUSH    EBX
  8.         PUSH    ESI
  9.         PUSH    EDI
  10.  
  11.         MOV     ESI,EAX { Point ESI to substr           }
  12.         MOV     EDI,EDX { Point EDI to s                }
  13.  
  14.         XOR     ECX,ECX { ECX = Length(s)               }
  15.         MOV     CL,[EDI]
  16.         INC     EDI             { Point EDI to first char of s  }
  17.  
  18.         PUSH    EDI             { remember s position to calculate index        }
  19.  
  20.         XOR     EDX,EDX { EDX = Length(substr)          }
  21.         MOV     DL,[ESI]
  22.         INC     ESI             { Point ESI to first char of substr     }
  23.  
  24.         DEC     EDX             { EDX = Length(substr) - 1              }
  25.         JS      @@fail  { < 0 ? return 0                        }
  26.         MOV     AL,[ESI]        { AL = first char of substr             }
  27.         INC     ESI             { Point ESI to 2'nd char of substr      }
  28.  
  29.         SUB     ECX,EDX { #positions in s to look at    }
  30.                         { = Length(s) - Length(substr) + 1      }
  31.         JLE     @@fail
  32. @@loop:
  33.         REPNE   SCASB
  34.         JNE     @@fail
  35.         MOV     EBX,ECX { save outer loop counter               }
  36.         PUSH    ESI             { save outer loop substr pointer        }
  37.         PUSH    EDI             { save outer loop s pointer             }
  38.  
  39.         MOV     ECX,EDX
  40.         REPE    CMPSB
  41.         POP     EDI             { restore outer loop s pointer  }
  42.         POP     ESI             { restore outer loop substr pointer     }
  43.         JE      @@found
  44.         MOV     ECX,EBX { restore outer loop counter    }
  45.         JMP     @@loop
  46.  
  47. @@fail:
  48.         POP     EDX             { get rid of saved s pointer    }
  49.         XOR     EAX,EAX
  50.         JMP     @@exit
  51.  
  52. @@found:
  53.         POP     EDX             { restore pointer to first char of s    }
  54.         MOV     EAX,EDI { EDI points of char after match        }
  55.         SUB     EAX,EDX { the difference is the correct index   }
  56. @@exit:
  57.         POP     EDI
  58.         POP     ESI
  59.         POP     EBX
  60. end;
Add Comment
Please, Sign In to add comment