-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_list_remove_if_bonus.s
More file actions
77 lines (65 loc) · 2.08 KB
/
ft_list_remove_if_bonus.s
File metadata and controls
77 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
; **************************************************************************** ;
; ;
; ::: :::::::: ;
; ft_list_remove_if_bonus.s :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: lrocca <marvin@42.fr> +;+ +:+ +;+ ;
; +;+;+;+;+;+ +;+ ;
; Created: 2021/04/08 19:12:55 by lrocca ;+; ;+; ;
; Updated: 2021/04/08 19:12:56 by lrocca ;;; ;;;;;;;;.fr ;
; ;
; **************************************************************************** ;
section .text
global _ft_list_remove_if
extern _free
%macro PUSH_ALL 0
push rdi
push rsi
push rdx
push rcx
%endmacro
%macro POP_ALL 0
pop rcx
pop rdx
pop rsi
pop rdi
%endmacro
_ft_list_remove_if:
push rbp ; save base pointer
mov rbp, rsp ; copy stack pointer
sub rsp, 8 ; align stack
cmp rdi, 0 ; node == 0
jz return
cmp qword [rdi], 0 ; *node == 0
jz return
PUSH_ALL
mov rdi, [rdi] ; *node
mov rdi, [rdi + 0] ; (*node)->data
call rdx ; cmp((*node)->data, data_ref)
POP_ALL
cmp rax, 0 ; cmp returned 0, remove
jz remove
mov rdi, [rdi] ; *node
lea rdi, [rdi + 8] ; (*node)->next
call _ft_list_remove_if ; call recursively
jmp return
remove:
mov rax, [rdi] ; rax = *node
mov rax, [rax + 8] ; rax = rax->next
mov [rbp - 8], rax ; saved_next = (*node)->next
PUSH_ALL
mov rdi, [rdi] ; *node
mov rdi, [rdi + 0] ; (*node)->data
call rcx ; free_fct((*node)->data)
POP_ALL
PUSH_ALL
mov rdi, [rdi] ; *node
call _free ; free(*node)
POP_ALL
mov rax, [rbp - 8] ; get next node
mov [rdi], rax
call _ft_list_remove_if ; call recursively
return:
mov rsp, rbp ; restore stack pointer
pop rbp ; restore base pointer
ret