aboutsummaryrefslogtreecommitdiff
path: root/ft_list_remove_if.s
diff options
context:
space:
mode:
Diffstat (limited to 'ft_list_remove_if.s')
-rw-r--r--ft_list_remove_if.s88
1 files changed, 62 insertions, 26 deletions
diff --git a/ft_list_remove_if.s b/ft_list_remove_if.s
index 9ae6ba5..40e2415 100644
--- a/ft_list_remove_if.s
+++ b/ft_list_remove_if.s
@@ -10,49 +10,85 @@
; ;
; **************************************************************************** ;
-global _ft_list_remove_if
+global ft_list_remove_if
+
+extern _free
+
+%define NULL 0x0
+
+%macro EXTERN_FUNCTION_SAVE 0
+ push rdi
+ push rsi
+ push rdx
+ push rcx
+%endmacro
+
+%macro EXTERN_FUNCTION_SAVE_END 0
+ pop rcx
+ pop rdx
+ pop rsi
+ pop rdi
+%endmacro
; ft_list_remove_if(t_list **begin_list, void *data_ref,
-; int (*cmp)(), void (*free_fct)(void *))
+; int (*cmp)(void *data, void *data_ref),
+; void (*free_fct)(void *))
+ft_list_remove_if:
+ ; t_list *saved_next
-_ft_list_remove_if:
+ ; === prolog ===
+ push rbp
+ mov rbp, rsp
sub rsp, 8
- cmp rdi, 0x0
+ ; === base condition ===
+ cmp rdi, NULL
je FT_LIST_REMOVE_IF_END
- cmp [rdi], 0x0
+ cmp qword [rdi], NULL
je FT_LIST_REMOVE_IF_END
- push rdi
- mov rdi, [rdi] + 8 ;&(*begin_list)->val
- call rdx
- pop rdi
- cmp rax, 0x0
+ ; === compare (*begin_list)->data and data_ref
+ EXTERN_FUNCTION_SAVE
+ mov rdi, [rdi]
+ mov rdi, [rdi + 0]
+ call rdx ; cmp((*begin_list)->data, data_ref)
+ EXTERN_FUNCTION_SAVE_END
+ cmp rax, 0
je FT_LIST_REMOVE_IF_REMOVE
-
- mov rdi, [rdi] + 8
- call _ft_list_remove_if
- add rsp, 8
- ret
-
-FT_LIST_REMOVE_IF_REMOVE:
- mov [rsp], [rdi] + 8 ; saved_next = (*begin_list)->next
-
+ ; === next element ===
push rdi
- mov rdi, [[rdi]]
- call rcx ; free_fct((*begin_list)->data)
+ mov rdi, [rdi]
+ lea rdi, [rdi + 8]
+ call ft_list_remove_if
pop rdi
+ jmp FT_LIST_REMOVE_IF_END
- push rdi
+ ; === remove head and go next ===
+FT_LIST_REMOVE_IF_REMOVE:
+ mov rax, [rdi] ; rax = *begin_list
+ mov rax, [rax + 8] ; rax = rax->next
+ mov [rbp - 8], rax ; saved_next = (*begin_list)->next
+
+ push rdi ; strange behavior
+ EXTERN_FUNCTION_SAVE
mov rdi, [rdi]
- call _free ; free(*begin_list)
+ mov rdi, [rdi + 0]
+ call rcx ; free_fct((*begin_list)->data)
+ EXTERN_FUNCTION_SAVE_END
pop rdi
- mov [rdi], [rsp] ; *begin_list = saved_next
+ EXTERN_FUNCTION_SAVE
+ mov rdi, [rdi]
+ call _free ; free(*begin_list)
+ EXTERN_FUNCTION_SAVE_END
- call _ft_list_remove_if
+ mov rax, [rbp - 8]
+ mov [rdi], rax
+ call ft_list_remove_if
FT_LIST_REMOVE_IF_END:
- add rsp, 8
+ ; === epilog ===
+ mov rsp, rbp
+ pop rbp
ret