libasm/src/ft_strdup.s
2026-01-17 18:23:33 +01:00

27 lines
855 B
ArmAsm

segment .note.GNU-stack
extern ft_strcpy
extern ft_strlen
extern malloc
section .text
global ft_strdup
ft_strdup:
push rbx ; save rbx
cmp rdi, 0 ; check s is NULL
je .exit ; exit (with restore of rbx) if true
mov rbx, rdi ; save rdi value in rbx
call ft_strlen ; call ft_strlen on rdi
lea rdi, [rax + 1] ; calculation of the malloc size
call malloc ; malloc is called return value on rax
test rax, rax ; check the return value of malloc
je .exit ; exit (with restore of rbx) if true
mov rdi, rax ; set the dest string (for ft_strcpy)
mov rsi, rbx ; set the source string (for ft_strcpy)
pop rbx ; restore rbx
jmp ft_strcpy ; call ft_strcpy
.exit:
pop rbx ; restore rbx
ret ; return rax (NULL)