You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何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é

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 02:00:13