I just started programming in assembly so I am a beginner.
To practice, I am trying to rewrite a basic libc in assembly (NASM Intel syntax).
But I'm stuck on the strcmp function:
;; Compare two C-style NUL-terminated strings
;; Inputs : ESI = address of s1, EDI = address of s2
;; Outputs : EAX = return an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2
strcmp:
call strlen
mov ecx, eax ; ecx = length of the string in esi
repe cmpsb
sub esi, edi ; result = *esi - *edi
mov eax, esi
ret
For me, it should work like this:
s1 db 'Hello World', 0
s2 db 'Hello Stack', 0
After the repe cmpsb
instruction, ESI
should be equal to [s1 + 7]
and EDI
to [s2 + 7]
.
So I just have to do EAX
= 'W' - 'S' = 87 - 83 = 4
The problem is, it doesn't work. I think the problem is that when I execute this instruction:
sub esi, edi ; result = *esi - *edi
I don't think that it means: subtract the characters pointed to by EDI
and ESI
.
Does anyone have an idea on how I can do this?