GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


strcmp in C w/disassembly by DELTRON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
 
1:
2:    using namespace std;
3:
4:    bool cmpstringz0rs(char * s1, char * s2, int lens1, int lens2)
5:    {
00401150 55                   push        ebp
00401151 8B EC                mov         ebp,esp
00401153 83 EC 44             sub         esp,44h
00401156 53                   push        ebx
00401157 56                   push        esi
00401158 57                   push        edi
00401159 8D 7D BC             lea         edi,[ebp-44h]
0040115C B9 11 00 00 00       mov         ecx,11h
00401161 B8 CC CC CC CC       mov         eax,0CCCCCCCCh
00401166 F3 AB                rep stos    dword ptr [edi]
6:        if (lens1 != lens2) return false;
00401168 8B 45 10             mov         eax,dword ptr [ebp+10h]
0040116B 3B 45 14             cmp         eax,dword ptr [ebp+14h]
0040116E 74 04                je          cmpstringz0rs+24h (00401174)
00401170 32 C0                xor         al,al
00401172 EB 44                jmp         cmpstringz0rs+68h (004011b8)
7:        for (int i=0; i<lens1; i++)
00401174 C7 45 FC 00 00 00 00 mov         dword ptr [ebp-4],0
0040117B EB 09                jmp         cmpstringz0rs+36h (00401186)
0040117D 8B 4D FC             mov         ecx,dword ptr [ebp-4]
00401180 83 C1 01             add         ecx,1
00401183 89 4D FC             mov         dword ptr [ebp-4],ecx
00401186 8B 55 FC             mov         edx,dword ptr [ebp-4]
00401189 3B 55 10             cmp         edx,dword ptr [ebp+10h]
0040118C 7D 28                jge         cmpstringz0rs+66h (004011b6)
8:        {
9:            if ((*s1++)!=(*s2++)) return false;
0040118E 8B 45 08             mov         eax,dword ptr [ebp+8]
00401191 0F BE 08             movsx       ecx,byte ptr [eax]
00401194 8B 55 0C             mov         edx,dword ptr [ebp+0Ch]
00401197 0F BE 02             movsx       eax,byte ptr [edx]
0040119A 8B 55 0C             mov         edx,dword ptr [ebp+0Ch]
0040119D 83 C2 01             add         edx,1
004011A0 89 55 0C             mov         dword ptr [ebp+0Ch],edx
004011A3 8B 55 08             mov         edx,dword ptr [ebp+8]
004011A6 83 C2 01             add         edx,1
004011A9 89 55 08             mov         dword ptr [ebp+8],edx
004011AC 3B C8                cmp         ecx,eax
004011AE 74 04                je          cmpstringz0rs+64h (004011b4)
004011B0 32 C0                xor         al,al
004011B2 EB 04                jmp         cmpstringz0rs+68h (004011b8)
10:       }
004011B4 EB C7                jmp         cmpstringz0rs+2Dh (0040117d)
11:       return true;
004011B6 B0 01                mov         al,1
12:   }
004011B8 5F                   pop         edi
004011B9 5E                   pop         esi
004011BA 5B                   pop         ebx
004011BB 8B E5                mov         esp,ebp
004011BD 5D                   pop         ebp
004011BE C3                   ret

14:   void main (void)
15:   {
004011E0 55                   push        ebp
004011E1 8B EC                mov         ebp,esp
004011E3 83 EC 5C             sub         esp,5Ch
004011E6 53                   push        ebx
004011E7 56                   push        esi
004011E8 57                   push        edi
004011E9 8D 7D A4             lea         edi,[ebp-5Ch]
004011EC B9 17 00 00 00       mov         ecx,17h
004011F1 B8 CC CC CC CC       mov         eax,0CCCCCCCCh
004011F6 F3 AB                rep stos    dword ptr [edi]
16:       char string1[]= {"Hello there"};
004011F8 A1 3C F0 42 00       mov         eax,[string "Hello there" (0042f03c)]
004011FD 89 45 F4             mov         dword ptr [ebp-0Ch],eax
00401200 8B 0D 40 F0 42 00    mov         ecx,dword ptr [string "Hello there"+4 (0042f040)]
00401206 89 4D F8             mov         dword ptr [ebp-8],ecx
00401209 8B 15 44 F0 42 00    mov         edx,dword ptr [string "Hello there"+8 (0042f044)]
0040120F 89 55 FC             mov         dword ptr [ebp-4],edx
17:       char string2[]= {"Hi there"};
00401212 A1 30 F0 42 00       mov         eax,[string "Hi there" (0042f030)]
00401217 89 45 E8             mov         dword ptr [ebp-18h],eax
0040121A 8B 0D 34 F0 42 00    mov         ecx,dword ptr [string "Hi there"+4 (0042f034)]
00401220 89 4D EC             mov         dword ptr [ebp-14h],ecx
00401223 8A 15 38 F0 42 00    mov         dl,byte ptr [string "Hi there"+8 (0042f038)]
00401229 88 55 F0             mov         byte ptr [ebp-10h],dl
18:
19:       bool result = cmpstringz0rs( string1, string2, sizeof(string1), sizeof(string2) );
0040122C 6A 09                push        9
0040122E 6A 0C                push        0Ch
00401230 8D 45 E8             lea         eax,[ebp-18h]
00401233 50                   push        eax
00401234 8D 4D F4             lea         ecx,[ebp-0Ch]
00401237 51                   push        ecx
00401238 E8 18 FE FF FF       call        @ILT+80(cmpstringz0rs) (00401055)
0040123D 83 C4 10             add         esp,10h
00401240 88 45 E4             mov         byte ptr [ebp-1Ch],al
20:
21:       if (result) cout << "equal";
00401243 8B 55 E4             mov         edx,dword ptr [ebp-1Ch]
00401246 81 E2 FF 00 00 00    and         edx,0FFh
0040124C 85 D2                test        edx,edx
0040124E 74 14                je          main+84h (00401264)
00401250 68 28 F0 42 00       push        offset string "equal" (0042f028)
00401255 68 A0 3E 43 00       push        offset std::cout (00433ea0)
0040125A E8 37 FE FF FF       call        @ILT+145(std::operator<<) (00401096)
0040125F 83 C4 08             add         esp,8
22:       else cout << "not equal";
00401262 EB 12                jmp         main+96h (00401276)
00401264 68 1C F0 42 00       push        offset string "not equal" (0042f01c)
00401269 68 A0 3E 43 00       push        offset std::cout (00433ea0)
0040126E E8 23 FE FF FF       call        @ILT+145(std::operator<<) (00401096)
00401273 83 C4 08             add         esp,8
23:       return;
24:   }
00401276 5F                   pop         edi
00401277 5E                   pop         esi
00401278 5B                   pop         ebx
00401279 83 C4 5C             add         esp,5Ch
0040127C 3B EC                cmp         ebp,esp
0040127E E8 1D 7C 00 00       call        __chkesp (00408ea0)
00401283 8B E5                mov         esp,ebp
00401285 5D                   pop         ebp
00401286 C3                   ret