I want to code a strcmp
in nasm x86. The main problem is when it has to return a negative value. I've tried the following, but in many cases it just returns a wrong value.
I'm calling it from C, like: strcmp("abc", "abd")
and I expect an int
to be returned.
strcmp:
push ebp
mov ebp, esp
cld
xor eax, eax
mov esi, [ebp+0x8]
mov edi, [ebp+0xc]
mov ecx, -1
strcmp_loop:
cmp byte [esi], 0
jz strcmp_end
repe cmpsb
dec esi
dec edi
strcmp_end:
mov al, byte [esi]
sub al, byte [edi]
jns strcmp_ret
neg al
neg eax
strcmp_ret:
pop ebp
ret
The double neg
is to handle negative values, otherwise it returns 255
instead of -1
and so on.