我正在尝试从 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: Debug Win32 ------ 1>
Assembling asm.asm... 1> Main.cpp 1> Tutorial.vcxproj -> C:\Users\nilo\documents\visual studio 2012\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/