0

Given the wrapper function for system call write :

ssize_t my_write(int fd, const void *buf, size_t count)
{
    long __res;
    __asm__ volatile
        ("int $0x80"
                : "=a" (__res)
                : "0" (4),"D" ((long)(fd)),"S" ((long)(buf)), "d" ((long)(count))
                : "ebx","memory");

      if (-125 <= __res && __res < 0)
      {
        errno = -__res;
        __res   = -1;
      }

      return __res;
}

I've tried it with the code (from int main()) :

int main() {


     my_write(2,"an read error occured\n",26);


     return 0;
 }

However it doesn't work . Any idea why ?

Thanks

JAN
  • 21,236
  • 66
  • 181
  • 318
  • 1
    Try reading this question: http://stackoverflow.com/questions/499188/how-is-the-system-call-in-linux-implemented – ugoren Apr 22 '12 at 12:55
  • That is your fourth question in the same direction. what kind of project is that where you are forced to implement all low level IO with assembler calls into the kernel? – Jens Gustedt Apr 22 '12 at 16:02
  • @JensGustedt: Friend,just making a conversation . As you can see I'm not asking you to solve my homework , just correct me when I'm wrong. – JAN Apr 22 '12 at 16:47

1 Answers1

3

Your constraints are off, the file descriptor needs to go in EBX, the buffer in ECX (not EDI/ESI respectively like you have).

Try:

__asm__ volatile
    ("int $0x80"
            : "=a" (__res)
            : "0" (4),"b" ((long)(fd)),"c" ((long)(buf)), "d" ((long)(count))
            : "memory");
Mat
  • 202,337
  • 40
  • 393
  • 406