2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
cahenepudi
  • 31
  • 3
  • What does it return in that case? – Sami Kuhmonen Dec 26 '18 at 16:54
  • 2
    **`repe cmpsb` implements `memcmp`, not `strcmp`**. If the strings are equal out to the terminating 0 byte, `repe cmpsb` will keep going until it finds a difference in whatever comes next, or gets to an unmapped page and segfaults. Your whole approach is flawed, regardless slow branch sign-extension. You'd have to `strlen` one of your inputs first, with `repne scasb`. But don't do that because it's slow, and so is `repe cmpsb`. [Fast Strings microcode only applies to `rep stos/movs`.](https://stackoverflow.com/q/33480999) – Peter Cordes Dec 26 '18 at 16:58
  • `movsx eax, al` would be much more efficient. – Peter Cordes Dec 26 '18 at 17:02

0 Answers0