I tried to run the following program:
C code :
int main()
{
char *s1 = "hello";
printf("string : %s\n", strchr(s1, 'l'));
}
assembly code:
global strchr
section .text
strchr:
push rbp
mov rbp, rsp
strchr_loop:
mov al, byte [rsi] ; My bug come from here
cmp byte [rdi], al ; and from here
je strchr_end
cmp byte[rdi], 0
jz strchr_nul
inc rdi
jmp strchr_loop
strchr_end:
mov rax, rdi
mov rsp, rbp
pop rbp
ret
strchr_nul:
mov rax, 0
mov rsp, rbp
pop rbp
ret
When I executed this, I got a Segmentation fault.
But when I replace the bug line with the value 'l' instead of use al, the program is working
cmp byte [rdi], 'l' ; It's working