getting back into x86.. painfully

This commit is contained in:
Jack Halford 2017-11-15 18:11:53 +01:00
parent 8e29c2c26b
commit 6d62442fd8
9 changed files with 66 additions and 36 deletions

View file

@ -17,12 +17,14 @@ NASM = nasm
UNAME_S = $(shell uname -s) UNAME_S = $(shell uname -s)
ifeq ($(UNAME_S),Linux) ifeq ($(UNAME_S),Linux)
FLAGS_ASM = -f elf64\ FLAGS_ASM = -g\
-f elf64\
-D READ=0x0000000\ -D READ=0x0000000\
-DWRITE=0x0000001 -DWRITE=0x0000001
endif endif
ifeq ($(UNAME_S),Darwin) ifeq ($(UNAME_S),Darwin)
FLAGS_ASM = -f macho64\ FLAGS_ASM = -g\
-f macho64\
-D READ=0x2000003\ -D READ=0x2000003\
-DWRITE=0x2000004 -DWRITE=0x2000004
endif endif
@ -103,6 +105,13 @@ test: $(NAME) test.c
@gcc test.c -I $(INC_DIR) -Wall -Wextra -Werror -L. -lfts -o test @gcc test.c -I $(INC_DIR) -Wall -Wextra -Werror -L. -lfts -o test
@printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n" @printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n"
.PHONY : fclean clean re debug: $(NAME) debug.c
@gcc debug.c -I $(INC_DIR) -Wall -Wextra -Werror -L. -lfts -o debug
@printf "\r\033[38;5;117m✓ MAKE debug\033[0m\033[K\n"
run-dbg: debug
lldb ./debug
.PHONY : fclean clean re run-gdb
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

6
libftasm/debug.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
int main(void)
{
ft_puts("hello\0");
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -8,23 +8,26 @@ section .bss
section .text section .text
; int ft_putchar(int c)
_ft_putchar: _ft_putchar:
ft_putchar: ft_putchar:
xor rdx, rdx ; save c value
mov dl, dil push rdi
mov [char], rdx
; write(STDOUT, char, 1)
mov rdi, STDOUT mov rdi, STDOUT
mov rsi, char lea rsi, [rdi]
mov rdx, 1 mov rdx, 1
mov rax, WRITE mov rax, WRITE
syscall syscall
cmp rax, 0 cmp rax, 0
jle end jle err
mov rax, [char] ; success case then return c
pop rax
ret ret
end: err:
mov rax, -42 ; error case then return -1
mov rax, -1
ret ret

View file

@ -6,36 +6,35 @@ extern ft_putchar
%define STDOUT 1 %define STDOUT 1
section .data
string db "(null)"
.len: equ $ - string
_ft_puts: ; int puts(const char *s) _ft_puts: ; int puts(const char *s)
ft_puts: ft_puts:
push rdi push rdi ; because strlen will clobber rdi
call ft_strlen call ft_strlen
pop rdi pop rdi
push rax ; Number of printed chars have to be returned by ft_puts
cmp rax, 0 cmp rax, 0
jg print_string ; if length > 0, print string je print_nl ; if empty string skip printing
mov rdi, 0xa
call ft_putchar
jmp error ; else go to error
print_string: print_string:
mov rsi, rdi ; string arg for write ; int write(int fd, const char *str, size_t size)
mov rdi, STDOUT ; file_descriptor arg for write mov rsi, rdi ; char *str
mov rdx, rax ; length arg returned by ft_strlen for write mov rdi, STDOUT ; int fd
mov rax, WRITE ; write mov rdx, rax ; size_t strlen
mov byte [rsi + rdx], 0xa ; newline at end of string mov rax, WRITE ; WRITE
inc rdx
syscall syscall
dec rdx jc error
mov byte [rsi + rdx], 0x0 ; put back eos
test rax, rax print_nl:
jle error ; if write failed, go to error mov rdi, 0xa
jmp success call ft_putchar
test rax, rax
jc error ; if write failed, go to error
success: success:
pop rax ; Get number of chars printed by print_string mov rax, 0xa ; success returns '\n'
inc rax ; Add new line printed by print_newline to this number
jmp end jmp end
error: error:

View file

@ -1,6 +1,7 @@
global _ft_strdup global _ft_strdup
global ft_strdup global ft_strdup
extern _malloc
extern malloc extern malloc
extern ft_strlen extern ft_strlen
extern ft_memcpy extern ft_memcpy
@ -15,7 +16,7 @@ ft_strdup:
inc rax inc rax
push rax push rax
mov rdi, rax mov rdi, rax
call malloc call _malloc
cmp rax, 0 cmp rax, 0
je end je end
mov rdi, rax mov rdi, rax

View file

@ -214,10 +214,22 @@ int test_puts()
char str[] = "It's a char line you have to print. Not this one"; char str[] = "It's a char line you have to print. Not this one";
str[35] = 0; str[35] = 0;
int ret, ret_cmp; int ret, ret_cmp;
/* ft_putstr("Original:"); ret_cmp = puts(NULL); */ printf("Original:|"); ret = puts("");printf("|\n");
ft_putstr("Notre___:|"); ft_puts(NULL);ft_putstr("|\n"); printf("Notre :|"); ret_cmp = ft_puts("");printf("|\n");
ft_putstr("Notre___:|"); ret = ft_puts(str);ft_putstr("|\n"); if (ret != ret_cmp)
ft_putstr("Original:|"); ret_cmp = puts(str);ft_putstr("|\n"); {
printf("FAILED: %d vs %d\n", ret, ret_cmp);
return (1);
}
printf("Original:|"); ret = puts(NULL);printf("|\n");
printf("Notre :|"); ret_cmp = ft_puts(NULL);printf("|\n");
if (ret != ret_cmp)
{
printf("FAILED: %d vs %d\n", ret, ret_cmp);
return (1);
}
printf("Notre :|"); ret = ft_puts(str);printf("|\n");
printf("Original:|"); ret_cmp = puts(str);printf("|\n");
if (ret != ret_cmp) if (ret != ret_cmp)
{ {
printf("FAILED: %d vs %d\n", ret, ret_cmp); printf("FAILED: %d vs %d\n", ret, ret_cmp);