From c7f7e10e74b0213a28b14494f51c8e01343f141b Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 13 Dec 2025 13:36:08 +0100 Subject: [PATCH 01/10] feat(strcpy): adding my own strcpy version --- src/ft_strcpy.s | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/ft_strcpy.s diff --git a/src/ft_strcpy.s b/src/ft_strcpy.s new file mode 100644 index 0000000..de5cd90 --- /dev/null +++ b/src/ft_strcpy.s @@ -0,0 +1,23 @@ +segment .note.GNU-stack + +section .text + global ft_strcpy + +ft_strcpy: + mov rax, rdi ; dest is the return value + cmp rdi, 0 ; check if dest is NULL + je .exit ; if true return NULL + cmp rsi, 0 ; check if dest is NULL + je .exit ; if true return NULL + xor ecx, ecx ; index set to 0 + +.loop: + mov dl, BYTE [rsi + rcx] ; take the value of src + mov BYTE [rax + rcx], dl ; copy the value of src in dst + cmp dl, 0 ; check if the null byte is the last copied + je .exit ; return if it was the last byte + inc rcx ; increment the index + jmp .loop ; restart the loop again + +.exit: + ret ; return the rax (dest) value From b2c1ad07c9beb350c2c8925461f7be4b9951c854 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 13 Dec 2025 13:42:55 +0100 Subject: [PATCH 02/10] test(read): increase the buffer size --- test/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/main.c b/test/main.c index c40690a..24cdb90 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/12 14:02:17 by rparodi #+# #+# */ -/* Updated: 2025/12/13 13:30:29 by rparodi ### ########.fr */ +/* Updated: 2025/12/13 13:41:58 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -171,8 +171,8 @@ static void _test_read(void) int original, homemade; int err_original, err_homemade; int test_file; - char buf_orig[100]; - char buf_home[100]; + char buf_orig[8192]; + char buf_home[8192]; printf("\n%sTesting '%sft_read%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET); From 0bfedb14e00943a7f7540d0b23174bf79309d4cd Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 28 Dec 2025 19:49:35 +0100 Subject: [PATCH 03/10] refactor(read): adding a whitespace between segment and extern --- includes/libasm.h | 3 ++- src/ft_read.s | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/libasm.h b/includes/libasm.h index 4b9378b..9744e6a 100644 --- a/includes/libasm.h +++ b/includes/libasm.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/12 14:02:08 by rparodi #+# #+# */ -/* Updated: 2025/12/13 13:18:50 by rparodi ### ########.fr */ +/* Updated: 2025/12/28 19:42:43 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include #include +char *ft_strdup(char *s); char *ft_strcpy(char *dest, const char *src); int ft_strcmp(const char *s1, const char *s2); size_t ft_strlen(char *s); diff --git a/src/ft_read.s b/src/ft_read.s index 111e5bb..3cbabc3 100644 --- a/src/ft_read.s +++ b/src/ft_read.s @@ -1,4 +1,5 @@ segment .note.GNU-stack + extern __errno_location section .text From 13be686bcd6e6941123f2304cae1e04a243b6261 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sun, 28 Dec 2025 19:49:42 +0100 Subject: [PATCH 04/10] refactor(write): adding a whitespace between segment and extern --- src/ft_write.s | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ft_write.s b/src/ft_write.s index cd27233..ec756a4 100644 --- a/src/ft_write.s +++ b/src/ft_write.s @@ -1,4 +1,5 @@ segment .note.GNU-stack + extern __errno_location section .text From e84d0ea7745934404f4ef983c8a33da01b78b9f1 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 29 Dec 2025 16:58:18 +0100 Subject: [PATCH 05/10] feat(read): adding the return value on -1 if the buf is NULL --- src/ft_read.s | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ft_read.s b/src/ft_read.s index 3cbabc3..5eb489c 100644 --- a/src/ft_read.s +++ b/src/ft_read.s @@ -7,6 +7,7 @@ section .text ft_read: cmp rsi, 0 ; check if the buffer is NULL + mov rax, -1 ; set the return value to -1 (for error) (UB) je .exit ; if true go to exit mov rax, 0 ; syscall 0 is for read syscall ; call the system call read From cb24a4301ec0196d92c281d9bca2168fc4f6cb00 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 29 Dec 2025 17:03:30 +0100 Subject: [PATCH 06/10] docs(strcpy): fix the documentation for check NULL for rsi --- src/ft_strcpy.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ft_strcpy.s b/src/ft_strcpy.s index de5cd90..305e699 100644 --- a/src/ft_strcpy.s +++ b/src/ft_strcpy.s @@ -7,7 +7,7 @@ ft_strcpy: mov rax, rdi ; dest is the return value cmp rdi, 0 ; check if dest is NULL je .exit ; if true return NULL - cmp rsi, 0 ; check if dest is NULL + cmp rsi, 0 ; check if src is NULL je .exit ; if true return NULL xor ecx, ecx ; index set to 0 From ac1ce88633e4f19db66fd9d4155a2049de8c3a63 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 13 Jan 2026 14:00:21 +0100 Subject: [PATCH 07/10] 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) From d287180a85ee12fdc5b24d8108b72295fc64e341 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 13 Jan 2026 14:02:48 +0100 Subject: [PATCH 08/10] build(make): adding the strdup to the makefile --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ba5f71c..3a05232 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2025/12/13 13:27:57 by rparodi ### ########.fr # +# Updated: 2025/12/28 19:40:32 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -39,6 +39,7 @@ OBJ_BONUS = $(addprefix $(OBJDIRNAME)/,$(SRC_BONUS:.s=.o)) SRC = src/ft_strlen.s \ src/ft_strcmp.s \ src/ft_strcpy.s \ + src/ft_strdup.s \ src/ft_write.s \ src/ft_read.s From 93a750ca6a34ef3eea3537d46e852973f3214c09 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 13 Jan 2026 14:03:15 +0100 Subject: [PATCH 09/10] test(strdup): adding test on strdup function --- test/main.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/main.c b/test/main.c index 24cdb90..ad5a8e0 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/12 14:02:17 by rparodi #+# #+# */ -/* Updated: 2025/12/13 13:41:58 by rparodi ### ########.fr */ +/* Updated: 2026/01/13 12:12:01 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "_internal_test.h" #include #include +#include #include #include #include @@ -121,6 +122,30 @@ static void _test_strcpy(void) putchar('\n'); } +static void _test_strdup(void) +{ + printf("\n%sTesting '%sstrdup%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET); + ft_strdup(NULL); + printf("%s✔%s ", CLR_GREEN, RESET); + for (int i = 0; strs[i]; i++) + { + char buf_orig[8192]; + char buf_home[8192]; + memset(buf_orig, 0, sizeof(buf_orig)); + memset(buf_home, 0, sizeof(buf_home)); + char *ret_orig = strdup(strs[i]); + char *ret_home = ft_strdup(strs[i]); + if (strcmp(ret_orig, ret_home) == 0 && strcmp(buf_orig, buf_home) == 0) + printf("%s✔%s", CLR_GREEN, RESET); + else + dprintf(2, "\t%s✘%s", CLR_RED, RESET); + putchar(' '); + free(ret_orig); + free(ret_home); + } + putchar('\n'); +} + static void _test_write(void) { int original, homemade; @@ -222,6 +247,7 @@ int main(int argc, char *argv[]) _test_strcpy, _test_write, _test_read, + _test_strdup, NULL }; char *func_name[] = { @@ -230,6 +256,7 @@ int main(int argc, char *argv[]) "strcpy", "write", "read", + "strdup", NULL }; From b5172551b8c53ed6085e4ffc65b6bf2f87eca76b Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 17 Jan 2026 18:23:33 +0100 Subject: [PATCH 10/10] refactor(strdup): only one exit can change to .exit .restore_exit => .exit --- src/ft_strdup.s | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ft_strdup.s b/src/ft_strdup.s index 7b57390..2bd7fa4 100644 --- a/src/ft_strdup.s +++ b/src/ft_strdup.s @@ -10,18 +10,18 @@ section .text ft_strdup: push rbx ; save rbx cmp rdi, 0 ; check s is NULL - je .restore_exit ; exit (with restore of rbx) if true + 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 .restore_exit ; exit (with restore of rbx) if true + 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 -.restore_exit: +.exit: pop rbx ; restore rbx ret ; return rax (NULL)