write()的包装函数不能按预期工作

时间:2012-04-22 12:40:38

标签: c linux system-calls

给出系统调用的包装函数:

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;
}

我已尝试使用代码(来自int main()):

int main() {


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


     return 0;
 }

然而它不起作用。知道为什么吗?

谢谢

1 个答案:

答案 0 :(得分:3)

您的约束已关闭,文件描述符需要进入EBX,即ECX中的缓冲区(不是像您一样的EDI / ESI)。

尝试:

__asm__ volatile
    ("int $0x80"
            : "=a" (__res)
            : "0" (4),"b" ((long)(fd)),"c" ((long)(buf)), "d" ((long)(count))
            : "memory");