Search code examples
assemblynasmsystem-callserrno

Trying to set up Errno using NASM


I'm doing a school's project and try to set up the variable Errno using NASM.

My exercice is to replicate the function write using only NASM. I can't use C language at all. My function has to allow a C file to read the value of Errno if there is an error (length to write < 0 for example).

I tried to use call ___error ("extern ___error" in the file's header) but I'm not really sure about how it should be used.

What I understood is that Errno isn't linked to a particular register.

So my question is : How should I do to assign the good value to Errno without using C language ?

Here is my code at the moment :

extern  ___error
SYS_WRITE_MAC equ 0x2000004
SYS_WRITE_LINUX equ 4
section .text
    global _ft_write
_ft_write:
    cmp rdx, 0
    jl _end
    cmp rdi, 0
    jl _end
    mov rax, SYS_WRITE_MAC
    syscall
    mov rdi, rax
    mov r15, rax
    call ___error
    mov rax, r15
    ret
_end:
    call ___error
    mov rax, -1
    ret

Sorry if I'm not understandable, I'm not a native english speaker.


Solution

  • I finally understood :

    Using "call ___error" allows to return a int* that point on the variable Errno. So the solution is to modify the byte pointed by Rax after use of call ___error.