I'm new to assembly (Intel x86_64) and I am trying to recode some functions from the C library. I am on a 64-bit Linux and compiling with NASM.
I have an error with the strchr function and I can't find a solution...
As a reminder here is an extract from the man page of strchr :
char *strchr(const char *s, int c);
The strchr() function returns a pointer to the first occurrence of the character c in the string s.
Here is what I tried :
strchr:
push rpb
mov rbp, rsp
mov r12, rdi ; get first argument
mov r13, rsi ; get second argument
call strchr_loop
strchr_loop:
cmp [r12], r13 ; **DON'T WORK !** check if current character is equal to character given in parameter...
je strchr_end ; go to end
cmp [r12], 0h ; test end of string
je strchr_end ; go to end
inc r12 ; move to next character of the string
jmp strchr_loop ; loop
strchr_end
mov rax, r12 ; set function return
mov rsp, rbp
pop rbp
This return a pointer on the ned of the string and don't find the character...
I think it's this line which doesn't work :
cmp [r12], r13
I tested with this and it worked :
cmp [r12], 97 ; 97 = 'a' in ASCII
The example :
char *s;
s = strchr("blah", 'a');
printf("%s\n", s);
Returned :
ah
But I can't make it work with a register comparison. What am I doing wrong, and how can I fix it?