From eadf7cde629cfc80d4a23cdf4f826abd48220704 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 5 Sep 2024 18:14:20 +0200 Subject: [PATCH] add: ft_list_push_front --- include/libasm.h | 3 ++- src/ft_list_push_front.asm | 27 +++++++++++++++++++++++++++ test/test.c | 32 +++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/ft_list_push_front.asm diff --git a/include/libasm.h b/include/libasm.h index 83156a5..f5ceefb 100644 --- a/include/libasm.h +++ b/include/libasm.h @@ -12,4 +12,5 @@ ssize_t ft_write(int fd, const void *buf, size_t count); char *ft_strdup(const char *s); ssize_t ft_read(int fildes, void *buf, size_t nbyte); -int ft_list_size(t_list *begin_list); \ No newline at end of file +int ft_list_size(t_list *begin_list); +void ft_list_push_front(t_list **begin_list, void *data); \ No newline at end of file diff --git a/src/ft_list_push_front.asm b/src/ft_list_push_front.asm new file mode 100644 index 0000000..7f52808 --- /dev/null +++ b/src/ft_list_push_front.asm @@ -0,0 +1,27 @@ +extern malloc + +section .text + global ft_list_push_front + ft_list_push_front: + + push rdi + push rsi + + mov rdi, 16 + + call malloc wrt ..plt + + cmp rax, 0 + je out + + pop rsi + pop rdi + + mov rdx, [rdi] + mov [rax + 0], rsi ; .data = arg#1 + mov [rax + 8], rdx ; .next = *arg#0 + + mov [rdi], rax + + out: + ret \ No newline at end of file diff --git a/test/test.c b/test/test.c index f5b1227..5701f6a 100644 --- a/test/test.c +++ b/test/test.c @@ -103,6 +103,7 @@ t_list *create_list(size_t len) for (size_t i = 0; i < len; i++) { current->next = malloc(sizeof(t_list)); + current->data = (void *) i; if (current->next == NULL) { destroy_list(current, NULL); @@ -120,7 +121,7 @@ t_list *create_list(size_t len) void multiple_test_list_size() { size_t len; - t_list **root; + t_list *root; size_t specific_size[nb_specific_test] = {0}; srand(time(NULL)); @@ -137,6 +138,31 @@ void multiple_test_list_size() } } +void multiple_test_push_front() +{ + size_t len; + t_list *root; + + srand(time(NULL)); + + for (size_t i = 0; i < nb_random_test; i++) + { + len = rand() % 100; + + root = create_list(len); + if (root == NULL) + return; + ft_list_push_front(&root, (void *) 101); + printf("data: "); + test_size_t((size_t) root->data, 101); + printf(" data-next: "); + test_size_t((size_t) root->next->data, 0); + printf(" list-size: "); + test_int(len + 1, ft_list_size(root)); + printf("\n"); + } +} + int main() { printf("STRLEN\n"); @@ -165,4 +191,8 @@ int main() printf("FT_LIST_SIZE\n"); multiple_test_list_size(); printf("\n"); + + printf("FT_LIST_PUSH_FRONT\n"); + multiple_test_push_front(); + printf("\n"); } \ No newline at end of file