My strdup in ASM

Viewed 495

Hello every body for my school i have to do my own strdup function in ASM [intel] [NASM].

I have e strange problem ...

In my code if I call _malloc

My code segfault with this error :

Program received signal SIGSEGV, Segmentation fault.
0x00007fff849612da in stack_not_16_byte_aligned_error () from /usr/lib/system/libdyld.dylib

I don't understand why because In the section .text I said extern _malloc

Some one have an idee why I got this mistake ? :)

This is my code :

section .text
     global _ft_strdup
     extern _strlen
     extern _malloc
     ;  extern _ft_memcpy

_ft_strdup:
     call _strlen           ;rax = len of str
     mov r8, rdi            ;r8 = str = src
     inc rax                ;rax++
     ;  mov r9, rax         ;len of dest with '\0'
     mov rdi, rax           ;to send the len for malloc
     call _malloc           ;rax = ptr of dest
     ;  cmp rax, 0          ;malloc failled
     ;  jle _error_malloc
     ;  mov rdi, rax        ;malloc param 1 of ft_memcpy
     ;  mov rsi, r8         ;str in param 2 of ft_memcpy
     ;  mov rdx, r9         ;len of str with '\0' param 3 of ft_memcpy
     ;  call _ft_memcpy     ;call ft_memcpy
     ret
_error_malloc:
     xor rax, rax           ;return NULL
     ret

All functions start with ft_are the same than the libc Thx all

1 Answers
Related