Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

assembly - 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 ?

question from:https://stackoverflow.com/questions/65886492/why-does-this-asm-strcmp-return-wrong-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...