1

调用函数时,g ++中是否有一种安全的方法可以强制变量位于某个寄存器中?此函数包含假定某些寄存器中的输入的 inline-asm-code。

我试图将局部变量声明在固定寄存器 ( register int x asm ("$10")) 中并将它们传递给函数,但-O3搞砸了。

我不想通过在寄存器中声明全局变量来为整个程序保留寄存器。

4

4 回答 4

1

我希望函数的参数在寄存器中传递,您可以执行以下操作:

int __attribute__((fastcall)) foo(register int a, register int b)
{
    return a + b;
}
  • __attribute__((fastcall))表示函数的前两个参数分别传入ECX和EDX。
  • register关键字用于防止 GCC 在进入函数后将参数复制到堆栈中。

我发现这可以在不同-O级别上可靠地工作。

于 2011-01-17T12:39:12.003 回答
0

使用asm volatile内联汇编块,如本页所述。

于 2010-11-11T13:44:31.263 回答
0

Pass variables declared with explicit registers directly to the inline asm statement; the register must be specified in the function containing the asm statement.

于 2012-01-28T19:28:46.357 回答
0

您可以使用扩展程序集。这适用于 gcc,它应该可以工作: http: //gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html 您可以使用将由您想要的变量填充的输入寄存器。或者您可以在内联汇编代码中直接通过其名称来引用 C++ 变量。

于 2011-01-17T12:11:41.620 回答