I am trying to implement my own strncmp in asm. Here is the NASM code:
section .text
global my_strncmp
my_strncmp:
mov rcx, 0; using rcx like counter
.next:
cmp rdx, rcx ; i think this comparing doesn't work (i have no idea with it)
je .return_0
mov dh, byte [rdi]
mov dl, byte [rsi]
cmp byte [rdi], 0
je .return_0 ;string ending
cmp byte [rsi], 0
je .return_0 ;string ending
inc rcx
inc rsi
inc rdi
cmp dh, dl
je .next ; the loop continues until the string ends
jl .return_1
jg .retrun_2
.return_0:
mov rax, 0
ret
.return_1:
mov rax, 1
ret
.retrun_2:
mov rax, -1
ret
And I am using this simple C code to run NASM:
int main(){
printf("%d\n", my_strncmp("string2", "string1", 3));
}
My strncmp actually works like ordinary strcmp (from C std. library), it's just not jumping to .return_0 label from line 8 which reads cmp rdx, rcx.
cmp rdx, rcxcan't work because the rest of the code erroneously modifies part of the RDX register (mov dh, byte [rdi]andmov dl, byte [rsi]both touch parts of RDX).You don't need to use 2 registers to implement the 'compare at most' part of the task. Just decrement the 3rd argument and return equal once the count runs out.
jbandja(replacingjlandjg).