From 62e5d095b6eb4bdadbf0dec441bc22731a147b18 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 8 Mar 2024 20:15:51 +0100 Subject: [PATCH] feat(bonus): size and push_front OK, sort WIP --- Makefile | 7 ++++- ft_list_print.c | 9 ++++++ ft_list_push_front.s | 41 ++++++++++++++++++++++++ ft_list_size.s | 12 +++++++ ft_list_sort.s | 64 ++++++++++++++++++++++++++++++++++++++ libasm.h | 11 +++++++ main.c | 74 ++++++++++++++------------------------------ 7 files changed, 167 insertions(+), 51 deletions(-) create mode 100644 ft_list_print.c create mode 100644 ft_list_push_front.s create mode 100644 ft_list_size.s create mode 100644 ft_list_sort.s diff --git a/Makefile b/Makefile index 6f494b4..8b0ce74 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ SRC = \ ft_write.s \ ft_read.s \ ft_strdup.s \ + ft_list_size.s \ + ft_list_push_front.s \ + ft_list_sort.s \ OBJ = $(SRC:.s=.o) @@ -15,11 +18,13 @@ OBJ = $(SRC:.s=.o) all: $(NAME) +bonus: all + $(NAME): $(OBJ) ar rcs libasm.a $(OBJ) test: $(NAME) - gcc -Wall -Werror -Wextra -no-pie -g main.c -L. -lasm -o libasm_unit_tests + gcc -Wall -Werror -Wextra -no-pie -g ft_list_print.c main.c -L. -lasm -o libasm_unit_tests clean: rm $(OBJ) diff --git a/ft_list_print.c b/ft_list_print.c new file mode 100644 index 0000000..632ea9c --- /dev/null +++ b/ft_list_print.c @@ -0,0 +1,9 @@ +#include "libasm.h" +#include + +void ft_list_print(t_list *begin_list) { + while (begin_list) { + printf("%s", (char *)begin_list->data); + begin_list = begin_list->next; + } +} diff --git a/ft_list_push_front.s b/ft_list_push_front.s new file mode 100644 index 0000000..6ddfea4 --- /dev/null +++ b/ft_list_push_front.s @@ -0,0 +1,41 @@ +global ft_list_push_front +extern malloc + +ft_list_push_front: + push rsp + push rdi + push rsi + mov rdi, 16 + call malloc + cmp rax, 0 + je .done + pop rsi + pop rdi + mov [rax], rsi + mov rbx, [rdi] + mov [rax + 8], rbx + mov [rdi], rax +.done: + pop rsp + ret + +;ft_list_push_front: +; push ebp +; mov ebp, esp +; sub esp 0x10 +; push rdi +; push rsi +; mov rdi, 16 +; call malloc +; pop rsi +; pop rdi +; ;cmp rax, 0 +; ;je .error +; +; mov rbx, [rdi] +; mov rbx, [rbx + 8] +; mov [rax], rsi +; mov [rax + 8], rbx +; mov [rdi], rax +; leave +; ret diff --git a/ft_list_size.s b/ft_list_size.s new file mode 100644 index 0000000..566b9d8 --- /dev/null +++ b/ft_list_size.s @@ -0,0 +1,12 @@ +global ft_list_size + +ft_list_size: + xor rax, rax +.loop: + cmp rdi, 0 + je .done + mov rdi, [rdi + 8] + inc rax + jmp .loop +.done: + ret diff --git a/ft_list_sort.s b/ft_list_sort.s new file mode 100644 index 0000000..b1b3615 --- /dev/null +++ b/ft_list_sort.s @@ -0,0 +1,64 @@ +global ft_list_sort + +ft_list_sort: + push rsp + push rdi ; push begin_list + push rsi ; push cmp_func + push r12 ; callee saved + push r13 ; same + push r14 ; same + mov r12, 0 ; r12 "prev" = NULL + mov r13, [rdi] ; r13 "curr" = begin_list + mov r14, [rdi] ; r14 begin_list memory + mov rcx, rsi ; rcx = cmp_func + +.loop: + cmp qword [r13 + 8], 0 ; if curr->next is null then exit + je .done + mov rdi, [r13] ; first arg is curr->data + mov rsi, [r13 + 8] ; rsi = curr->next; + mov rsi, [rsi] ; rsi = rsi->data; + ;push rbx ; rbx is caller saved + ;push r13 ; same + ; align stack here ? + sub rsp, 0; align stack ??? + call rcx + add rsp, 0 + ;pop rdx ; rdx is caller saved + ;pop rbx ; rbx is caller saved + cmp rax, 0 + jle .else ; if in order continue... + cmp r13, r14 + je .is_first_elem + mov rbx, [r12 + 8] + mov rbx, [r13 + 8] + mov rbx, [r13 + 8] + mov rbx, [rbx + 8] + mov [r13 + 8], rbx + mov rbx, [r12 + 8] + mov rbx, [rbx + 8] + mov rbx, r13 + jmp .loop + +.is_first_elem: + mov r14, [r13 + 8] + mov rbx, [r13 + 8] + mov rbx, [rbx + 8] + mov [r13 + 8], rbx + mov [r14 + 8], r13 + jmp .loop + +.else: ; ...here + mov r12, rdx ; prev = curr; + mov rdx, [rdx + 8] ; curr = curr->next; + jmp .loop + + +.done: + pop r14 + pop r13 ; restore stack and return + pop r12 + pop rsi + pop rdi + pop rsp + ret diff --git a/libasm.h b/libasm.h index 4f1dcb6..5709d6b 100644 --- a/libasm.h +++ b/libasm.h @@ -5,6 +5,11 @@ #include #include +typedef struct s_list { + void *data; + struct s_list *next; +} t_list; + size_t ft_strlen(const char *); char *ft_strcpy(char *, const char *); int ft_strcmp(const char *, const char *); @@ -12,4 +17,10 @@ ssize_t ft_write(int, const void *, size_t); ssize_t ft_read(int, void *, size_t); char *ft_strdup(const char *); +void ft_list_push_front(t_list **, void *); +int ft_list_size(t_list *); +void ft_list_sort(t_list **, int (*)()); + +void ft_list_print(t_list *begin_list); + #endif diff --git a/main.c b/main.c index 0f61ff4..808b54b 100644 --- a/main.c +++ b/main.c @@ -1,53 +1,27 @@ -#include "libasm.h" +#include #include -#include -#include +#include +#include "libasm.h" -void my_test(void) { - write(1, "Hello, world!\n", 14); - printf("Hello, world!\n"); -} - -void *my_malloc(size_t size) { - printf("CALLLLLLED\n"); - fflush(stdout); - printf("called with size %ld\n", size); - fflush(stdout); - printf("222323\n"); - fflush(stdout); - return malloc(size); -} - - -int main() { -/* char *str1 = (char *)malloc(88 * sizeof(char)); - ft_strcpy(str1, "Salut moi une chaine de caractere trop longue\n"); - printf("%ld\n", ft_write(1, str1, 46)); - printf("%d\n", (int)str1[0]); - free(str1); - - char *str2 = (char *)malloc(88 * sizeof(char)); - strcpy(str2, "Salut moi une chaine de caractere trop longue\n"); - printf("%ld\n", write(1, str2, 46)); - printf("%d\n", (int)str2[0]); - free(str2); - - ssize_t write_ret = write(4, "test\n", 5); - if (write_ret == -1) { - fprintf(stderr, "error = %s\n", strerror(errno)); - perror("write"); - } - errno = 42; - write_ret = ft_write(4, "test\n", 5); - printf("ft_write ret %ld\n", write_ret); - if (write_ret == -1) { - fprintf(stderr, "error = %d\n", errno); - fprintf(stderr, "error = %s\n", strerror(errno)); - perror("ft_write"); - }*/ - char *str = ft_strdup("Salut c'est moi libasm\n"); - ft_write(1, "Hello, world!\n", 14); - printf("len %ld\n", ft_strlen(str)); - ft_write(1, str, ft_strlen(str)); - return 0; +int main(void) { + t_list *list; + + list = NULL; + + ft_list_push_front(&list, "toto\n"); + ft_list_push_front(&list, "tutu\n"); + ft_list_push_front(&list, "zaza\n"); + ft_list_push_front(&list, "bobo\n"); + ft_list_push_front(&list, "tata\n"); + ft_list_push_front(&list, "babar\n"); + ft_list_push_front(&list, "zoro\n"); + + ft_list_print(list); + + printf("%lu\n", sizeof(t_list)); + printf("%d\n", ft_list_size(list)); + + ft_list_sort(&list, &strcmp); + printf("On est sorti.e.s de cet enfer\n"); + ft_list_print(list); }