Compare commits

...

2 Commits

Author SHA1 Message Date
gbrochar 30ee274862 feat(list_sort): dirty 2024-03-10 06:20:40 +01:00
gbrochar 83d43c88fc fix(strcmp): set rax and rbx to 0 before cmp 2024-03-10 06:20:10 +01:00
3 changed files with 130 additions and 24 deletions

View File

@ -7,10 +7,13 @@ ft_list_sort:
push r12 ; callee saved
push r13 ; same
push r14 ; same
push r15
cmp qword [rdi], 0
je .done
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
mov r14, rdi ; r14 begin_list memory
mov r15, rsi
.loop:
cmp qword [r13 + 8], 0 ; if curr->next is null then exit
@ -21,40 +24,69 @@ ft_list_sort:
;push rbx ; rbx is caller saved
;push r13 ; same
; align stack here ?
sub rsp, 0; align stack ???
call rcx
add rsp, 0
sub rsp, 72; align stack ???
call r15
add rsp, 72
;pop rdx ; rdx is caller saved
;pop rbx ; rbx is caller saved
cmp rax, 0
cmp eax, 0
jle .else ; if in order continue...
cmp r13, r14
cmp r13, [r14]
je .is_first_elem
mov rbx, [r12 + 8]
mov rdx, [r13 + 8]
mov [r12 + 8], rdx
mov rbx, [r13 + 8]
mov rbx, [r13 + 8]
mov rbx, [rbx + 8]
mov [r13 + 8], rbx
mov rdx, [rbx + 8]
mov [r13 + 8], rdx
mov rbx, [r12 + 8]
mov rbx, [rbx + 8]
mov rbx, r13
mov [rbx + 8], r13
mov r12, 0
mov r13, [r14]
;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
;mov r12, 0, ; reset prev
;mov r13, [r14] ; reset curr
jmp .loop
.is_first_elem:
mov r14, [r13 + 8]
;mov r14, [r13 + 8] ; begin_list = curr->next
;mov rbx, [r13 + 8] ; rbx = curr->next
;mov rdx, [rbx + 8] ; rbx = curr->next->next
;mov [r13 + 8], rdx ; curr->next = curr->next->next
;mov [r14 + 8], r13 ; begin_list->next = curr
;mov rdx, [r13 + 8]
mov rbx, [r13 + 8]
mov rbx, [rbx + 8]
mov [r13 + 8], rbx
mov [r14 + 8], r13
mov [r14], rbx
;je .done
mov rbx, [r13 + 8]
mov rdx, [rbx + 8]
mov [r13 + 8], rdx
mov rbx, [r14]
mov [rbx + 8], r13
;jmp .done
mov r12, 0 ; reset prev
mov r13, [r14] ; reset curr
jmp .loop
.else: ; ...here
mov r12, rdx ; prev = curr;
mov rdx, [rdx + 8] ; curr = curr->next;
mov r12, r13 ; prev = curr;
mov r13, [r13 + 8] ; curr = curr->next;
jmp .loop
.done:
pop r15
pop r14
pop r13 ; restore stack and return
pop r12

View File

@ -1,19 +1,20 @@
global ft_strcmp
ft_strcmp:
xor rdx, rdx
xor rdx, rdx
xor rax, rax
.loop:
mov al, [rsi + rdx]
cmp byte [rdi + rdx], al
cmp byte [rdi + rdx], al
jne .done
cmp byte [rdi + rdx], 0
je .done
inc rdx
jmp .loop
.done:
xor rax, rax
xor rbx, rbx
mov al, byte [rdi + rdx]
mov bl, byte [rsi + rdx]
sub rax, rbx
ret

77
main.c
View File

@ -3,6 +3,15 @@
#include <stdio.h>
#include "libasm.h"
int ft_strcmp(const char *s1, const char *s2) {
printf("s1: %ss2: %s", s1, s2);
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
int main(void) {
t_list *list;
@ -21,7 +30,71 @@ int main(void) {
printf("%lu\n", sizeof(t_list));
printf("%d\n", ft_list_size(list));
ft_list_sort(&list, &strcmp);
printf("On est sorti.e.s de cet enfer\n");
ft_list_sort(&list, &ft_strcmp);
ft_list_print(list);
t_list *a;
a = NULL;
ft_list_push_front(&a, "d\n");
ft_list_push_front(&a, "c\n");
ft_list_push_front(&a, "b\n");
ft_list_push_front(&a, "a\n");
ft_list_print(a);
ft_list_sort(&a, &ft_strcmp);
printf("On est sorti.e.s de cet enfer\n");
ft_list_print(a);
t_list *b;
b = NULL;
ft_list_push_front(&b, "ziziron\n");
ft_list_push_front(&b, "abalon\n");
ft_list_push_front(&b, "zebulon\n");
ft_list_print(b);
ft_list_sort(&b, &ft_strcmp);
ft_list_print(b);
t_list *null = NULL;
ft_list_sort(&null, &ft_strcmp);
t_list *begin_ok = NULL;
ft_list_push_front(&begin_ok, "c");
ft_list_push_front(&begin_ok, "f");
ft_list_push_front(&begin_ok, "b");
ft_list_push_front(&begin_ok, "h");
ft_list_push_front(&begin_ok, "e");
ft_list_push_front(&begin_ok, "l");
ft_list_push_front(&begin_ok, "d");
ft_list_push_front(&begin_ok, "i");
ft_list_push_front(&begin_ok, "j");
ft_list_push_front(&begin_ok, "k");
ft_list_push_front(&begin_ok, "g");
ft_list_push_front(&begin_ok, "a");
ft_list_print(begin_ok);
printf("\n");
ft_list_sort(&begin_ok, &ft_strcmp);
printf("\n");
printf("\n");
ft_list_print(begin_ok);
printf("\n");
t_list *c = NULL;
ft_list_push_front(&c, "b\n");
ft_list_push_front(&c, "c\n");
ft_list_push_front(&c, "a\n");
ft_list_print(c);
ft_list_sort(&c, *ft_strcmp);
ft_list_print(c);
return 0;
}