Assembly code snippets
Details
Title | InString |
---|---|
Author | eko |
Submitted by: | eko |
Date added: | 2002-07-06 18:39:09 |
Date modified: | 2002-07-06 18:39:09 |
Comments
orignal version had been written by hutch ( can be found in m32lib)
this is an optimized version of Instring by eko
Snippet
InString proc uses ebx esi edi startpos:DWORD,lpSource:DWORD,lpPattern:DWORD
invoke StrLen,lpPattern ; pattern length
push eax ;because of strlen
invoke StrLen,lpSource ; mov sLen, eax ; source length
mov ebx,startpos
pop edx
cmp ebx,1
jb @ER
cmp eax,ebx
jg @F
@ER:
mov eax, -2
jmp isOut ; exit if startpos is past end OR exit if startpos not 1 or greater
@@:
dec ebx ; ; correct from 1 to 0 based index
sub eax,edx
jg @F
mov eax, -1
jmp isOut ; exit if pattern longer than source
@@:
inc eax
mov esi, lpSource
mov sLen, eax
; ----------------
; setup loop code
; ----------------
add esi,eax
neg eax ; invert sign
mov edi, lpPattern
dec edx ;to save dec later
mov cl,[edi] ; get 1st char in pattern
push edx
add eax,ebx ;add startpos
;*****************************************************
;the loop
;*****************************************************
Scan_Loop:
cmp cl, [esi+eax]
je Pre_Match
@set_counter:
inc eax
jnz Scan_Loop ; the loop set eax to set if not found
jmp @loopout
Pre_Match:
mov edx,[esp] ; we have done dec edx before the push
lea ebx,[esi+eax] ;
mov ch, [edi+edx] ; get the last char of the pattern in here .
Test_Match:
cmp ch, [ebx+edx] ; stall . but only one time per prematch..
jne @set_counter ; jump back on mismatc;
dec edx
mov ch, [edi+edx]
jnz Test_Match
add eax,sLen
inc eax
@loopout:
add esp,4 ; instead of this . make local var and save edx there
isOut:
ret
InString endp
invoke StrLen,lpPattern ; pattern length
push eax ;because of strlen
invoke StrLen,lpSource ; mov sLen, eax ; source length
mov ebx,startpos
pop edx
cmp ebx,1
jb @ER
cmp eax,ebx
jg @F
@ER:
mov eax, -2
jmp isOut ; exit if startpos is past end OR exit if startpos not 1 or greater
@@:
dec ebx ; ; correct from 1 to 0 based index
sub eax,edx
jg @F
mov eax, -1
jmp isOut ; exit if pattern longer than source
@@:
inc eax
mov esi, lpSource
mov sLen, eax
; ----------------
; setup loop code
; ----------------
add esi,eax
neg eax ; invert sign
mov edi, lpPattern
dec edx ;to save dec later
mov cl,[edi] ; get 1st char in pattern
push edx
add eax,ebx ;add startpos
;*****************************************************
;the loop
;*****************************************************
Scan_Loop:
cmp cl, [esi+eax]
je Pre_Match
@set_counter:
inc eax
jnz Scan_Loop ; the loop set eax to set if not found
jmp @loopout
Pre_Match:
mov edx,[esp] ; we have done dec edx before the push
lea ebx,[esi+eax] ;
mov ch, [edi+edx] ; get the last char of the pattern in here .
Test_Match:
cmp ch, [ebx+edx] ; stall . but only one time per prematch..
jne @set_counter ; jump back on mismatc;
dec edx
mov ch, [edi+edx]
jnz Test_Match
add eax,sLen
inc eax
@loopout:
add esp,4 ; instead of this . make local var and save edx there
isOut:
ret
InString endp