feat(bonus): size and push_front OK, sort WIP
This commit is contained in:
parent
774df72db3
commit
62e5d095b6
7
Makefile
7
Makefile
|
@ -7,6 +7,9 @@ SRC = \
|
|||
ft_write.s \
|
||||
ft_read.s \
|
||||
ft_strdup.s \
|
||||
ft_list_size.s \
|
||||
ft_list_push_front.s \
|
||||
ft_list_sort.s \
|
||||
|
||||
OBJ = $(SRC:.s=.o)
|
||||
|
||||
|
@ -15,11 +18,13 @@ OBJ = $(SRC:.s=.o)
|
|||
|
||||
all: $(NAME)
|
||||
|
||||
bonus: all
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
ar rcs libasm.a $(OBJ)
|
||||
|
||||
test: $(NAME)
|
||||
gcc -Wall -Werror -Wextra -no-pie -g main.c -L. -lasm -o libasm_unit_tests
|
||||
gcc -Wall -Werror -Wextra -no-pie -g ft_list_print.c main.c -L. -lasm -o libasm_unit_tests
|
||||
|
||||
clean:
|
||||
rm $(OBJ)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
#include "libasm.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void ft_list_print(t_list *begin_list) {
|
||||
while (begin_list) {
|
||||
printf("%s", (char *)begin_list->data);
|
||||
begin_list = begin_list->next;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
global ft_list_push_front
|
||||
extern malloc
|
||||
|
||||
ft_list_push_front:
|
||||
push rsp
|
||||
push rdi
|
||||
push rsi
|
||||
mov rdi, 16
|
||||
call malloc
|
||||
cmp rax, 0
|
||||
je .done
|
||||
pop rsi
|
||||
pop rdi
|
||||
mov [rax], rsi
|
||||
mov rbx, [rdi]
|
||||
mov [rax + 8], rbx
|
||||
mov [rdi], rax
|
||||
.done:
|
||||
pop rsp
|
||||
ret
|
||||
|
||||
;ft_list_push_front:
|
||||
; push ebp
|
||||
; mov ebp, esp
|
||||
; sub esp 0x10
|
||||
; push rdi
|
||||
; push rsi
|
||||
; mov rdi, 16
|
||||
; call malloc
|
||||
; pop rsi
|
||||
; pop rdi
|
||||
; ;cmp rax, 0
|
||||
; ;je .error
|
||||
;
|
||||
; mov rbx, [rdi]
|
||||
; mov rbx, [rbx + 8]
|
||||
; mov [rax], rsi
|
||||
; mov [rax + 8], rbx
|
||||
; mov [rdi], rax
|
||||
; leave
|
||||
; ret
|
|
@ -0,0 +1,12 @@
|
|||
global ft_list_size
|
||||
|
||||
ft_list_size:
|
||||
xor rax, rax
|
||||
.loop:
|
||||
cmp rdi, 0
|
||||
je .done
|
||||
mov rdi, [rdi + 8]
|
||||
inc rax
|
||||
jmp .loop
|
||||
.done:
|
||||
ret
|
|
@ -0,0 +1,64 @@
|
|||
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
|
11
libasm.h
11
libasm.h
|
@ -5,6 +5,11 @@
|
|||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct s_list {
|
||||
void *data;
|
||||
struct s_list *next;
|
||||
} t_list;
|
||||
|
||||
size_t ft_strlen(const char *);
|
||||
char *ft_strcpy(char *, const char *);
|
||||
int ft_strcmp(const char *, const char *);
|
||||
|
@ -12,4 +17,10 @@ ssize_t ft_write(int, const void *, size_t);
|
|||
ssize_t ft_read(int, void *, size_t);
|
||||
char *ft_strdup(const char *);
|
||||
|
||||
void ft_list_push_front(t_list **, void *);
|
||||
int ft_list_size(t_list *);
|
||||
void ft_list_sort(t_list **, int (*)());
|
||||
|
||||
void ft_list_print(t_list *begin_list);
|
||||
|
||||
#endif
|
||||
|
|
74
main.c
74
main.c
|
@ -1,53 +1,27 @@
|
|||
#include "libasm.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "libasm.h"
|
||||
|
||||
void my_test(void) {
|
||||
write(1, "Hello, world!\n", 14);
|
||||
printf("Hello, world!\n");
|
||||
}
|
||||
|
||||
void *my_malloc(size_t size) {
|
||||
printf("CALLLLLLED\n");
|
||||
fflush(stdout);
|
||||
printf("called with size %ld\n", size);
|
||||
fflush(stdout);
|
||||
printf("222323\n");
|
||||
fflush(stdout);
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
/* char *str1 = (char *)malloc(88 * sizeof(char));
|
||||
ft_strcpy(str1, "Salut moi une chaine de caractere trop longue\n");
|
||||
printf("%ld\n", ft_write(1, str1, 46));
|
||||
printf("%d\n", (int)str1[0]);
|
||||
free(str1);
|
||||
|
||||
char *str2 = (char *)malloc(88 * sizeof(char));
|
||||
strcpy(str2, "Salut moi une chaine de caractere trop longue\n");
|
||||
printf("%ld\n", write(1, str2, 46));
|
||||
printf("%d\n", (int)str2[0]);
|
||||
free(str2);
|
||||
|
||||
ssize_t write_ret = write(4, "test\n", 5);
|
||||
if (write_ret == -1) {
|
||||
fprintf(stderr, "error = %s\n", strerror(errno));
|
||||
perror("write");
|
||||
}
|
||||
errno = 42;
|
||||
write_ret = ft_write(4, "test\n", 5);
|
||||
printf("ft_write ret %ld\n", write_ret);
|
||||
if (write_ret == -1) {
|
||||
fprintf(stderr, "error = %d\n", errno);
|
||||
fprintf(stderr, "error = %s\n", strerror(errno));
|
||||
perror("ft_write");
|
||||
}*/
|
||||
char *str = ft_strdup("Salut c'est moi libasm\n");
|
||||
ft_write(1, "Hello, world!\n", 14);
|
||||
printf("len %ld\n", ft_strlen(str));
|
||||
ft_write(1, str, ft_strlen(str));
|
||||
return 0;
|
||||
int main(void) {
|
||||
t_list *list;
|
||||
|
||||
list = NULL;
|
||||
|
||||
ft_list_push_front(&list, "toto\n");
|
||||
ft_list_push_front(&list, "tutu\n");
|
||||
ft_list_push_front(&list, "zaza\n");
|
||||
ft_list_push_front(&list, "bobo\n");
|
||||
ft_list_push_front(&list, "tata\n");
|
||||
ft_list_push_front(&list, "babar\n");
|
||||
ft_list_push_front(&list, "zoro\n");
|
||||
|
||||
ft_list_print(list);
|
||||
|
||||
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_print(list);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue