强制GCC在寄存器中传递参数 [英] Force GCC to pass arguments in registers
问题描述
我开始尝试在C ++中内联ASM,所以我写了这个小代码片段: #include< iostream>
int foo(int,int,int);
int main(void)
{
return foo(1,2,3);
}
int foo(int a,int b,int c)
{
asm volatile(add%1,%0\\\
\t
加%2,%0 \\\
\t
加$ 0x01,%0:+ r(a):r(b),r (C): CC);
}
输出以下汇编代码:
main:
.LFB969:
subq $ 40,%rsp
.seh_stackalloc 40
.seh_endprologue
call __main
movl $ 3,%r8d
movl $ 2,%edx
movl $ 1,%ecx
call _Z3fooiii
...没有显示的东西...
_Z3fooiii:
.LFB970:
.seh_endprologue
movl%ecx,8(%rsp)
movl%edx,16(%rsp)
movl%r8d,24(%rsp )
movl 16(%rsp),%edx
movl 24(%rsp),%ecx
movl 8(%rsp),%eax
/ APP
#15K:\inline_asm_practice_1.cpp1
add%edx,%eax
add%ecx,%eax
add $ 0x01,%eax
#0 2
/ NO_APP
movl%eax,8(%rsp)
ret
所以我可以看到它输入我的代码的位置,但上面的堆栈操作有什么用处?有什么办法可以摆脱他们;他们似乎不必要。我应该可以有
(在main中)
movl $ 3,%r8d
movl $ 2,%edx
movl $ 1,%ecx
call _Z3fooiii
(in foo)
add%edx,%ecx
add%r8d, %eax
add $ 0x01,%eax
ret
我该如何制作海湾合作委员会明白,它不需要推动堆栈中的东西,并以不同的顺序返回?我已经煎过fastcall和regparam了,我找不到任何东西。
您可能需要启用优化通过像 -O2
之类的东西来让编译器尝试写出更好更快的代码,而不是简单/易于调试/理解代码。I'm starting to try to mess around with inlining ASM in C++, so I wrote up this little snippet:
#include <iostream>
int foo(int, int, int);
int main(void)
{
return foo(1,2,3);
}
int foo(int a, int b, int c)
{
asm volatile("add %1, %0\n\t"
"add %2, %0\n\t"
"add $0x01, %0":"+r"(a):"r"(b), "r"(c):"cc");
}
Which outputs the following assembly code:
main:
.LFB969:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
movl $3, %r8d
movl $2, %edx
movl $1, %ecx
call _Z3fooiii
... stuff not shown...
_Z3fooiii:
.LFB970:
.seh_endprologue
movl %ecx, 8(%rsp)
movl %edx, 16(%rsp)
movl %r8d, 24(%rsp)
movl 16(%rsp), %edx
movl 24(%rsp), %ecx
movl 8(%rsp), %eax
/APP
# 15 "K:\inline_asm_practice_1.cpp" 1
add %edx, %eax
add %ecx, %eax
add $0x01, %eax
# 0 "" 2
/NO_APP
movl %eax, 8(%rsp)
ret
So I can see where it inputs my code, but what's with the stack manipulations above it? Is there any way I can get rid of them; they seem unnecessary. I should just be able to have
(in main)
movl $3, %r8d
movl $2, %edx
movl $1, %ecx
call _Z3fooiii
(in foo)
add %edx, %ecx
add %r8d, %eax
add $0x01, %eax
ret
How do I make gcc understand that it doesn't need to shove things on the stack and bring them back in a different order? I've fried fastcall and regparam already, and I can't find anything aboout this.
You probably need to enable optimizations via something like -O2
in order to get the compiler to try and write better/faster code, instead simpler/easier to debug/understand code.
这篇关于强制GCC在寄存器中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!