feat: ft_list_sort

This commit is contained in:
lohhiiccc 2026-06-22 13:33:52 +02:00
parent bc61696ccb
commit 04a9b24499
3 changed files with 142 additions and 9 deletions

View file

@ -12,15 +12,17 @@ SHARED_LIB=lib$(LIB_NAME).so
SRCS=$(SRC_DIR)/ft_strlen.asm \ SRCS=$(SRC_DIR)/ft_strlen.asm \
$(SRC_DIR)/ft_strcpy.asm \ $(SRC_DIR)/ft_strcpy.asm \
$(SRC_DIR)/ft_strcmp.asm \ $(SRC_DIR)/ft_strcmp.asm \
$(SRC_DIR)/ft_write.asm \ $(SRC_DIR)/ft_write.asm \
$(SRC_DIR)/ft_read.asm \ $(SRC_DIR)/ft_read.asm \
$(SRC_DIR)/ft_strdup.asm \ $(SRC_DIR)/ft_strdup.asm \
$(SRC_DIR)/ft_memcpy.asm \ $(SRC_DIR)/ft_memcpy.asm \
$(SRC_DIR)/ft_isspace.asm \ $(SRC_DIR)/ft_isspace.asm \
$(SRC_DIR)/ft_bzero.asm \ $(SRC_DIR)/ft_bzero.asm \
$(SRC_DIR)/ft_atoibase.asm \ $(SRC_DIR)/ft_atoibase.asm \
$(SRC_DIR)/ft_list_push_front.asm \ $(SRC_DIR)/ft_list_push_front.asm \
$(SRC_DIR)/ft_list_size.asm \
$(SRC_DIR)/ft_list_sort.asm
OBJS=$(patsubst $(SRC_DIR)/%.asm,$(OBJ_DIR)/%.o,$(SRCS)) OBJS=$(patsubst $(SRC_DIR)/%.asm,$(OBJ_DIR)/%.o,$(SRCS))
DEPS=$(OBJS:.o=.d) DEPS=$(OBJS:.o=.d)

130
src/ft_list_sort.asm Normal file
View file

@ -0,0 +1,130 @@
global ft_list_sort
struc list
.data resq 1
.next resq 1
endstruc
section .text
;void ft_list_sort(t_list **begin_list, int (*cmp)())
;{
; t_list *cur;
; int swapped;
; void swap;
;
; if (NULL == begin_list || NULL == *begin_list || NULL == (*begin_list)->next)
; return;
;
; do
; {
; swapped = 0;
; cur = *begin_list;
; while (cur->next)
; {
; if (cmp(cur->data, cur->next->data) > 0)
; {
; swap = cur->data;
; cur->data = cur->next->data;
; cur->next->data = swap;
; swapped = 1;
; }
; cur = cur->next;
; }
; }
; while (swapped);
;}
;
; rdi: t_list **begin_list
; rsi: int (*cmp)()
ft_list_sort:
; NULL == cmp
test rsi, rsi
jz .ret
; NULL == begin_list
test rdi, rdi
jz .ret
; NULL == *begin_list
mov rax, [rdi]
test rax, rax
jz .ret
; NULL == (*begin_list)->next
mov rdx, [rax + list.next]
test rdx, rdx
jz .ret
.L1:
;swapper = 0
xor rcx, rcx
; cur = *begin_list
mov rax, [rdi]
; rax should be cur
; while (cur->next)
.L2:
; cmp 2nd param (part 1)
; also used for while (cur->next)
mov rdx, [rax + list.next]
test rdx, rdx
je .L2_end
push rax
push rdx
push rdi
push rsi
push rcx
; cmp 1st param
mov rdi, [rax + list.data]
; save cmp in rax B4 overwriting rsi
mov rax, rsi
; cmp 2nd param (part2)
mov rsi, [rdx + list.data]
call rax
mov r8, rax
pop rcx
pop rsi
pop rdi
pop rdx
pop rax
test r8, r8
jg .swap
; cur = cur->next;
mov rax, [rax + list.next]
jmp .L2
.L2_end:
; } while swapped
test rcx, rcx
jnz .L1
.ret:
ret
.swap:
; rax = cur
mov rdx, [rax + list.next]
; swap cur->data <-> cur->next->data
mov r8, [rax + list.data]
mov r9, [rdx + list.data]
mov [rax + list.data], r9
mov [rdx + list.data], r8
; swapped = 1;
mov rcx, 1
mov rax, rdx
jmp .L2

View file

@ -23,6 +23,7 @@ void ft_bzero(void *s, size_t n);
int ft_atoibase(char *str, char *base); int ft_atoibase(char *str, char *base);
void ft_list_push_front(t_list **begin_list, void *data); void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *begin_list); int ft_list_size(t_list *begin_list);
void ft_list_sort(t_list **begin_list, int (*cmp)(void *, void *));
#ifdef __cplusplus #ifdef __cplusplus
} }