-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_list_sort.s
50 lines (42 loc) · 1.26 KB
/
ft_list_sort.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
global ft_list_sort
section .text
ft_list_sort: ; rdi = t_list **begin, rsi = int (*cmp)()
enter 0, 0
mov rdx, QWORD [rdi] ; rdx = t_list *begin, rcx = t_list *current
cmp rsi, 0
jz ft_list_sort_ret
ft_list_sort_loop_begin:
cmp rdx, 0 ; if begin == NULL
jz ft_list_sort_ret ; return
mov rcx, [rdx+8] ; current = begin->next
ft_list_sort_loop_current:
cmp rcx, 0x0 ; if current == NULL
jz ft_list_sort_loop_begin_end ; break
push rdx
push rcx
push rdi
push rsi
mov rdi, [rdx]
mov rsi, [rcx]
call [rsp] ; call int (*cmp)() => rdi = begin->data, rsi = current->data
pop rsi
pop rdi
pop rcx
pop rdx
cmp rax, 0 ; if return of int (*cmp)() > 0
jg ft_list_sort_swap ; jump handle sort
ft_list_sort_loop_current_end:
mov rcx, [rcx+8] ; current = current->next
jmp ft_list_sort_loop_current
ft_list_sort_swap:
mov r8, QWORD [rdx]
mov r9, QWORD [rcx]
mov [rdx], r9
mov [rcx], r8
jmp ft_list_sort_loop_current_end
ft_list_sort_loop_begin_end:
mov rdx, [rdx+8] ; begin = begin->next
jmp ft_list_sort_loop_begin
ft_list_sort_ret:
leave
ret