diff --git a/Makefile b/Makefile index d7590dc..5d4908a 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,8 @@ SRCS_NAMES = ft_strlen.s\ SRCS_BONUS_NAMES = ft_atoi_base.s\ ft_list_push_front.s\ ft_list_size.s\ - ft_list_sort.s + ft_list_sort.s\ + ft_list_remove_if.s\ SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES)) SRCS_BONUS = $(addprefix $(SRCS_BONUS_DIR)/, $(SRCS_NAMES)) diff --git a/libasm.h b/libasm.h index b7a3bbf..b4ea6a9 100644 --- a/libasm.h +++ b/libasm.h @@ -19,5 +19,6 @@ int ft_atoi_base(char *nbr, char *base); void ft_list_push_front(t_list **begin_list, void *data); int ft_list_size(t_list *list_start); void ft_list_sort(t_list **begin_list, int (*cmp)()); +void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *)); #endif diff --git a/srcs/bonus/ft_list_push_front.s b/srcs/bonus/ft_list_push_front.s index 05f4ccd..6252c53 100644 --- a/srcs/bonus/ft_list_push_front.s +++ b/srcs/bonus/ft_list_push_front.s @@ -9,7 +9,7 @@ ft_list_push_front: push rdi push rsi - mov rdi, 10 + mov rdi, 16 call malloc wrt ..plt pop rsi pop rdi diff --git a/srcs/bonus/ft_list_remove_if.s b/srcs/bonus/ft_list_remove_if.s new file mode 100644 index 0000000..945e3cb --- /dev/null +++ b/srcs/bonus/ft_list_remove_if.s @@ -0,0 +1,95 @@ +global ft_list_remove_if + +section .text + +remove_element: + + call call_free_elem + push rax + mov rax, [rax + 8] + + test r8, r8 + jz element_is_first + + mov [r8 + 8], rax + pop rax + ret + +element_is_first: + mov [rdi], rax + pop rax + ret + + +call_free_elem: + push rdi + push rsi + push rdx + push rcx + push rax + push r8 + + mov rdi, [rax] + call rcx + + pop r8 + pop rax + pop rcx + pop rdx + pop rsi + pop rdi + ret + + + + + +ft_list_remove_if: + test rdi, rdi + jz end + test rdx, rdx + jz end + test rcx, rcx + jz end + + xor r8, r8 + + mov rax, [rdi] +loop: + test rax, rax + jz end + push rax + + push rdi + push rsi + push rdx + push rcx + push r8 + + mov rdi, [rax] + call rdx + + test eax, eax + pop r8 + pop rcx + pop rdx + pop rsi + pop rdi + + jz loop_end + + mov rax, [rsp] + call remove_element + +loop_end: + + pop rax + mov r8, rax + mov rax, [rax + 8] + jmp loop + + + + +end: + ret diff --git a/srcs/ft_strcpy.s b/srcs/ft_strcpy.s index 6e1ff61..5a41c56 100644 --- a/srcs/ft_strcpy.s +++ b/srcs/ft_strcpy.s @@ -7,18 +7,16 @@ ft_strcpy: test rsi, rsi jz err push rdi - jmp loop_start -condition_check: - mov al, [rdi] - test al, al - jz end + loop_start: - mov rax, [rsi] - mov [rdi], rax + mov al, [rsi] + mov [rdi],al lea rdi, [rdi + 1] lea rsi, [rsi + 1] - jmp condition_check + + test al, al + jne loop_start end: pop rax diff --git a/srcs/ft_strdup.s b/srcs/ft_strdup.s index c24723b..90fe4a6 100644 --- a/srcs/ft_strdup.s +++ b/srcs/ft_strdup.s @@ -11,6 +11,7 @@ ft_strdup: push rdi ;push to use it for the strcpy call ft_strlen + inc rax mov rdi, rax call malloc wrt ..plt diff --git a/test.c b/test.c index fbb4ab4..d150114 100644 --- a/test.c +++ b/test.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */ -/* Updated: 2025/04/06 01:39:26 by tomoron ### ########.fr */ +/* Updated: 2025/04/12 18:33:43 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ int test_strcpy(void) memset(buf,'a', 255); buf[255] = 0; - TEST("copy normal string", strcmp(ft_strcpy(buf, "hello"),"hello") == 0); + TEST("copy normal string", strcmp(ft_strcpy(buf, "patate douce"),"patate douce") == 0); TEST("copy empty string", strcmp(ft_strcpy(buf, ""),"") == 0); TEST("copy to null pointer", ft_strcpy(NULL, "") == 0); TEST("copy from null pointer", ft_strcpy("", NULL) == 0); @@ -237,6 +237,40 @@ int test_list_sort() return(fail); } +int remove_if_compare(void *data, int *nb) +{ + return((long)data == *nb); +} + +void remove_if_free(void *data) +{ + printf("%ld\n", (long int) data); +} + +int test_list_remove_if() +{ + int fail = 0; + int nb = 2; + t_list *tmp; + + tmp = 0; + ft_list_push_front(&tmp, (void *)4); + ft_list_push_front(&tmp, (void *)3); + ft_list_push_front(&tmp, (void *)2); + ft_list_push_front(&tmp, (void *)1); + + ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free); + TEST("is the number 2 gone", (long int)tmp->next->data == 3); + nb = 1; + ft_list_remove_if(&tmp, &nb, remove_if_compare, remove_if_free); + TEST("is the number 1 gone", (long int)tmp->data == 3); + ft_list_remove_if(&tmp, &nb, 0, remove_if_free); + ft_list_remove_if(&tmp, &nb, remove_if_compare, 0); + TEST("null pointers in functions", 1); + + return(fail); +} + int main(void) { @@ -260,5 +294,7 @@ int main(void) printf("\n\n"); FNC_TEST("ft_list_sort", test_list_sort()); printf("\n\n"); + FNC_TEST("ft_list_sort", test_list_remove_if()); + printf("\n\n"); FNC_TEST("check valgrind", 1); }