add ft_list_sort

This commit is contained in:
2025-04-06 01:39:39 +02:00
parent 34b139c60b
commit 919948abe9
11 changed files with 163 additions and 86 deletions

View File

@ -16,7 +16,7 @@ get_char_index:
get_char_index_loop:
cmp dil, [rsi] ;is char invalid
je get_char_index_found
jz get_char_index_found
mov al, [rsi] ; is end of string
test al, al
@ -40,7 +40,7 @@ check_duplicate_char:
mov al ,[rdi] ; test if \0
test al, al
je no_duplicate
jz no_duplicate
push rdi
mov rsi, rdi
@ -51,7 +51,7 @@ check_duplicate_char:
cmp rax, -1 ;is char found
lea rdi, [rdi + 1]
je check_duplicate_char
jz check_duplicate_char
mov rax, 1
ret
@ -78,7 +78,7 @@ chk_bs_duplicate:
call check_duplicate_char
pop rdi
test rax, rax
je chk_bs_char
jz chk_bs_char
xor rax, rax
ret
@ -87,7 +87,7 @@ chk_bs_duplicate:
chk_bs_char:
mov al, [rdi] ; if rdi is \0, return 0
test al, al
je base_ok
jz base_ok
push rdi ; if current char is in invalid_chars
mov dil, [rdi];
@ -97,7 +97,7 @@ chk_bs_char:
cmp rax, -1 ; if not -1, error
lea rdi, [rdi + 1]
je chk_bs_char
jz chk_bs_char
xor rax, rax; return 0
ret
@ -113,9 +113,9 @@ base_ok:
ft_atoi_base:
xor rax, rax ;check if there is a null pointer in rdi or rsi
test rdi, rdi
je final_number
jz final_number
test rsi, rsi
je final_number
jz final_number
push rdi ; is base valid
push rsi
@ -155,10 +155,10 @@ plus_minus_inc:
plus_minus:
mov al, [rdi]
cmp al, '-'
je plus_minus_invert
jz plus_minus_invert
cmp al, '+'
je plus_minus_inc
jz plus_minus_inc
@ -178,7 +178,7 @@ plus_minus:
number_loop:
mov dl, [rdi]
test dl, dl
je final_number
jz final_number
push rax
push rdi
@ -192,7 +192,7 @@ number_loop:
pop rax
cmp rdx, -1
je final_number
jz final_number
push rdx ; imul sets rdx to 0
imul rbx

View File

@ -5,7 +5,7 @@ section .text
ft_list_push_front:
test rdi, rdi
je err
jz err
push rdi
push rsi
@ -15,7 +15,7 @@ ft_list_push_front:
pop rdi
test rax, rax ; did malloc return 0
je err
jz err
push rdi
mov rdi, [rdi]

View File

@ -7,7 +7,7 @@ ft_list_size:
count_start:
test rdi, rdi
je end
jz end
mov rdi, [rdi + 8]

84
srcs/bonus/ft_list_sort.s Normal file
View File

@ -0,0 +1,84 @@
global ft_list_sort
extern ft_list_size
section .text
; function that swaps 2 list elements
; takes 2 args :
; - rdi : element 1
; - rsi : element 2
list_swap:
mov rdx, [rdi]
mov rax, [rsi]
mov [rsi], rdx
mov [rdi], rax
ret
ft_list_sort:
test rdi, rdi
jz err
test rsi, rsi
jz err
mov rdi, [rdi]
push rdi
call ft_list_size
pop rdi
loop:
test rax, rax
jz err
push rax
push rdi
push rsi
mov rax, rsi ; move cmp to rax
jmp loop2
loop2_inc:
mov rdi, [rdi + 8]
loop2:
test rdi, rdi
jz loop2_end
mov rsi, [rdi + 8]
test rsi, rsi
jz loop2_end
push rdi
mov rdi, [rdi]
mov rsi, [rsi]
; first call cmp in rax
push rax
call rax
cmp eax, 0
pop rax
pop rdi
jle loop2_inc
; then call list_swap
mov rsi, [rdi + 8]
push rax
call list_swap
pop rax
jmp loop2_inc
loop2_end:
pop rsi
pop rdi
pop rax
lea rax, [rax - 1]
jmp loop
err:
ret

View File

@ -5,20 +5,20 @@ ft_strcmp:
xor rax, rax
test rdi, rdi ; test if args are false
je err
jz err
test rsi, rsi
je err
jz err
loop_start:
mov al, [rdi]
cmp al, [rsi]
jne compare
test al, al ; test if rdi is \0
je compare
jz compare
mov al, [rsi] ; test if rsi is \0
test al, al
je compare
jz compare
lea rsi, [rsi + 1]
lea rdi, [rdi + 1]

View File

@ -3,15 +3,15 @@ section .text
ft_strcpy:
test rdi, rdi
je err
jz err
test rsi, rsi
je err
jz err
push rdi
jmp loop_start
condition_check:
mov al, [rdi]
test al, al
je end
jz end
loop_start:
mov rax, [rsi]

View File

@ -7,7 +7,7 @@ section .text
ft_strdup:
test rdi, rdi ; test if arg is NULL
je err
jz err
push rdi ;push to use it for the strcpy
call ft_strlen
@ -15,7 +15,7 @@ ft_strdup:
call malloc wrt ..plt
test rax, rax
je malloc_failed
jz malloc_failed
mov rdi, rax
pop rsi
call ft_strcpy

View File

@ -3,7 +3,7 @@ section .text
ft_strlen:
test rdi, rdi
je err
jz err
mov rsi, rdi
jmp loop_start