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

标签 c++ assembly

我正在尝试从 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/

相关文章:

c++ - 即使没有要复制的对象,复制构造函数也会自动调用吗?

汇编语言 ASCII 字符串排序

assembly - 使用 int 21h 和 ah = 2Ch 的延迟程序

c - 当 -mavx2 打开时为 __builtin_popcount 生成臃肿的代码

c++ - 从 char 到 const char* 的无效转换

c++ - 如何将值附加到命令行参数数组?

c - 给定返回地址,如何获取函数的地址?

assembly - XOR是MIPS中的伪命令吗?

c++ - 模板参数必须是类型吗?

c++ - 如果我提前将参数声明为变量而不是将它们写入函数调用的行中,这(在内存方面)有什么区别?