Finish mandatory
This commit is contained in:
parent
83bd3495a1
commit
4ef28d1ce9
4
Makefile
4
Makefile
|
|
@ -1,7 +1,7 @@
|
|||
CC := gcc
|
||||
AS := nasm
|
||||
ASFLAGS := -g -f elf64 -I src/
|
||||
CFLAGS := -g -Wall -Wextra -Wno-override-init -std=c2x
|
||||
CFLAGS := -g -Wall -Wextra -Wno-override-init -std=c2x -fPIC -fPIE
|
||||
MAIN := $(wildcard *.c)
|
||||
SRC := $(wildcard src/*.s)
|
||||
OBJ_DIR := build
|
||||
|
|
@ -12,7 +12,7 @@ NAME := main
|
|||
LIB_NAME:= asm
|
||||
|
||||
LD := $(CC)
|
||||
LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME)
|
||||
LDFLAGS := -L$(OBJ_DIR) -l$(LIB_NAME) -fPIC -fPIE
|
||||
|
||||
|
||||
all: $(NAME)
|
||||
|
|
|
|||
62
main.c
62
main.c
|
|
@ -22,6 +22,8 @@ main(void) {
|
|||
test_read();
|
||||
printf("\n");
|
||||
test_strdup();
|
||||
printf("\n");
|
||||
test_calloc();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -99,12 +101,22 @@ void
|
|||
test_write() {
|
||||
// ft_write
|
||||
{
|
||||
|
||||
printf("ft_write ret: %ld\n", ft_write(1, "test ", 5));
|
||||
size_t error = ft_write(-1, "test ", 5);
|
||||
printf("ft_write error: %ld\n", error);
|
||||
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
||||
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// write
|
||||
{
|
||||
printf("write ret: %ld\n", write(1, "test ", 5));
|
||||
size_t error = write(-1, "test ", 5);
|
||||
printf("write error: %ld\n", error);
|
||||
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -115,6 +127,9 @@ test_read() {
|
|||
char buf[10];
|
||||
printf("ft_read ret: %ld\n", ft_read(open("main.c", O_RDONLY), buf, 10));
|
||||
printf ("buf: %s\n", buf);
|
||||
size_t error = ft_read(open("test", O_RDWR), buf, 10);
|
||||
printf("ft_read error: %ld\n", error);
|
||||
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
|
@ -124,6 +139,9 @@ test_read() {
|
|||
char buf[10];
|
||||
printf("read ret: %ld\n", read(open("main.c", O_RDONLY), buf, 10));
|
||||
printf ("buf: %s\n", buf);
|
||||
size_t error = read(open("test", O_RDWR), buf, 10);
|
||||
printf("read error: %ld\n", error);
|
||||
printf("ERRNO_%d: %s\n", errno, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -131,12 +149,27 @@ void
|
|||
test_strdup() {
|
||||
// ft_strdup
|
||||
{
|
||||
char *buf = malloc(10);
|
||||
strcpy(buf, "test");
|
||||
|
||||
char *dup = ft_strdup(buf);
|
||||
printf("ft_strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
||||
|
||||
free(buf);
|
||||
free(dup);
|
||||
}
|
||||
|
||||
|
||||
// strdup
|
||||
{
|
||||
char *buf = malloc(10);
|
||||
strcpy(buf, "test");
|
||||
|
||||
char *dup = strdup(buf);
|
||||
printf("strdup buf: %s_%p, dup: %s_%p\n", buf, buf, dup, dup);
|
||||
|
||||
free(buf);
|
||||
free(dup);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -144,11 +177,40 @@ void
|
|||
test_calloc() {
|
||||
// ft_calloc
|
||||
{
|
||||
printf("ft_calloc\n");
|
||||
|
||||
char *error = ft_calloc(2, 10); // return ""
|
||||
printf("error: %p\n", error);
|
||||
|
||||
char *empty = ft_calloc(0, 0);
|
||||
printf("empty: %s_%p\n", empty, empty);
|
||||
|
||||
char *buf = ft_calloc(10, 1);
|
||||
strcpy(buf, "test");
|
||||
printf("test: %s_%p\n", buf, buf);
|
||||
|
||||
free(empty);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// calloc
|
||||
{
|
||||
printf("calloc\n");
|
||||
|
||||
char *error = calloc(2, 10); // return ""
|
||||
printf("error: %p\n", error);
|
||||
|
||||
char *empty = calloc(0, 0);
|
||||
printf("empty: %s_%p\n", empty, empty);
|
||||
|
||||
char *buf = calloc(10, 1);
|
||||
strcpy(buf, "test");
|
||||
printf("test: %s_%p\n", buf, buf);
|
||||
|
||||
free(empty);
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
2
main.h
2
main.h
|
|
@ -5,6 +5,8 @@
|
|||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
// DONE
|
||||
extern size_t ft_strlen(const char *s);
|
||||
|
|
|
|||
30
src/calloc.s
Normal file
30
src/calloc.s
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
bits 64
|
||||
|
||||
%include "defines.inc"
|
||||
|
||||
section .text
|
||||
global ft_calloc
|
||||
; rdi = nitems, rsi = size
|
||||
ft_calloc:
|
||||
push rbx
|
||||
mov rbx, rdi
|
||||
|
||||
mul rbx, rsi
|
||||
cmp rbx, rdi
|
||||
jl return_null
|
||||
|
||||
mov rdi, rbx
|
||||
call malloc wrt ..plt
|
||||
|
||||
mov rdi, rax
|
||||
inc rbx
|
||||
mov rsi, rbx
|
||||
call bzero wrt ..plt
|
||||
|
||||
pop rbx
|
||||
ret
|
||||
|
||||
return_null:
|
||||
pop rbx
|
||||
mov rax, 0
|
||||
ret
|
||||
|
|
@ -8,5 +8,8 @@
|
|||
|
||||
extern ft_strlen
|
||||
extern ft_strcpy
|
||||
extern malloc
|
||||
extern bzero
|
||||
extern __errno_location
|
||||
|
||||
%endif
|
||||
|
|
|
|||
|
|
@ -9,4 +9,16 @@ section .text
|
|||
mov rax, SYS_READ
|
||||
syscall
|
||||
|
||||
cmp rax, 0
|
||||
jl errno
|
||||
|
||||
ret
|
||||
|
||||
errno:
|
||||
neg rax
|
||||
push rax
|
||||
call __errno_location wrt ..plt
|
||||
pop rdx
|
||||
mov [rax], edx
|
||||
mov rax, -1
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@ bits 64
|
|||
|
||||
section .text
|
||||
global ft_strcmp
|
||||
; rdi = s1, rsi = s2
|
||||
ft_strcmp:
|
||||
|
||||
mov rax, 0
|
||||
mov rax, -1
|
||||
loop:
|
||||
mov cl, [rdi + rax]
|
||||
mov dl, [rsi + rax]
|
||||
|
||||
inc rax
|
||||
mov cl, [rdi + rax] ; cl = s1[rax]
|
||||
mov dl, [rsi + rax] ; dl = s2[rax]
|
||||
|
||||
cmp cl, 0
|
||||
je endloop
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ section .text
|
|||
global ft_strcpy
|
||||
; rdi = dst, rsi = src
|
||||
ft_strcpy:
|
||||
push rbp
|
||||
; push rbp
|
||||
|
||||
mov rax, 0 ; counter
|
||||
mov rcx, 0
|
||||
|
|
@ -21,7 +21,7 @@ section .text
|
|||
cmp dl, 0
|
||||
jne loop
|
||||
|
||||
pop rbp
|
||||
; pop rbp
|
||||
|
||||
mov rax, rdi
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -4,4 +4,19 @@ bits 64
|
|||
|
||||
section .text
|
||||
global ft_strdup
|
||||
; rdi = s
|
||||
ft_strdup:
|
||||
push rbx
|
||||
mov rbx, rdi
|
||||
|
||||
call ft_strlen
|
||||
mov rdi, rax ; put strlen result in rdi
|
||||
inc rdi
|
||||
call malloc wrt ..plt
|
||||
|
||||
mov rdi, rax ; put malloc result in dest
|
||||
mov rsi, rbx ; put s in src
|
||||
call ft_strcpy
|
||||
|
||||
pop rbx
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -3,24 +3,17 @@ bits 64
|
|||
section .text
|
||||
global ft_strlen
|
||||
ft_strlen:
|
||||
|
||||
; empilement
|
||||
push rbp
|
||||
|
||||
; code
|
||||
mov rbx, rdi
|
||||
mov rsi, 0
|
||||
mov rsi, -1
|
||||
|
||||
loop:
|
||||
mov al, [rbx + rsi]
|
||||
inc rsi
|
||||
mov al, [rbx + rsi]
|
||||
|
||||
cmp al, 0
|
||||
jne loop
|
||||
|
||||
; rétablissement
|
||||
pop rbp
|
||||
|
||||
; return
|
||||
mov rax, rsi
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -4,9 +4,21 @@ bits 64
|
|||
|
||||
section .text
|
||||
global ft_write
|
||||
ft_write:
|
||||
; rdi = fd, rsi = buf, rdx = count
|
||||
ft_write:
|
||||
mov rax, SYS_WRITE
|
||||
syscall
|
||||
|
||||
cmp rax, 0
|
||||
jl errno
|
||||
|
||||
ret
|
||||
|
||||
errno:
|
||||
neg rax
|
||||
push rax
|
||||
call __errno_location wrt ..plt
|
||||
pop rdx
|
||||
mov [rax], edx
|
||||
mov rax, -1
|
||||
ret
|
||||
|
|
|
|||
Loading…
Reference in a new issue