有人可以解释以下代码语句吗?

标签 c volatile

谁能解释一下下面的说法?

static inline void cpu_write_dtlb(uint32_t vpn, unsigned attr)
{
    asm volatile ("wdtlb  %1, %0; dsync\n" :: "r" (vpn), "r" (attr));
}

最佳答案

如果您的问题是关于语法,那就是 GCC 内联汇编语言语法。 特别是,%0%1 被替换为 GCC 存储 “r” 参数的寄存器。

volatile 关键字有这样的效果:

GCC’s optimizers sometimes discard asm statements if they determine there is no need for the output variables. Also, the optimizers may move code out of loops if they believe that the code will always return the same result (i.e. none of its input values change between calls). Using the volatile qualifier disables these optimizations. asm statements that have no output operands, including asm goto statements, are implicitly volatile.

有关此扩展语法的详细信息,请参阅 Extended Asm - Assembler Instructions with C Expression Operands 的 GCC 文档.

关于有人可以解释以下代码语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57307193/

相关文章:

java - volatile 变量并刷新到主内存/从主内存读取

c - 关于逻辑运算符的基本编程练习

c - 是否所有 C 编译器都允许函数返回结构?

c - 为什么编译器会发出 "warning: assignment makes integer from pointer without a cast"?

c - 如何使用函数指针数组?

python - 获取文件保存的目录(tkinter filesave 提示)

c - C 中的浮点加法

c - 使用 CAS(Compare And Swap)时,如何确保旧值确实是旧值?

java - Java AtomicReference#getAndSet 的用例是什么?

c++ - 是只需要一次 volatile 还是从MMIO mmap'd地址指针派生的每个指针上都需要volatile?