feat: first part done

This commit is contained in:
gbrochar 2024-02-26 05:47:24 +01:00
parent d05135501f
commit 0b3382b478
8 changed files with 124 additions and 23 deletions

View File

@ -4,6 +4,9 @@ SRC = \
ft_strlen.s \
ft_strcpy.s \
ft_strcmp.s \
ft_write.s \
ft_read.s \
ft_strdup.s \
OBJ = $(SRC:.s=.o)
@ -16,7 +19,7 @@ $(NAME): $(OBJ)
ar rcs libasm.a $(OBJ)
test: $(NAME)
gcc -Wall -Werror -Wextra -g main.c -L. -lasm -o libasm_unit_tests
gcc -Wall -Werror -Wextra -no-pie -g main.c -lc -L. -lasm -o libasm_unit_tests
clean:
rm $(OBJ)

21
ft_read.s Normal file
View File

@ -0,0 +1,21 @@
extern __errno_location
section .text
global ft_read
ft_read:
mov rax, 0 ; syscall read, other arguments should be good accord to calling convention
syscall
cmp eax, -1 ; linux syscalls errors are always in the range -4095 to -1
jg .done
cmp eax, -4095
jl .done
mov rbx, rax ; save return value of syscall
call __errno_location ; put address of errno in rax
neg rbx ; errno should be positive
mov [rax], rbx ; set errno
mov rax, -1 ; set return value of ft_read to -1
ret
.done:
ret

View File

@ -12,8 +12,8 @@ ft_strcmp:
inc rdx
jmp .loop
.done:
movsx rax, byte [rdi + rdx]
movsx rbx, byte [rsi + rdx]
mov al, byte [rdi + rdx]
mov bl, byte [rsi + rdx]
sub rax, rbx
ret

View File

@ -10,7 +10,6 @@ ft_strcpy:
inc rax
jmp .loop
.done:
inc rax
mov byte [rdi + rax], 0
mov rax, rdi
ret

25
ft_strdup.s Normal file
View File

@ -0,0 +1,25 @@
global ft_strdup
extern my_test
extern malloc
extern ft_strlen
extern ft_strcpy
ft_strdup:
enter 16, 0
push rdi
enter 16, 0
call ft_strlen
mov rdi, rax
inc rdi
call malloc
cmp rax, 0
jz .done
leave
mov rdi, rax
pop rsi
call ft_strcpy
.done:
leave
ret

21
ft_write.s Normal file
View File

@ -0,0 +1,21 @@
extern __errno_location
section .text
global ft_write
ft_write:
mov rax, 1 ; syscall write, other arguments should be good accord to calling convention
syscall
cmp eax, -1 ; linux syscalls errors are always in the range -4095 to -1
jg .done
cmp eax, -4095
jl .done
mov rbx, rax ; save return value of syscall
call __errno_location ; put address of errno in rax
neg rbx ; errno should be positive
mov [rax], rbx ; set errno
mov rax, -1 ; set return value of ft_write to -1
ret
.done:
ret

View File

@ -2,9 +2,14 @@
# define _LIBASM_H
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
size_t ft_strlen(const char *);
char *ft_strcpy(char *, const char *);
int ft_strcmp(const char *, const char *);
ssize_t ft_write(int, const void *, size_t);
ssize_t ft_read(int, void *, size_t);
char *ft_strdup(const char *);
#endif

65
main.c
View File

@ -1,26 +1,53 @@
#include "libasm.h"
#include <string.h>
#include <stdlib.h>
#include <errno.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 my_string[37] = "c'est moi la chaine de 36 caracteres";
printf("%ld\n", ft_strlen(my_string));
/* 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 *str;
str = (char *)malloc(150 * sizeof(char));
str = "Salut c'est moi la zone mdr\0";
char *dst;
dst = (char *)malloc(50 * sizeof(char));
char *test = ft_strcpy(dst, str);
printf("%s\n", str);
printf("%s\n", dst);
printf("%s\n", test);
printf("should be 0: %d\n", ft_strcmp(str, dst));
printf("should be 0 and not segv: %d\n", ft_strcmp(dst, test));
printf("should be 16 %d\n", ft_strcmp(my_string, str));
printf("should be -16 %d\n", ft_strcmp(str, my_string));
dst = "Salut c'est moi la diff mdr\0";
printf("should be 22: %d\n", ft_strcmp(str, dst));
dst = "Salut\0 c'est moi la diff mdr\0";
printf("should be 32: %d\n", ft_strcmp(str, dst));
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;
}