Title: Instring + InstringReverse
Question: Find string inside string
Answer:
function InString (StartPosition:cardinal;const Source,Pattern:string):cardinal; stdcall;
asm
push esi
push edi
push ebx
test StartPosition,-1
jne @fw
inc StartPosition
@fw:
mov edi,dword ptr [Source]
mov esi,dword ptr [Pattern]
mov edx,dword ptr [edi-4]
mov ebx,dword ptr [esi-4]
lea edx, [edx+edi]
lea ebx, [ebx+esi]
dec edx
dec ebx
add edi,StartPosition
dec edi
mov eax,edi
add eax,dword ptr [esi-4]
dec eax
cmp eax,edx
ja @Er
@1:
mov esi,dword ptr [Pattern]
@2:
movzx ecx,byte ptr [edi]
movzx eax,byte ptr [esi]
or ecx,32
or eax,32
sub ecx,eax
jz @Prov
@ProvKraj:
inc edi
cmp edi,edx
ja @Er
jmp @1
@Prov:
cmp esi,ebx
jne @Nije
//String odgovara!
sub ebx,dword ptr [Pattern]
sub edi,ebx
sub edi,dword ptr [Source]
mov eax,edi
inc eax //vrati poziciju!
pop ebx
pop edi
pop esi
pop ebp
ret 12
@Nije:
inc edi
inc esi
cmp edi,edx
jbe @2
@Er:
xor eax,eax
pop ebx
pop edi
pop esi
pop ebp
ret 12
end;
function InStringReverse (StartPosition:cardinal;const Source,Pattern:string):cardinal; stdcall;
asm
push esi
push edi
push ebx
mov edx,dword ptr [Source]
mov ebx,dword ptr [Pattern]
mov edi,dword ptr [edx-4]
mov esi,dword ptr [ebx-4]
test StartPosition,-1
jne @fw
mov eax,edi
sub eax,esi
inc eax
mov StartPosition,eax
@fw:
lea esi, [ebx+esi]
dec esi
mov edi,edx
add edi,StartPosition
add edi,dword ptr [ebx-4]
lea edi, [edi-2]
mov ecx,edx
add ecx,dword ptr [edx-4]
dec ecx
cmp edi,ecx
ja @Er
@1:
mov esi,dword ptr [Pattern]
add esi,dword ptr [esi-4]
dec esi
@2:
movzx ecx,byte ptr [edi]
movzx eax,byte ptr [esi]
or ecx,32
or eax,32
sub ecx,eax
jz @Prov
@ProvKraj:
dec edi
cmp edi,edx
jb @Er
jmp @1
@Prov:
cmp esi,ebx
jne @Nije
sub edi,dword ptr [Source]
mov eax,edi
inc eax
pop ebx
pop edi
pop esi
pop ebp
ret 12
@Nije:
dec edi
dec esi
cmp edi,edx
jae @2
@Er:
xor eax,eax
pop ebx
pop edi
pop esi
pop ebp
ret 12
end;