Compare commits
2 Commits
f787b174b1
...
0b3382b478
Author | SHA1 | Date |
---|---|---|
gbrochar | 0b3382b478 | |
gbrochar | d05135501f |
5
Makefile
5
Makefile
|
@ -4,6 +4,9 @@ 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)
|
||||||
|
|
||||||
|
@ -16,7 +19,7 @@ $(NAME): $(OBJ)
|
||||||
ar rcs libasm.a $(OBJ)
|
ar rcs libasm.a $(OBJ)
|
||||||
|
|
||||||
test: $(NAME)
|
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:
|
clean:
|
||||||
rm $(OBJ)
|
rm $(OBJ)
|
||||||
|
|
|
@ -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
|
|
@ -12,8 +12,8 @@ ft_strcmp:
|
||||||
inc rdx
|
inc rdx
|
||||||
jmp .loop
|
jmp .loop
|
||||||
.done:
|
.done:
|
||||||
movsx rax, byte [rdi + rdx]
|
mov al, byte [rdi + rdx]
|
||||||
movsx rbx, byte [rsi + rdx]
|
mov bl, byte [rsi + rdx]
|
||||||
sub rax, rbx
|
sub rax, rbx
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,11 @@ ft_strcpy:
|
||||||
.loop:
|
.loop:
|
||||||
cmp byte [rsi + rax], 0
|
cmp byte [rsi + rax], 0
|
||||||
je .done
|
je .done
|
||||||
mov rdx, [rsi + rax]
|
mov dl, byte [rsi + rax]
|
||||||
mov [rdi + rax], rdx
|
mov byte [rdi + rax], dl
|
||||||
inc rax
|
inc rax
|
||||||
jmp .loop
|
jmp .loop
|
||||||
.done:
|
.done:
|
||||||
|
mov byte [rdi + rax], 0
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
ret
|
ret
|
||||||
|
|
|
@ -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
|
|
@ -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
|
5
libasm.h
5
libasm.h
|
@ -2,9 +2,14 @@
|
||||||
# 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
65
main.c
|
@ -1,26 +1,53 @@
|
||||||
#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 my_string[37] = "c'est moi la chaine de 36 caracteres";
|
/* char *str1 = (char *)malloc(88 * sizeof(char));
|
||||||
printf("%ld\n", ft_strlen(my_string));
|
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;
|
char *str2 = (char *)malloc(88 * sizeof(char));
|
||||||
str = (char *)malloc(150 * sizeof(char));
|
strcpy(str2, "Salut moi une chaine de caractere trop longue\n");
|
||||||
str = "Salut c'est moi la zone mdr\0";
|
printf("%ld\n", write(1, str2, 46));
|
||||||
char *dst;
|
printf("%d\n", (int)str2[0]);
|
||||||
dst = (char *)malloc(50 * sizeof(char));
|
free(str2);
|
||||||
char *test = ft_strcpy(dst, str);
|
|
||||||
printf("%s\n", str);
|
ssize_t write_ret = write(4, "test\n", 5);
|
||||||
printf("%s\n", dst);
|
if (write_ret == -1) {
|
||||||
printf("%s\n", test);
|
fprintf(stderr, "error = %s\n", strerror(errno));
|
||||||
printf("should be 0: %d\n", ft_strcmp(str, dst));
|
perror("write");
|
||||||
printf("should be 0 and not segv: %d\n", ft_strcmp(dst, test));
|
}
|
||||||
printf("should be 16 %d\n", ft_strcmp(my_string, str));
|
errno = 42;
|
||||||
printf("should be -16 %d\n", ft_strcmp(str, my_string));
|
write_ret = ft_write(4, "test\n", 5);
|
||||||
dst = "Salut c'est moi la diff mdr\0";
|
printf("ft_write ret %ld\n", write_ret);
|
||||||
printf("should be 22: %d\n", ft_strcmp(str, dst));
|
if (write_ret == -1) {
|
||||||
dst = "Salut\0 c'est moi la diff mdr\0";
|
fprintf(stderr, "error = %d\n", errno);
|
||||||
printf("should be 32: %d\n", ft_strcmp(str, dst));
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue