gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:05 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com