From ac1ce88633e4f19db66fd9d4155a2049de8c3a63 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 13 Jan 2026 14:00:21 +0100 Subject: [PATCH] feat(strdup): adding the function in asm --- src/ft_strdup.s | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/ft_strdup.s diff --git a/src/ft_strdup.s b/src/ft_strdup.s new file mode 100644 index 0000000..7b57390 --- /dev/null +++ b/src/ft_strdup.s @@ -0,0 +1,27 @@ +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 .restore_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 .restore_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 + +.restore_exit: + pop rbx ; restore rbx + ret ; return rax (NULL)