-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement create_elem, list_push_front, list_size, list_remove_…
…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
Showing
12 changed files
with
643 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.