getting back into x86.. painfully
This commit is contained in:
parent
8e29c2c26b
commit
6d62442fd8
9 changed files with 66 additions and 36 deletions
|
|
@ -17,12 +17,14 @@ NASM = nasm
|
|||
|
||||
UNAME_S = $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
FLAGS_ASM = -f elf64\
|
||||
FLAGS_ASM = -g\
|
||||
-f elf64\
|
||||
-D READ=0x0000000\
|
||||
-DWRITE=0x0000001
|
||||
endif
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
FLAGS_ASM = -f macho64\
|
||||
FLAGS_ASM = -g\
|
||||
-f macho64\
|
||||
-D READ=0x2000003\
|
||||
-DWRITE=0x2000004
|
||||
endif
|
||||
|
|
@ -103,6 +105,13 @@ test: $(NAME) test.c
|
|||
@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"
|
||||
|
||||
.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)
|
||||
|
|
|
|||
6
libftasm/debug.c
Normal file
6
libftasm/debug.c
Normal 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.
|
|
@ -8,23 +8,26 @@ section .bss
|
|||
|
||||
section .text
|
||||
|
||||
; int ft_putchar(int c)
|
||||
_ft_putchar:
|
||||
ft_putchar:
|
||||
xor rdx, rdx
|
||||
mov dl, dil
|
||||
mov [char], rdx
|
||||
; save c value
|
||||
push rdi
|
||||
|
||||
; write(STDOUT, char, 1)
|
||||
mov rdi, STDOUT
|
||||
mov rsi, char
|
||||
lea rsi, [rdi]
|
||||
mov rdx, 1
|
||||
mov rax, WRITE
|
||||
syscall
|
||||
|
||||
cmp rax, 0
|
||||
jle end
|
||||
mov rax, [char]
|
||||
jle err
|
||||
; success case then return c
|
||||
pop rax
|
||||
ret
|
||||
|
||||
end:
|
||||
mov rax, -42
|
||||
err:
|
||||
; error case then return -1
|
||||
mov rax, -1
|
||||
ret
|
||||
|
|
|
|||
|
|
@ -6,36 +6,35 @@ extern ft_putchar
|
|||
|
||||
%define STDOUT 1
|
||||
|
||||
section .data
|
||||
string db "(null)"
|
||||
.len: equ $ - string
|
||||
|
||||
_ft_puts: ; int puts(const char *s)
|
||||
ft_puts:
|
||||
push rdi
|
||||
call ft_strlen
|
||||
push rdi ; because strlen will clobber rdi
|
||||
call ft_strlen
|
||||
pop rdi
|
||||
push rax ; Number of printed chars have to be returned by ft_puts
|
||||
cmp rax, 0
|
||||
jg print_string ; if length > 0, print string
|
||||
mov rdi, 0xa
|
||||
call ft_putchar
|
||||
jmp error ; else go to error
|
||||
je print_nl ; if empty string skip printing
|
||||
|
||||
print_string:
|
||||
mov rsi, rdi ; string arg for write
|
||||
mov rdi, STDOUT ; file_descriptor arg for write
|
||||
mov rdx, rax ; length arg returned by ft_strlen for write
|
||||
mov rax, WRITE ; write
|
||||
mov byte [rsi + rdx], 0xa ; newline at end of string
|
||||
inc rdx
|
||||
; int write(int fd, const char *str, size_t size)
|
||||
mov rsi, rdi ; char *str
|
||||
mov rdi, STDOUT ; int fd
|
||||
mov rdx, rax ; size_t strlen
|
||||
mov rax, WRITE ; WRITE
|
||||
syscall
|
||||
dec rdx
|
||||
mov byte [rsi + rdx], 0x0 ; put back eos
|
||||
jc error
|
||||
|
||||
test rax, rax
|
||||
jle error ; if write failed, go to error
|
||||
jmp success
|
||||
print_nl:
|
||||
mov rdi, 0xa
|
||||
call ft_putchar
|
||||
test rax, rax
|
||||
jc error ; if write failed, go to error
|
||||
|
||||
success:
|
||||
pop rax ; Get number of chars printed by print_string
|
||||
inc rax ; Add new line printed by print_newline to this number
|
||||
mov rax, 0xa ; success returns '\n'
|
||||
jmp end
|
||||
|
||||
error:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
global _ft_strdup
|
||||
global ft_strdup
|
||||
|
||||
extern _malloc
|
||||
extern malloc
|
||||
extern ft_strlen
|
||||
extern ft_memcpy
|
||||
|
|
@ -15,7 +16,7 @@ ft_strdup:
|
|||
inc rax
|
||||
push rax
|
||||
mov rdi, rax
|
||||
call malloc
|
||||
call _malloc
|
||||
cmp rax, 0
|
||||
je end
|
||||
mov rdi, rax
|
||||
|
|
|
|||
|
|
@ -214,10 +214,22 @@ int test_puts()
|
|||
char str[] = "It's a char line you have to print. Not this one";
|
||||
str[35] = 0;
|
||||
int ret, ret_cmp;
|
||||
/* ft_putstr("Original:"); ret_cmp = puts(NULL); */
|
||||
ft_putstr("Notre___:|"); ft_puts(NULL);ft_putstr("|\n");
|
||||
ft_putstr("Notre___:|"); ret = ft_puts(str);ft_putstr("|\n");
|
||||
ft_putstr("Original:|"); ret_cmp = puts(str);ft_putstr("|\n");
|
||||
printf("Original:|"); ret = puts("");printf("|\n");
|
||||
printf("Notre :|"); ret_cmp = ft_puts("");printf("|\n");
|
||||
if (ret != ret_cmp)
|
||||
{
|
||||
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)
|
||||
{
|
||||
printf("FAILED: %d vs %d\n", ret, ret_cmp);
|
||||
|
|
|
|||
Loading…
Reference in a new issue