Skip to content

Commit

Permalink
feat: implement create_elem, list_push_front, list_size, list_remove_…
Browse files Browse the repository at this point in the history
…if (#5)

* feat: implement create_elem, list_push_front, list_size, list_remove_if

* style: format

* fix: add valgrind to github action dependencies
  • Loading branch information
tdameros authored Jul 24, 2024
1 parent df83cc5 commit c63751e
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ on:
branches: [ "main" ]

jobs:
build:
tests:

runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y nasm clang-format
run: sudo apt-get update && sudo apt-get install -y nasm clang-format valgrind

- name: Check format
run: make check-format
Expand Down
35 changes: 22 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ TEST_NAME = tests_bin
# --------------- FILES --------------- #

LIST_ASM_SRC = \
ft_read.s \
ft_strcmp.s \
ft_strcpy.s \
ft_strdup.s \
ft_strlen.s \
ft_create_elem.s \
ft_list.s \
ft_list_push_front.s \
ft_list_remove_if.s \
ft_list_size.s \
ft_read.s \
ft_strcmp.s \
ft_strcpy.s \
ft_strdup.s \
ft_strlen.s \
ft_write.s

LIST_TEST_SRC = \
ft_read.c \
ft_strcmp.c \
ft_strcpy.c \
ft_strdup.c \
ft_strlen.c \
ft_write.c \
ft_create_elem.c \
ft_list_push_front.c \
ft_list_remove_if.c \
ft_list_size.c \
ft_read.c \
ft_strcmp.c \
ft_strcpy.c \
ft_strdup.c \
ft_strlen.c \
ft_write.c \
main.c

# ------------ DIRECTORIES ------------ #
Expand All @@ -44,7 +53,7 @@ UTEST_INCLUDE = $(DIR_UTEST)
# ------------ COMPILATION ------------ #

AS = nasm
ASFLAGS = -f elf64
ASFLAGS = -f elf64 -I $(DIR_SRC)

CFLAGS = -Wall -Wextra -Werror

Expand All @@ -64,7 +73,7 @@ all: $(NAME)

.PHONY: tests
tests: $(TEST_NAME)
./$(TEST_NAME)
valgrind ./$(TEST_NAME)

$(TEST_NAME): $(TEST_OBJ) $(NAME)
$(CC) $(CFLAGS) $(TEST_OBJ) -L. -l:$(NAME) -o $(TEST_NAME)
Expand Down
12 changes: 12 additions & 0 deletions include/libasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@

# include <sys/types.h>

typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;


ssize_t ft_read(int fd, void *buf, size_t count);
int ft_strcmp(const char *s1, const char *s2);
char *ft_strcpy(char *dst, const char *src);
char *ft_strdup(const char *s1);
size_t ft_strlen(const char *str);
ssize_t ft_write(int fd, const void *buf, size_t count);

t_list *ft_create_elem(void *data);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *begin_list);
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *));

#endif
30 changes: 30 additions & 0 deletions src/ft_create_elem.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
bits 64

extern malloc

%include "ft_list.s"

section .note.GNU-stack

section .text
global ft_create_elem

;typedef struct s_list
;{
; void *data;
; struct s_list *next;
;} t_list;

; t_list *ft_create_elem(void *data);
ft_create_elem:
push rdi
mov rdi, LIST_SIZE
call malloc wrt ..plt
cmp rax, 0
jz return
pop rdi
mov [rax + LIST_DATA_OFFSET], rdi
mov qword [rax + LIST_NEXT_OFFSET], 0
return
return:
ret
16 changes: 16 additions & 0 deletions src/ft_list.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bits 64

section .note.GNU-stack

;typedef struct s_list
;{
; void *data;
; struct s_list *next;
;} t_list;
section .data
LIST_DATA_OFFSET EQU 0
LIST_NEXT_OFFSET EQU 8
LIST_SIZE EQU 16



35 changes: 35 additions & 0 deletions src/ft_list_push_front.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
bits 64

extern malloc
extern ft_create_elem

%include "ft_list.s"


section .note.GNU-stack

