c - GCC 内联汇编——与 __volatile__ 和 "memory"有什么区别?

标签 c gcc inline-assembly

在 GCC 内联汇编中,有两种方法可以防止被优化掉:__volatile__ 关键字和将 "memory" 插入 clobber 寄存器列表。

我的问题是 __volatile__"memory" 有什么区别 - 看起来它们是一样的......但是,今天我遇到了奇怪的情况,这表明它们绝对不同! (当我使用 "memory" 时,我的程序在端口 I/O 函数中有一个错误,但是当我使用 __volatile__ 时它变得很好。)

有什么区别?

最佳答案

我对 GCC 文档的阅读是,__volatile__ 关键字适用于具有副作用的程序集:也就是说,它除了从输出中产生输入之外还做了其他事情。在您的情况下,我想“端口 I/O 功能”会导致副作用。

"memory" 破坏器仅用于读取/写入内存而不是输入/输出操作数的程序集。虽然这是一种副作用,但并非所有副作用都涉及内存。

来自manual :

The typical use of Extended asm statements is to manipulate input values to produce output values. However, your asm statements may also produce side effects. If so, you may need to use the volatile qualifier to disable certain optimizations.

The "memory" clobber tells the compiler that the assembly code performs memory reads or writes to items other than those listed in the input and output operands (for example accessing the memory pointed to by one of the input parameters).

关于c - GCC 内联汇编——与 __volatile__ 和 "memory"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059599/

相关文章:

c++ - 如何在mfc中打开类中的文件?

c - 将十六进制字符串转换为字节数组的预处理器宏

C汇编代码

C++:带有 packaged_task 的核心转储

linux - ip(null)时如何判断段错误发生在什么地方?

c - 何时在扩展的 GCC 内联汇编中使用特定的操作数约束?

无法从 MSR 读回

c - 使用 MPI_Put 的异步有限差分方案

c - 循环有自己的堆栈记录吗? (也就是为什么这段代码合法)

c - 为什么 fabs() 在使用 GCC 编译时不需要 -lm 选项