Compare commits

..

No commits in common. "0b3382b47899958949f41c8f2ae7bdc5dc4130ad" and "f787b174b1844ed76ad86eccdc9e753322c12820" have entirely different histories.

8 changed files with 24 additions and 127 deletions

View File

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

View File

@ -1,21 +0,0 @@
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 inc rdx
jmp .loop jmp .loop
.done: .done:
mov al, byte [rdi + rdx] movsx rax, byte [rdi + rdx]
mov bl, byte [rsi + rdx] movsx rbx, byte [rsi + rdx]
sub rax, rbx sub rax, rbx
ret ret

View File

@ -5,11 +5,10 @@ ft_strcpy:
.loop: .loop:
cmp byte [rsi + rax], 0 cmp byte [rsi + rax], 0
je .done je .done
mov dl, byte [rsi + rax] mov rdx, [rsi + rax]
mov byte [rdi + rax], dl mov [rdi + rax], rdx
inc rax inc rax
jmp .loop jmp .loop
.done: .done:
mov byte [rdi + rax], 0
mov rax, rdi mov rax, rdi
ret ret

View File

@ -1,25 +0,0 @@
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

View File

@ -1,21 +0,0 @@
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,14 +2,9 @@
# define _LIBASM_H # define _LIBASM_H
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <unistd.h>
size_t ft_strlen(const char *); size_t ft_strlen(const char *);
char *ft_strcpy(char *, const char *); char *ft_strcpy(char *, const char *);
int ft_strcmp(const 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 #endif

65
main.c
View File

@ -1,53 +1,26 @@
#include "libasm.h" #include "libasm.h"
#include <string.h>
#include <stdlib.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() { int main() {
/* char *str1 = (char *)malloc(88 * sizeof(char)); char my_string[37] = "c'est moi la chaine de 36 caracteres";
ft_strcpy(str1, "Salut moi une chaine de caractere trop longue\n"); printf("%ld\n", ft_strlen(my_string));
printf("%ld\n", ft_write(1, str1, 46));
printf("%d\n", (int)str1[0]);
free(str1);
char *str2 = (char *)malloc(88 * sizeof(char)); char *str;
strcpy(str2, "Salut moi une chaine de caractere trop longue\n"); str = (char *)malloc(150 * sizeof(char));
printf("%ld\n", write(1, str2, 46)); str = "Salut c'est moi la zone mdr\0";
printf("%d\n", (int)str2[0]); char *dst;
free(str2); dst = (char *)malloc(50 * sizeof(char));
char *test = ft_strcpy(dst, str);
ssize_t write_ret = write(4, "test\n", 5); printf("%s\n", str);
if (write_ret == -1) { printf("%s\n", dst);
fprintf(stderr, "error = %s\n", strerror(errno)); printf("%s\n", test);
perror("write"); printf("should be 0: %d\n", ft_strcmp(str, dst));
} printf("should be 0 and not segv: %d\n", ft_strcmp(dst, test));
errno = 42; printf("should be 16 %d\n", ft_strcmp(my_string, str));
write_ret = ft_write(4, "test\n", 5); printf("should be -16 %d\n", ft_strcmp(str, my_string));
printf("ft_write ret %ld\n", write_ret); dst = "Salut c'est moi la diff mdr\0";
if (write_ret == -1) { printf("should be 22: %d\n", ft_strcmp(str, dst));
fprintf(stderr, "error = %d\n", errno); dst = "Salut\0 c'est moi la diff mdr\0";
fprintf(stderr, "error = %s\n", strerror(errno)); printf("should be 32: %d\n", ft_strcmp(str, dst));
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; return 0;
} }