feat(list_remove_if)

This commit is contained in:
gbrochar 2024-03-10 20:05:57 +01:00
parent 73d921ebc7
commit 69b65b653a
4 changed files with 81 additions and 2 deletions

View File

@ -10,6 +10,7 @@ SRC = \
ft_list_size.s \
ft_list_push_front.s \
ft_list_sort.s \
ft_list_remove_if.s \
OBJ = $(SRC:.s=.o)

61
ft_list_remove_if.s Normal file
View File

@ -0,0 +1,61 @@
global ft_list_remove_if
extern free
ft_list_remove_if:
push rsp ; push callee saved registers
push rbx
push r12
push r13
push r14
push r15
push rsi
mov r14, rdx ; save cmp func
mov r15, rcx ; save free func
mov r13, rdi ; save begin_list
mov rbx, 0 ; prev = null
mov r12, [r13] ; curr = *begin_list
.loop:
cmp r12, 0
je .done
mov rdi, [r12]
pop rsi
push rsi
call r14
cmp rax, 0
jne .next
cmp rbx, 0
je .first_elem
mov rdi, [r12]
call r15
mov rax, [r12 + 8]
mov [rbx + 8], rax
mov rdi, r12
call free WRT ..plt
mov r12, [rbx + 8]
jmp .loop
.first_elem:
mov rdi, [r12]
call r15
mov rax, [r12 + 8]
mov [r13], rax
mov rdi, r12
call free WRT ..plt
mov r12, [r13]
jmp .loop
.next:
mov rbx, r12 ; prev = curr
mov r12, [r12 + 8] ; curr = curr->next
jmp .loop
.done:
pop rsi
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rsp
ret

View File

@ -20,7 +20,9 @@ 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_remove_if(t_list **, void *, int (*)(), void (*)(void *));
void ft_list_print(t_list *begin_list);
#endif

19
main.c
View File

@ -3,6 +3,10 @@
#include <stdio.h>
#include "libasm.h"
void do_nothing(void *data) {
(void)data;
}
int ft_strcmp(const char *s1, const char *s2) {
printf("s1: %ss2: %s", s1, s2);
while (*s1 && *s2 && *s1 == *s2) {
@ -13,6 +17,7 @@ int ft_strcmp(const char *s1, const char *s2) {
}
int main(void) {
/*
t_list *list;
list = NULL;
@ -86,13 +91,23 @@ int main(void) {
printf("\n");
ft_list_print(begin_ok);
printf("\n");
*/
t_list *c = NULL;
ft_list_push_front(&c, "b\n");
ft_list_push_front(&c, "c\n");
ft_list_push_front(&c, "a\n");
ft_list_push_front(&c, "c\n");
ft_list_push_front(&c, "a\n");
ft_list_push_front(&c, "b\n");
ft_list_push_front(&c, "c\n");
ft_list_push_front(&c, "c\n");
ft_list_push_front(&c, "b\n");
ft_list_push_front(&c, "a\n");
ft_list_push_front(&c, "b\n");
ft_list_print(c);
ft_list_sort(&c, *ft_strcmp);
// ft_list_sort(&c, *ft_strcmp);
ft_list_print(c);
ft_list_remove_if(&c, "b\n", &ft_strcmp, &do_nothing);
ft_list_print(c);
return 0;