我在一个小程序中包含了一些示例 ASM 代码来进行测试。
我的程序是:
#include <stdio.h>
static inline
unsigned char inb (int port) {
unsigned char data;
asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
return data;
}
int main()
{
printf("hello world %d\n", inb(22));
return 0;
}
当我运行该程序时,它在执行 ASM 代码时因段错误而崩溃。 有人能告诉我这个小程序有什么问题吗?非常感谢。
最佳答案
您需要使用 ioperm
在您被允许使用端口 I/O 之前。另外,请注意内核已经提供了 inb
and outb
functions .
Use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault.
关于c - C程序中的ASM代码导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10330767/