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?)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…