c - C程序中的ASM代码导致段错误

标签 c linux gcc x86 inline-assembly

我在一个小程序中包含了一些示例 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/

相关文章:

c - 在 C 中存储和比较字节值

linux - 这个Centos 8系统怎么安装postgis?获取 "Error: Problem: cannot install the best candidate for the job"

linux - 当我尝试获取 ~/.bashrc 时没有 .sc_start 文件

c++ - 我怎样才能模仿一元 + 运算符正在做什么来让代码在 gcc-4.6 中正确编译?

c - 简单内存分配的段错误

c++ - 将包含复数的 C 代码移植到 C++

linux - 如何设置 Samba 在写入单个 Samba 共享时保留文件所有权?

java - 如何使用 GCJ 创建 Java String 数组?

c++ - 在 try-catch block 中包装循环会导致性能问题吗?

c - 使用 CUDA C 的大型矩阵加法