Я пытаюсь написать функцию-оболочку для системного вызова read(), используя asm volatile, но это не сработает, так как res не меняет своего значения.
Вот код:
ssize_t my_read(int fd, void *buf, size_t count)
{
ssize_t res;
__asm__ volatile(
"int $0x80" /* make the request to the OS */
: "=a" (res), /* return result in eax ("a") */
"+b" (fd), /* pass arg1 in ebx ("b") */
"+c" (buf), /* pass arg2 in ecx ("c") */
"+d" (count) /* pass arg3 in edx ("d") */
: "a" (5) /* passing the system call for read to %eax , with call number 5 */
: "memory", "cc"); /* announce to the compiler that the memory and condition codes have been modified */
/* The operating system will return a negative value on error;
* wrappers return -1 on error and set the errno global variable */
if (-125 <= res && res < 0)
{
errno = -res;
res = -1;
}
return res;
}
а вот int main ()
:
int main() {
int fd = 432423;
char buf[128];
size_t count = 128;
my_read(fd, buf, count);
return 0;
}
Я делаю что-то неправильно ? может это из-за volatile
?
Я пытался отладить код, и когда Eclipse переходит в my_read(fd, buf, count);
и доходит до строки __asm__ volatile(
в my_read
, он терпит неудачу и переходит в if (-125 <= res && res < 0)
...
ИЗМЕНИТЬ:
ssize_t my_read(int fd, void *buf, size_t count)
{
ssize_t res;
__asm__ volatile(
"int $0x80" /* make the request to the OS */
: "=a" (res) /* return result in eax ("a") */
: "a" (5) , /* passing the system call for read to %eax , with call number 5 */
"b" (fd), /* pass arg1 in ebx ("b") */
"c" (buf), /* pass arg2 in ecx ("c") */
"d" (count) /* pass arg3 in edx ("d") */
: "memory", "cc"); /* announce to the compiler that the memory and condition codes have been modified */
/* The operating system will return a negative value on error;
* wrappers return -1 on error and set the errno global variable */
if (-125 <= res && res < 0)
{
errno = -res;
res = -1;
}
return res;
}
и основное:
int main() {
int fd = 0;
char buf[128];
size_t count = 128;
my_read(fd, buf, count);
return 0;
}