From d55c035b9781f0a28be7a9a7a2ecd120b296e5d9 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 11 Dec 2025 23:11:56 +0100 Subject: [PATCH] feat(read): adding the read function in asm --- src/ft_read.s | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/ft_read.s diff --git a/src/ft_read.s b/src/ft_read.s new file mode 100644 index 0000000..8b2fb73 --- /dev/null +++ b/src/ft_read.s @@ -0,0 +1,20 @@ +segment .note.GNU-stack +extern __errno_location + +section .text + global ft_read + +ft_read: + mov rax, 0 ; syscall 0 is for read + syscall ; call the system call read + cmp rax, 0 ; check if the return value of syscall + jl .error ; jump if the syscall return less than 0 + ret ; return the value of the syscall + +.error: + mov rdi, rax ; stock errno value in rdi + neg rdi ; errno value in positive (index used by C function) + call __errno_location ; rax = &errno + mov [rax], edi ; stock the errno return in the address of rax + mov rax, -1 ; set the value of rax to -1 + ret ; return with -1 (and errno value)