From 5d5c7aba23e0cde2ef739ea7867fe82316351fa7 Mon Sep 17 00:00:00 2001 From: y-syo Date: Thu, 4 Dec 2025 23:19:20 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip(s?= =?UTF-8?q?tr*):=20working=20on=20them,=20strcmp=20doesn't=20work=20the=20?= =?UTF-8?q?rest=20does=20i=20think?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/str/ft_strcmp.s | 32 ++++++++++++++++++++++++++++++++ src/str/ft_strcpy.s | 28 ++++++++++++++++++++++++++++ src/str/ft_strlen.s | 18 ++++++++++++++++++ src/str/main.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/str/ft_strcmp.s create mode 100644 src/str/ft_strcpy.s create mode 100644 src/str/ft_strlen.s create mode 100644 src/str/main.c diff --git a/src/str/ft_strcmp.s b/src/str/ft_strcmp.s new file mode 100644 index 0000000..03097fb --- /dev/null +++ b/src/str/ft_strcmp.s @@ -0,0 +1,32 @@ +section .text +global ft_strcmp + +; int ft_strcmp(const char *s1, const char *s2) +ft_strcmp: + mov rax, 0 + cmp rdi, 0 + je .ret + cmp rsi, 0 + je .ret + +.loop: + + mov r10b, BYTE [rdi] + cmp r10b, BYTE [rsi] + je .end + + cmp BYTE [rdi], 0 + je .end + cmp BYTE [rsi], 0 + je .end + + inc rsi + inc rdi + jmp .loop + +.end: + mov al, BYTE [rsi] + mov r10b, BYTE [rdi] + sub rax, r10 +.ret: + ret diff --git a/src/str/ft_strcpy.s b/src/str/ft_strcpy.s new file mode 100644 index 0000000..25c220c --- /dev/null +++ b/src/str/ft_strcpy.s @@ -0,0 +1,28 @@ +section .text +global ft_strcpy + +; char *ft_strcpy(char *dest, const char *src) +ft_strcpy: + mov rax, 0 + cmp rdi, 0 + je .end + cmp rsi, 0 + je .end + + mov rax, rdi + +.loop: + mov r10b, BYTE [rsi] + cmp r10b, 0 + je .end_loop + + mov BYTE [rdi], r10b + inc rsi + inc rdi + jmp .loop + +.end_loop: + mov BYTE [rdi], 0 + +.end: + ret diff --git a/src/str/ft_strlen.s b/src/str/ft_strlen.s new file mode 100644 index 0000000..a3981ca --- /dev/null +++ b/src/str/ft_strlen.s @@ -0,0 +1,18 @@ +section .text +global ft_strlen + +; size_t ft_strlen(const char *str) +ft_strlen: + mov rax, rdi + cmp rdi, 0 + je .end + +.loop: + cmp BYTE [rax], 0 + je .end + inc rax + jmp .loop + +.end: + sub rax, rdi + ret diff --git a/src/str/main.c b/src/str/main.c new file mode 100644 index 0000000..e01a185 --- /dev/null +++ b/src/str/main.c @@ -0,0 +1,30 @@ +int ft_strlen(char *str); +char *ft_strcpy(char *dest, const char *src); +int ft_strcmp(const char *s1, const char *s2); + +#include +#include +#include + +int main(void) +{ + printf("\n--- ft_strlen ---\n"); + printf("arg : \"i love kissing girls\" | result : %d\n", ft_strlen("i love kissing girls")); + printf("arg : NULL | result : %d\n", ft_strlen(NULL)); + printf("arg : \"\" | result : %d\n", ft_strlen("")); + + printf("\n--- ft_strcpy ---\n"); + char *a1 = calloc(sizeof(char), 10); + char *a2 = calloc(sizeof(char), 10); + printf("arg : \"abcdefgh\" | result : %s\n", ft_strcpy(a1, "abcdefgh")); + printf("arg : \"abcdefgh\" | result : %s\n", strcpy(a2, "abcdefgh")); + printf("arg : \"67\" | result : %s\n", ft_strcpy(a1, "67")); + printf("arg : \"67\" | result : %s\n", strcpy(a2, "67")); + printf("arg : \"aaa\" (dest = NULL) | result : %s\n", ft_strcpy(NULL, "67")); + printf("arg : NULL | result : %s\n", ft_strcpy(a2, NULL)); + + + printf("\n--- ft_strcmp ---\n"); + printf("arg : \"aaaaaa\" \"aaaa\" | result : %d (expected : %d)\n", ft_strcmp("aaaaaa", "aaaa"), strcmp("aaaaaa", "aaaa")); + printf("arg : \"aaaa\" \"bb\" | result : %d (expected : %d)\n", ft_strcmp("aaaa", "bb"), strcmp("aaaa", "bb")); +}