I am trying to implement my version of the strcpy function using x64 assembly on macos. I came across a SEGV error that I don't understand.
Here's my assembly code.
section .text
global _ft_strcpy
_ft_strcpy:
mov rax, rdi
loop:
mov rbx, [rsi]
mov [rdi], rbx
inc rdi
inc rsi
cmp [rsi] , byte 0
jne loop
end:
mov [rdi], byte 0
ret
Here's my main.c used for testing.
#include <stdio.h>
#include <string.h>
int main(void)
{
char src [11] = "Hello moto";
char dest [11];
ft_strcpy(dest, src);
printf("|%p|\n", src);
printf("|%s|\n", src);
printf("|%p|\n", dest);
printf("|%s|\n", dest);
return (0);
}
The output of fsanitize.
=================================================================
==53615==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000097 (pc 0x0001087a2c5f bp 0x7ffee745d990 sp 0x7ffee745d880 T0)
==53615==The signal is caused by a READ memory access.
==53615==Hint: address points to the zero page.
#0 0x1087a2c5e in main main.c:10
#1 0x7fff796e33d4 in start (libdyld.dylib:x86_64+0x163d4)
==53615==Register values:
rax = 0x00007ffee745d8c0 rbx = 0x000000000000004f rcx = 0x4f4d204f4c4c4500 rdx = 0x00001fffdce8bb10
rdi = 0x00000001087a2e60 rsi = 0x00007ffee745d8aa rbp = 0x00007ffee745d990 rsp = 0x00007ffee745d880
r8 = 0x00001fffdce8bb10 r9 = 0x00000001087a2e20 r10 = 0x0000000117f89c30 r11 = 0x00007ffddecbaa80
r12 = 0x0000000000000000 r13 = 0x0000000000000000 r14 = 0x0000000000000000 r15 = 0x0000000000000000
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV main.c:10 in main
The segfault seems to occurs after my call to ft_strcpy, on the first printf call. When I'm using the rcx register instead of the rbx one (in my assembly code), this program works. I've looked up the difference between rcx and rbx (Caller-saved vs Callee-saved), but I don't understand why it causes this problem. What am I missing ?
Feel free to point out any bad practices, I'm taking any advices here!
Thanks for reading.