Continue mixing time ...... Continue with boring code ...... Waiting for work ......
Strstr is a frequently used function. Many people initially think that this function is not provided in windows, so they write it by themselves. I am also one of them ...... (The lstrstr function is actually available in advapi32.dll ).
Now I want to write a simple strstr with no algorithm ...... It's easy to say that you can directly copy ......
The first is the general idea written in C language:
01 # 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}
37
38
That's it ......
Okay. Now I want to write it in the sink:
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, ""
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, ""
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
Where did you copy it ...... Because it is case sensitive, so ...... I threw my own section ......
It will take a long time to compile the KMP algorithm ......