gpt4 book ai didi

c++ - 组装发送参数-fastcall

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:47 24 4
gpt4 key购买 nike

我正在尝试从 C++ 调用我的 asm 函数并发送两个参数,根据维基百科关于 fastcall 调用约定,这两个参数应该保存在 ecx 和 edx 中。

但这不起作用。我错过了什么吗?

汇编 x86

.model flat

.code
_TestFunction proc

mov eax, ecx
add eax, edx
ret

_TestFunction endp
end

C++代码

#include <iostream>

extern "C" int TestFunction(int a, int b);

int main()
{
std::cout << "Function returns:" << TestFunction(200,100) << std::endl;

std::cin.get();
return 0;
}

函数返回 1,这是寄存器:

ECX = 00000000 EDX = 00000001

构建日志:

1>------ Rebuild All started: Project: Tutorial, Configuration: DebugWin32 ------ 1>
Assembling asm.asm... 1>Main.cpp 1>Tutorial.vcxproj -> C:\Users\nilo\documents\visual studio2012\Projects\Tutorial\Debug\Tutorial.exe

========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

最佳答案

如果你真的想要__fastcall calling convention在 Win32 中,您的代码需要做一些小改动:

在程序集文件中,更改

_TestFunction proc
...
_TestFunction endp

@TestFunction@8 proc
...
@TestFunction@8 endp

在C++文件中,更改

extern "C" int TestFunction(int a, int b);

extern "C" int __fastcall TestFunction(int a, int b);

关于c++ - 组装发送参数-fastcall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com