1.17. MORE ABOUT STRINGS
TheSUBinstruction just got executed:
Figure 1.61:OllyDbg:EAXto be decremented now
The difference of pointers is in theEAXregister now—7. Indeed, the length of the “hello!” string is 6, but
with the zero byte included—7. Butstrlen()must return the number of non-zero characters in the string.
So the decrement executes and then the function returns.
Optimizing GCC
Let’s check GCC 4.4.1 with optimizations turned on (-O3key):
public strlen
strlen proc near
arg_0 = dword ptr 8
push ebp
mov ebp, esp
mov ecx, [ebp+arg_0]
mov eax, ecx
loc_8048418:
movzx edx, byte ptr [eax]
add eax, 1
test dl, dl
jnz short loc_8048418
not ecx
add eax, ecx
pop ebp
retn
strlen endp
Here GCC is almost the same as MSVC, except for the presence ofMOVZX. However, hereMOVZXcould be
replaced with
mov dl, byte ptr [eax].