I am trying to implement my own strcmp in asm. Here is the ft_strcmp.s file:
global ft_strcmp
section .text
ft_strcmp:
mov eax, [rdi]
sub eax, [rsi]
jne .exit
cmp byte [rdi], 0 ; if s1 end
je .exit
cmp byte [rsi], 0 ; if s2 end
je .exit
inc rdi
inc rsi
jmp ft_strcmp
.exit:
ret
For the first letter it's OK: char *s1 = "Hello World" char *s2 = "Jdllo World" The result is 1. (0000 0001)
The problem is that when I am trying to compare these strings:
char *s1 = "Hello World"
char *s2 = "Hdllo World"
The result in RAX is NOT 1, but 256. (0000 0001 0000 0000)
Another example:
char *s1 = "Hello World"
char *s2 = "Hcllo World"
The result in RAX is NOT 2, but 512. (0000 0010 0000 0000)
As you have already understood, the result for third different letters will be:
char *s1 = "Hello World"
char *s2 = "Heklo World"
The result in RAX is NOT 1, but 65 536. (0000 0001 0000 0000 0000 0000)
I realize that RAX is incrementing not right, but I can't find the mistake in the code. So I ask you to help me understand.