1

I write program on c and I need to use functions from assembly. I think when a syscall is facing a problem it returns a negative value and write it to rax, but it doesn't do it.

I use macOS and NASM. To be more precise, my write function is:

        global  _ft_write
        extern  ___error

_ft_write:
            mov rax, 0x2000004
            syscall
            ret

for compile: nasm -fmacho64

and main:

#include <stdio.h>
#include <errno.h>

int         ft_write(int fd, const void *buf, size_t nbytes);

int         main(void)
{
    printf("%d", ft_write(-15, "wrong file descriptor", 3));
    return (1);
}

expexted output: -9

my output: 9

I tried to use read only files, but output was 9 too. Were is mistake? Why rax is non negative after syscall error and how can I receive syscall return after error?

Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55
  • A final `;` is missing on ft_write declaration – Ôrel Aug 11 '20 at 11:56
  • The method for reporting system calls is different from OS to OS. Where did you read that writing a negative value to RAX is what MacOS does? – zwol Aug 11 '20 at 11:58
  • So, Mac OS man says only about return value of syscall. That means syscall return it and write somewhere and I thought that it is RAX, but it isn't. May be you know where? – Sergey Khapugin Aug 11 '20 at 12:04
  • I don't. Perhaps you can figure it out by disassembling the syscall stubs in libSystem. – zwol Aug 11 '20 at 12:09
  • 3
    Instead of returning negative rax, the carry flag is set. Check the carry flag. – CherryDT Aug 11 '20 at 12:15

0 Answers0