I am trying to write a wrapper function for the read () system call using asm volatile, but it will not work since res does not change its value.
Here is the code:
ssize_t my_read(int fd, void *buf, size_t count)
{
ssize_t res;
__asm__ volatile(
"int $0x80"
: "=a" (res),
"+b" (fd),
"+c" (buf),
"+d" (count)
: "a" (5)
: "memory", "cc");
if (-125 <= res && res < 0)
{
errno = -res;
res = -1;
}
return res;
}
and here int main ():
int main() {
int fd = 432423;
char buf[128];
size_t count = 128;
my_read(fd, buf, count);
return 0;
}
Am I doing something wrong? perhaps because of volatile?
I tried to debug the code, and when Eclipse goes in my_read(fd, buf, count);and gets into the line __asm__ volatile(in my_read, it fails and goes into if (-125 <= res && res < 0)...
EDIT:
ssize_t my_read(int fd, void *buf, size_t count)
{
ssize_t res;
__asm__ volatile(
"int $0x80"
: "=a" (res)
: "a" (5) ,
"b" (fd),
"c" (buf),
"d" (count)
: "memory", "cc");
if (-125 <= res && res < 0)
{
errno = -res;
res = -1;
}
return res;
}
and main:
int main() {
int fd = 0;
char buf[128];
size_t count = 128;
my_read(fd, buf, count);
return 0;
}
source
share