-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_list_remove_if.s
87 lines (73 loc) · 1.63 KB
/
ft_list_remove_if.s
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
78
79
80
81
82
83
84
85
86
87
;rdi begin_list (t_list **)
;rsi data_ref (void *)
;rdx cmp (int (*)())
;rcx free_fct (void (*)(void *))
;rbx will have deref of **list, so *list
;r13 tmp for previous node
global ft_list_remove_if
extern free
section .text
ft_list_remove_if:
push rbx ;Preserved register: don't use it without saving it
cmp rdi, 0
je return
mov rbx, qword[rdi] ; loading the address t_list* into rbx
push r13
mov r13, rbx
loop:
cmp rbx, 0
je return
push rdi ; saving all args
push rsi
push rbx
push rcx
push rdx
mov rdi, qword[rbx] ; moving list->data to rdi
call rdx ; trying to call cmp function
pop rdx ; bringing back all args
pop rcx
pop rbx
pop rsi
pop rdi
cmp rax, 0 ; check result
je delete_data
mov r13, rbx ;saving curr node
mov rbx, qword[rbx + 8] ; moving to next node
jmp loop
return:
pop r13
pop rbx
ret
delete_data:
xor r8, r8
cmp [rdi], rbx ; check if current node is in head
setz r8b ; saving cmp result in r8
push rdi ; saving all args
push rsi
push rcx
push rdx
push r8
; ----------- free data
mov rdi, qword[rbx]
push rbx ; saving curr node
call rcx ; trying to call free_fct(); data is already in rdi
; ----------- free node
pop rbx
mov rdi, rbx ; saving current node in rdi, prepared to free
mov rbx, [rbx + 8] ; curr = curr->next
push rbx
call free wrt ..plt ; trying to free node
; -----------
pop rbx
pop r8
pop rdx
pop rcx
pop rsi
pop rdi
cmp r8b, 1
je head
mov [r13 + 8], rbx ; prev->next = curr->next
jmp loop
head:
mov [rdi], rbx
jmp loop