65 lines
1.2 KiB
ArmAsm
65 lines
1.2 KiB
ArmAsm
|
global ft_list_sort
|
||
|
|
||
|
ft_list_sort:
|
||
|
push rsp
|
||
|
push rdi ; push begin_list
|
||
|
push rsi ; push cmp_func
|
||
|
push r12 ; callee saved
|
||
|
push r13 ; same
|
||
|
push r14 ; same
|
||
|
mov r12, 0 ; r12 "prev" = NULL
|
||
|
mov r13, [rdi] ; r13 "curr" = begin_list
|
||
|
mov r14, [rdi] ; r14 begin_list memory
|
||
|
mov rcx, rsi ; rcx = cmp_func
|
||
|
|
||
|
.loop:
|
||
|
cmp qword [r13 + 8], 0 ; if curr->next is null then exit
|
||
|
je .done
|
||
|
mov rdi, [r13] ; first arg is curr->data
|
||
|
mov rsi, [r13 + 8] ; rsi = curr->next;
|
||
|
mov rsi, [rsi] ; rsi = rsi->data;
|
||
|
;push rbx ; rbx is caller saved
|
||
|
;push r13 ; same
|
||
|
; align stack here ?
|
||
|
sub rsp, 0; align stack ???
|
||
|
call rcx
|
||
|
add rsp, 0
|
||
|
;pop rdx ; rdx is caller saved
|
||
|
;pop rbx ; rbx is caller saved
|
||
|
cmp rax, 0
|
||
|
jle .else ; if in order continue...
|
||
|
cmp r13, r14
|
||
|
je .is_first_elem
|
||
|
mov rbx, [r12 + 8]
|
||
|
mov rbx, [r13 + 8]
|
||
|
mov rbx, [r13 + 8]
|
||
|
mov rbx, [rbx + 8]
|
||
|
mov [r13 + 8], rbx
|
||
|
mov rbx, [r12 + 8]
|
||
|
mov rbx, [rbx + 8]
|
||
|
mov rbx, r13
|
||
|
jmp .loop
|
||
|
|
||
|
.is_first_elem:
|
||
|
mov r14, [r13 + 8]
|
||
|
mov rbx, [r13 + 8]
|
||
|
mov rbx, [rbx + 8]
|
||
|
mov [r13 + 8], rbx
|
||
|
mov [r14 + 8], r13
|
||
|
jmp .loop
|
||
|
|
||
|
.else: ; ...here
|
||
|
mov r12, rdx ; prev = curr;
|
||
|
mov rdx, [rdx + 8] ; curr = curr->next;
|
||
|
jmp .loop
|
||
|
|
||
|
|
||
|
.done:
|
||
|
pop r14
|
||
|
pop r13 ; restore stack and return
|
||
|
pop r12
|
||
|
pop rsi
|
||
|
pop rdi
|
||
|
pop rsp
|
||
|
ret
|