separate declarations in libasm.h file and add list_push_front and list_size

This commit is contained in:
2025-04-03 04:55:28 +02:00
parent 394b016f2f
commit 34b139c60b
5 changed files with 126 additions and 10 deletions

View File

@ -11,7 +11,9 @@ SRCS_NAMES = ft_strlen.s\
ft_read.s\ ft_read.s\
ft_strdup.s ft_strdup.s
SRCS_BONUS_NAMES = ft_atoi_base.s SRCS_BONUS_NAMES = ft_atoi_base.s\
ft_list_push_front.s\
ft_list_size.s
SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES)) SRCS = $(addprefix $(SRCS_DIR)/, $(SRCS_NAMES))
SRCS_BONUS = $(addprefix $(SRCS_BONUS_DIR)/, $(SRCS_NAMES)) SRCS_BONUS = $(addprefix $(SRCS_BONUS_DIR)/, $(SRCS_NAMES))
@ -47,5 +49,6 @@ clean:
fclean: clean fclean: clean
rm -f $(NAME) test rm -f $(NAME) test
rm -f libasm_bonus.a
.PHONY: fclean clean all .PHONY: fclean clean all

22
libasm.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef LIBASM_H
# define LIBASM_H
#include <stdlib.h>
typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;
size_t ft_strlen(char *str);
char *ft_strcpy(char *dest, char *src);
int ft_strcmp(char *dest, char *src);
ssize_t ft_write(int fd, const void *buffer, size_t count);
ssize_t ft_read(int fd, void *buffer, size_t count);
char *ft_strdup(char *src);
int ft_atoi_base(char *nbr, char *base);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *list_start);
#endif

View File

@ -0,0 +1,29 @@
global ft_list_push_front
extern malloc
section .text
ft_list_push_front:
test rdi, rdi
je err
push rdi
push rsi
mov rdi, 10
call malloc wrt ..plt
pop rsi
pop rdi
test rax, rax ; did malloc return 0
je err
push rdi
mov rdi, [rdi]
mov [rax + 8], rdi ;set both settings
mov [rax], rsi
pop rdi
mov [rdi], rax
err:
ret

19
srcs/bonus/ft_list_size.s Normal file
View File

@ -0,0 +1,19 @@
global ft_list_size
section .text
ft_list_size:
xor rax, rax
count_start:
test rdi, rdi
je end
mov rdi, [rdi + 8]
lea rax, [rax + 1]
jmp count_start
end:
ret

61
test.c
View File

@ -6,16 +6,16 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */ /* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
/* Updated: 2025/04/02 02:35:57 by tomoron ### ########.fr */ /* Updated: 2025/04/03 04:52:46 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "libasm.h"
#define OK "\033[32mOK\n\033[0m" #define OK "\033[32mOK\n\033[0m"
#define KO "\033[31mKO\n\033[0m" #define KO "\033[31mKO\n\033[0m"
@ -38,13 +38,6 @@
else \ else \
write(1, KO, strlen(KO));\ write(1, KO, strlen(KO));\
size_t ft_strlen(char *str);
char *ft_strcpy(char *dest, char *src);
int ft_strcmp(char *dest, char *src);
ssize_t ft_write(int fd, const void *buffer, size_t count);
ssize_t ft_read(int fd, void *buffer, size_t count);
char *ft_strdup(char *src);
int ft_atoi_base(char *nbr, char *base);
int test_strlen(void) int test_strlen(void)
{ {
@ -212,6 +205,52 @@ int test_atoi_base(void)
return(nb_tests == passed); return(nb_tests == passed);
} }
int test_list_push_front()
{
int nb_tests;
int passed;
t_list *tmp;
nb_tests = 4;
passed = 0;
tmp = (void *)0x321;
ft_list_push_front(&tmp, (void *)0x123);
TEST("does it have a different address", tmp != (void *)0x321);
TEST("value is in the right field", tmp->data == (void *)0x123);
TEST("is the next at prev", tmp->next == (void *)0x321);
free(tmp);
ft_list_push_front(0, 0);
TEST("call with null pointer (should not segfault)", 1);
return(nb_tests == passed);
}
int test_list_size()
{
int nb_tests;
int passed;
t_list *tmp;
nb_tests = 2;
passed = 0;
tmp = 0;
ft_list_push_front(&tmp, (void *)0x123);
ft_list_push_front(&tmp, (void *)0x123);
ft_list_push_front(&tmp, (void *)0x123);
TEST("null pointer (empty list)", ft_list_size(0) == 0);
TEST("list of 3", ft_list_size(tmp) == 3);
free(tmp->next->next);
free(tmp->next);
free(tmp);
return(nb_tests == passed);
}
int main(void) int main(void)
{ {
FNC_TEST("ft_strlen", test_strlen()); FNC_TEST("ft_strlen", test_strlen());
@ -227,4 +266,8 @@ int main(void)
FNC_TEST("ft_strdup", test_strdup()); FNC_TEST("ft_strdup", test_strdup());
printf("\n\n"); printf("\n\n");
FNC_TEST("atoi_base", test_atoi_base()); FNC_TEST("atoi_base", test_atoi_base());
printf("\n\n");
FNC_TEST("list_push_front", test_list_push_front());
printf("\n\n");
FNC_TEST("list_size", test_list_size());
} }