头文件<string.h>
LPTSTR StrStr(
LPCTSTR lpFirst,
LPCTSTR lpSrch
);
返回第一个匹配的地址,否则返回NULL
lpFirst 被寻找的字符串
lpSrch 需要匹配的字符串
#include <stdio.h>
02 #include <windows.h>
03
04 int mystrstr(char *addr1, char *addr2)
05 {
06 int m,n;
07 int x;
08 x=strlen(addr1);
09 for (n=0; n<=strlen(addr1); n++)
10 {
11 if (*addr1+n == *addr2)
12 {
13 for (m=0; m<=strlen(addr2); m++)
14 {
15 if (*addr1+n+m != *addr2+m)
16 {
17 break;
18 }
19 if (m == strlen(addr2))
20 {
21 return n;
22 }
23 }
24 }
25
26 }
27
28 return 0;
29 }
30 int main()
31 {
32 int num;
33 num = mystrstr("12345678","78");
34 printf("%d",num);
35 return 0;
36 }
现在用汇编写一下:
01 strstr proc pszHaystack:PTR BYTE, pszNeedle:PTR BYTE, dwCaseSensitive:DWORD
02 push esi
03 push edi
04 xor eax, eax
05 xor ecx, ecx
06 xor edx, edx
07 mov esi, [pszHaystack]
08 mov edi, [pszNeedle]
09 @@char: mov al, byte ptr [esi]
10 mov ah, byte ptr [edi]
11 cmp [dwCaseSensitive], 0
12 jne @@cmp1
13 cmp al, "Z"
14 ja @@cmp0
15 cmp al, "A"
16 jb @@cmp0
17 add al, 32
18 @@cmp0: cmp [dwCaseSensitive], 0
19 jne @@cmp1
20 cmp ah, "Z"
21 ja @@cmp1
22 cmp ah, "A"
23 jb @@cmp1
24 add ah, 32
25 @@cmp1: cmp al, ah
26 jne @@next
27 inc ecx
28 mov eax, esi
29 inc edi
30 inc esi
31 inc eax
32 cmp byte ptr [esi], 0
33 je @@zero
34 cmp byte ptr [edi], 0
35 je @@quit
36 jmp @@char
37 @@zero: mov dl, byte ptr [edi]
38 cmp dl, byte ptr [esi]
39 je @@quit
40 xor eax, eax
41 xor ecx, ecx
42 jmp @@quit
43 @@next: mov edi, [pszNeedle]
44 test ecx, ecx
45 setz dl
46 add esi, edx
47 xor ecx, ecx
48 xor eax, eax
49 cmp byte ptr [esi], 0
50 jne @@char
51 @@quit: sub eax, ecx
52 pop edi
53 pop esi
54 ret
55 strstr endp