-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_list_sort_bonus.s
More file actions
56 lines (49 loc) · 1.78 KB
/
ft_list_sort_bonus.s
File metadata and controls
56 lines (49 loc) · 1.78 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
; **************************************************************************** ;
; ;
; ::: :::::::: ;
; ft_list_sort_bonus.s :+: :+: :+: ;
; +:+ +:+ +:+ ;
; By: lrocca <marvin@42.fr> +;+ +:+ +;+ ;
; +;+;+;+;+;+ +;+ ;
; Created: 2021/04/08 19:12:12 by lrocca ;+; ;+; ;
; Updated: 2021/04/08 19:12:14 by lrocca ;;; ;;;;;;;;.fr ;
; ;
; **************************************************************************** ;
section .text
global _ft_list_sort
; void ft_list_sort(t_list **rdi, int (*rsi)());
_ft_list_sort:
push r12
push r13
push r15
cmp rdi, 0 ; 1st arg == 0, return
jz return
mov r12, [rdi] ; r12 = *head
mov r15, rsi ; r15 = cmp function
_loop:
mov r13, [r12 + 8] ; r13 = lst.next
cmp r13, 0 ; lst.next == 0
jz return ; lst is over
push rdi ; save lst head
mov rdi, [r12] ; 1st arg = lst.data
mov rsi, [r13] ; 2nd arg = lst.next.data
call r15 ; call compare function
pop rdi ; restore lst head
cmp rax, 0 ; check return of cmp
jg swap ; ret > 0, then swap
jmp next ; else loop
swap:
mov r10, [r12] ; r10 = lst.data
mov r11, [r13] ; r11 = lst.next.data
mov [r12], r11 ; lst.data = r11
mov [r13], r10 ; lst.next.data = r10
mov r12, [rdi] ; reset lst head
jmp _loop
next:
mov r12, r13 ; lst = lst.next
jmp _loop
return:
pop r15
pop r13
pop r12
ret