问题描述
使用这个站点,我为一个简单程序生成了汇编代码.
Using this site, I generated assembly code for a simple program.
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 5
mov eax, DWORD PTR [rbp-4]
mov edi, eax
mov eax, 0
call ifunc
mov eax, 0
leave
ret
这里 a 的值从内存加载到 eax 中,然后移动到 edi 传递给函数.但是我写了一个类似的汇编代码,直接将a的值加载到edi中.两者都工作正常.GCC 是否遵循此进行某种优化?先加载eax有什么好处?
Here value of a is loaded from memory into eax and then it is moved to edi to pass to function. But I wrote a similar assembly code and directly loaded value of a into edi. Both works fine. Does GCC follows this for some kind of optimization? What is the benefit of loading eax first?
推荐答案
恰恰相反.您正在编译没有优化,因此 gcc 不会进行优化,该优化会删除多余的 mov
和不必要的 a
作为堆栈内存中的变量.
Just the opposite. You're compiling without optimizations so gcc doesn't do the optimization that would have removed the redundant mov
s and the unnecessary allocation of a
as a variable in stack memory.
先加载 eax 有什么好处?
它没有,只是多余的.未经优化的编译器代码包含这样看起来很愚蠢的东西是很常见的.
It has none and is just redundant. It is very common for unoptimized compiler code to contain stupid-looking stuff like this.
这篇关于为什么 GCC 先将整数加载到 eax,然后再加载到其他一些相关寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!