puts/putchar stuff

This commit is contained in:
Jack Halford 2017-11-16 15:49:59 +01:00
parent 6d62442fd8
commit 8e66148582
6 changed files with 35 additions and 25 deletions

View file

@ -102,11 +102,11 @@ fclean : clean
re : fclean all
test: $(NAME) test.c
@gcc test.c -I $(INC_DIR) -Wall -Wextra -Werror -L. -lfts -o test
@gcc test.c -I $(INC_DIR) -L. -lfts -o test
@printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n"
debug: $(NAME) debug.c
@gcc debug.c -I $(INC_DIR) -Wall -Wextra -Werror -L. -lfts -o debug
@gcc debug.c -I $(INC_DIR) -L. -lfts -o debug
@printf "\r\033[38;5;117m✓ MAKE debug\033[0m\033[K\n"
run-dbg: debug

View file

@ -2,5 +2,5 @@
int main(void)
{
ft_puts("hello\0");
return (ft_puts("h2g2"));
}

View file

@ -14,20 +14,23 @@ ft_putchar:
; save c value
push rdi
; write(STDOUT, char, 1)
mov rdi, STDOUT
lea rsi, [rdi]
mov rdx, 1
; int write(int fd, char *str, size_t len)
lea rsi, [char] ; char *str
mov [rsi], dil
mov rdi, STDOUT ; int fd
mov rdx, 1 ; size_t len
mov rax, WRITE
syscall
cmp rax, 0
jle err
jl err
; success case then return c
pop rax
ret
err:
; error case then return -1
pop rax
mov rax, -1
ret

View file

@ -25,22 +25,21 @@ print_string:
mov rdx, rax ; size_t strlen
mov rax, WRITE ; WRITE
syscall
jc error
cmp rax, 0
jl error
print_nl:
; xor rdi, rdi
; add rdi, 0xa
mov rdi, 0xa
call ft_putchar
test rax, rax
jc error ; if write failed, go to error
cmp rax, 0
jl error
success:
mov rax, 0xa ; success returns '\n'
jmp end
ret
error:
pop rax
mov rax, -1 ; Return EOF (alias -1) on error
jmp end
end:
ret

View file

@ -16,7 +16,7 @@ ft_strdup:
inc rax
push rax
mov rdi, rax
call _malloc
call malloc
cmp rax, 0
je end
mov rdi, rax

View file

@ -216,25 +216,32 @@ int test_puts()
int ret, ret_cmp;
printf("Original:|"); ret = puts("");printf("|\n");
printf("Notre :|"); ret_cmp = ft_puts("");printf("|\n");
#ifdef __APPLE__
if (ret != ret_cmp)
{
printf("FAILED: %d vs %d\n", ret, ret_cmp);
printf("FAILED: %d should be %d\n", ret_cmp, ret);
return (1);
}
#endif
#ifdef __APPLE__
// puts(NULL) segfaults on linux
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);
printf("FAILED: %d should be %d\n", ret_cmp, ret);
return (1);
}
#endif
printf("Notre :|"); ret = ft_puts(str);printf("|\n");
printf("Original:|"); ret_cmp = puts(str);printf("|\n");
#ifdef __APPLE__
if (ret != ret_cmp)
{
printf("FAILED: %d vs %d\n", ret, ret_cmp);
printf("FAILED: %d should be %d\n", ret_cmp, ret);
return (1);
}
#endif
return (0);
}
@ -484,6 +491,7 @@ int main(int argc, char **argv)
{
(void)argc;
(void)argv;
setbuf(stdout, NULL);
ft_putstr("\nPART 1:\n_______\n");
if (test_bzero() ||
test_isdigit() ||