我在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]));
}
}
所以我有一些问题:
- 这里内联汇编的目的是什么?
- 看起来prices[i]是值,它应该是指针吗?
- 在我看来,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/