为何MSVC编译C++代码时用LEA而非MOV传递函数参数?
x64 MSVC编译时用LEA传递函数参数的疑问
我正在研究C代码编译时的函数参数传递方式,使用x64 msvc 19.35/latest编译器编译以下C代码:
#include <cstdint> void f(std::uint32_t, std::uint32_t, std::uint32_t, std::uint32_t); void test() { f(1, 2, 3, 4); }
得到的汇编结果为:
void test(void) PROC mov edx, 2 lea r9d, QWORD PTR [rdx+2] lea r8d, QWORD PTR [rdx+1] lea ecx, QWORD PTR [rdx-1] jmp void f(unsigned int,unsigned int,unsigned int,unsigned int) void test(void) ENDP
我能理解LEA指令的工作机制及最终寄存器值的正确性,但预期的汇编应为:
void test(void) PROC mov ecx, 1 mov edx, 2 mov r8d, 3 mov r9d, 4 jmp void f(unsigned int,unsigned int,unsigned int,unsigned int) void test(void) ENDP
此外,我认为使用LEA的版本会因指令间的依赖关系更慢,而clang和gcc均生成了4条MOV指令的预期结果,因此不解MSVC为何选择LEA而非MOV。
内容的提问来源于stack exchange,提问作者Gilles-Philippe Paillé
相关产品推荐
相关产品推荐

