c - gcc,asm volatile ("": : "r" (x))的目的是什么

标签 c gcc assembly inline-assembly volatile

我在C源代码中看到过这样的代码,由gcc在Linux(计算机)上编译:

extern double prices[4000];

void somefunction()
{
  //this function is called again after each 5 seconds interval

  //some long codes that use prices[]
  // ...

  int i;
  for (i=0; i<4000; i++)
  {
      asm volatile ("" : : "r" (prices[i]));
  }
}

所以我有一些问题:

  1. 这里内联汇编的目的是什么?
  2. 看起来prices[i]是值,它应该是指针吗?
  3. 在我看来,asm代码只是将prices[i]放入寄存器中以供以后引用,但是循环次数为4000,这没有意义(计算机没有这么多寄存器)

最佳答案

volatile 关键字告诉编译器不允许移动这个汇编 block 。

asm("":::"memory")是一个简单的编译器栅栏。

来自here :

You can prevent an asm instruction from being deleted by writing the keyword volatile after the asm. [...] The volatile keyword indicates that the instruction has important side-effects. GCC will not delete a volatile asm if it is reachable.

关于c - gcc,asm volatile ("": : "r" (x))的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645498/

相关文章:

c - GTK3 更新菜单栏和菜单项

c - 编程语言和方法是否效率低下? (需要汇编程序和 C 知识)

c++ - 将 C/C++ 转换为 8086/88 汇编

assembly - rax=1 的最短 Intel x86-64 操作码?

linux - musl 无法将 libc.a 链接到共享库

c - 归并排序间隔递归

c - Linux 中 fork() 调用的来源在哪里?

c - 为什么我在使用 malloc 并使用 sizeof 打印后没有得到正确的大小?

c++ - 如何在 X86 PC 上为 arm 编译 gcc 编译器

c++ - 多类编译错误