Search code examples
assemblyx86-64strcmp

why does this asm strcmp() return wrong values


i'm trying to learn some assembly (intel syntax, x86_64). I wrote this code to make a simple implementation of strcmp():

section .text
    global ft_strcmp

ft_strcmp:
    cmp byte [rsi], 0
    je  exit
    mov cl, byte [rsi]
    cmp byte [rdi], cl
    jne exit
    inc rsi
    inc rdi
    jmp ft_strcmp

exit:
    xor rax, rax
    mov al, [rdi]
    sub al, byte [rsi]
    ret

But trying it by calling ft_strcmp("Hello", "Hellooooo") return 145, whereas the real strcmp() return -1, and I cant seems to figure why. Am I wrong with my syntax, or is the way I try to do this ?


Solution

  • strcmp is supposed to return a 32-bit int in eax which is positive or negative according to whether the first string is greater or less. By doing an 8-bit subtract, the upper 24 bits of eax remain zero, so that the result is positive when viewed as a signed integer.

    You want to do a 32-bit subtract, so you need both bytes in 32-bit registers with their upper 24 bits zeroed. This is efficient to do with movzx:

    exit:
        movzx eax, byte [rdi]
        movzx ecx, byte [rsi]
        sub eax, ecx
        ret
    

    If you didn't know about movzx, you could zero the whole register and then load the low byte:

    exit:
        xor eax, eax
        mov al, [rdi] ; 'byte' is unnecessary, operand size inferred from register al
        xor ecx, ecx
        mov cl, [rsi]
        sub eax, ecx
        ret
    

    (As a side comment, zeroing instructions like xor rax, rax can be replaced by xor eax, eax which is smaller and has the same effect: Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?)