section .text
global ft_list_push_front

;typedef struct s_list
;{
; void *data;
; struct s_list *next;
;} t_list;

;void ft_list_push_front(t_list **begin_list, void *data);
ft_list_push_front:
test rdi, rdi
jz .return
push rdi
mov rdi, rsi
call ft_create_elem
cmp rax, 0
jz .return
pop rdi
mov rsi, [rdi]
mov qword [rax + LIST_NEXT_OFFSET], rsi
mov qword [rdi], rax
.return
.return:
ret
177 changes: 177 additions & 0 deletions src/ft_list_remove_if.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
bits 64

%include "ft_list.s"

extern free

section .note.GNU-stack

section .text
global ft_list_remove_if

;typedef struct s_list
;{
; void *data;
; struct s_list *next;
;} t_list;

;void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(),
; void (*free_fct)(void *)) {
; if (begin_list == NULL || cmp == NULL) {
; return;
; }
;
; t_list *current = *begin_list;
; t_list *previous = NULL;
; while (current) {
; if (cmp(current->data, data_ref) == 0) {
; if (previous) {
; previous->next = current->next;
; } else {
; *begin_list = current->next;
; }
; if (free_fct) {
; free_fct(current->data);
; }
; free(current);
; current = previous ? previous->next : *begin_list;
; } else {
; previous = current;
; current = current->next;
; }
; }

;void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void *));
ft_list_remove_if:
; begin_list
test rdi, rdi
jz end
; cmp
test rdx, rdx
jz end
; save r8 and r9
push r8
push r9
; current_node
mov r8, [rdi]
; previous_node
xor r9, r9
jmp loop
loop:
test r8, r8
jz end_loop
jmp cmp_function

cmp_function:
push rdi
push rsi
push rdx
push rcx
push r8
push r9
mov rdi, [r8 + LIST_DATA_OFFSET]
; call cmp_fct
call rdx
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
test rax, rax
jz remove_node
jnz next_node

remove_node:
; if previous != NULL
test r9, r9
jnz link_previous_to_next
jmp link_to_next

link_previous_to_next:
mov rax, [r8 + LIST_NEXT_OFFSET]
mov [r9 + LIST_NEXT_OFFSET], rax
jmp free_node

link_to_next:
mov rax, [r8 + LIST_NEXT_OFFSET]
mov [rdi], rax
jmp free_node

free_node:
; if free_fct != NULL
test rcx, rcx
jnz call_free_fct
push rdi
push rsi
push rdx
push rcx
push r8
push r9
mov rdi, r8
call free wrt ..plt
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
jmp update_current_after_free

call_free_fct:
push rdi
push rsi
push rdx
push rcx
push r8
push r9
mov rdi, [r8 + LIST_DATA_OFFSET]
call rcx
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi

push rdi
push rsi
push rdx
push rcx
push r8
push r9
mov rdi, r8
;call rcx
call free wrt ..plt
pop r9
pop r8
pop rcx
pop rdx
pop rsi
pop rdi
jmp update_current_after_free

update_current_after_free:
; if previous != NULL
test r9, r9
jnz link_current_to_previous_next
mov r8, [rdi]
jmp loop

link_current_to_previous_next:
mov r8, [r9 + LIST_NEXT_OFFSET]
jmp loop

next_node:
mov r9, r8
mov r8, [r8 + LIST_NEXT_OFFSET]
jmp loop

end_loop:
; restore r8 and r9
pop r9
pop r8
ret

end:
ret
27 changes: 27 additions & 0 deletions src/ft_list_size.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
bits 64

%include "ft_list.s"

section .note.GNU-stack

section .text
global ft_list_size

;typedef struct s_list
;{
; void *data;
; struct s_list *next;
;} t_list;

;int ft_list_size(t_list *begin_list);
ft_list_size:
xor rax, rax
jmp loop
loop:
cmp rdi, 0
je end
mov rdi, [rdi + LIST_NEXT_OFFSET]
inc rax
jmp loop
end:
ret
Loading

0 comments on commit c63751e

Please sign in to comment.