使用来自 C++ 代码的参数调用 masm 函数

call masm function with arguments from c++ code

这里是汇编代码:

.386
.model flat, stdcall

_asmFunc proto arg1: dword, arg2: dword

.data
.code

_asmFunc proc, arg1: dword, arg2:dword
    mov eax, arg1
    add eax, arg2
    ret
_asmFunc endp

end

这里是 C++ 代码:

#include <iostream>

extern "C" int asmFunc(int, int);

int main()
{
    std::cout << asmFunc(5, 6);

    char a;
    std::cin >> a;
    return 0;
}

问题是:如果我从函数中删除所有参数,从 asm 中的模型中删除 stdcall 并删除原型行 - 我可以从 C++ 中调用它,但如果我想传递一些参数,我需要添加它们在过程头之后,这意味着我需要添加 stdcall,在这种情况下,c++ 告诉我程序找不到我的函数(未解析的外部符号 _asmFunc),我真的找不到任何正常的组合(因为我不想通过通过寄存器手动参数或手动将它们放入堆栈并在我的函数中取出它们,太多额外的代码)允许我使用来自 C++ 的参数调用 asm 函数,要么它不能有参数或 C++ 代码找不到它

asm代码中的函数名开头不带_,asm自己加下划线

  1. .model 指令中的调用约定必须与 C 函数声明中的调用约定相匹配。对于默认的 cdecl 调用约定,只需使用 .model flat, c

  2. 由于在 .model directive, name decoration is performed automatically. This means you should not add any name decoration like _ to the names, those will be decorated according to the name mangling rules 中为指定的调用约定指定了语言类型。