为什么 gcc 使用 movl 而不是 push 来传递函数 args? [英] Why does gcc use movl instead of push to pass function args?
问题描述
注意这段代码:
#include <stdio.h>
void a(int a, int b, int c)
{
char buffer1[5];
char buffer2[10];
}
int main()
{
a(1,2,3);
}
之后:
gcc -S a.c
该命令在汇编中显示我们的源代码.
that command shows our source code in assembly.
现在我们可以在主函数中看到,我们从不使用push"命令来推送参数a 函数入栈.它使用了移动"而不是那个
now we can see in the main function, we never use "push" command to push the arguments of the a function into the stack. and it used "movel" instead of that
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $3, 8(%esp)
movl $2, 4(%esp)
movl $1, (%esp)
call a
leave
为什么会这样?它们有什么区别?
why does it happen? what's difference between them?
推荐答案
这里是 gcc 手册 不得不说:
-mpush-args
-mno-push-args
Use PUSH operations to store outgoing parameters. This method is shorter and usually
equally fast as method using SUB/MOV operations and is enabled by default.
In some cases disabling it may improve performance because of improved scheduling
and reduced dependencies.
-maccumulate-outgoing-args
If enabled, the maximum amount of space required for outgoing arguments will be
computed in the function prologue. This is faster on most modern CPUs because of
reduced dependencies, improved scheduling and reduced stack usage when preferred
stack boundary is not equal to 2. The drawback is a notable increase in code size.
This switch implies -mno-push-args.
显然 -maccumulate-outgoing-args
默认启用,覆盖 -mpush-args
.使用 -mno-accumulate-outgoing-args
进行显式编译会在此处恢复为 PUSH
方法.
Apparently -maccumulate-outgoing-args
is enabled by default, overriding -mpush-args
. Explicitly compiling with -mno-accumulate-outgoing-args
does revert to the PUSH
method, here.
2019 年更新:自 Pentium M 出现以来,现代 CPU 已具有高效的推送/弹出功能.-mno-accumulate-outgoing-args
(并使用推送)最终在 2014 年 1 月成为 -mtune=generic
的默认设置.
2019 update: modern CPUs have had efficient push/pop since about Pentium M.
-mno-accumulate-outgoing-args
(and using push) eventually became the default for -mtune=generic
in Jan 2014.
这篇关于为什么 gcc 使用 movl 而不是 push 来传递函数 args?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!