在C ++中使用Assembly函数时出现“错误LNK2019:函数_main中引用的未解析的外部符号_long_add @ 12”

问题描述

我正在尝试使用Assembly(FASM)程序作为C ++中的函数,但是即使我链接了已编译的对象,该程序似乎也不起作用。有什么建议吗?

C ++程序:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
extern "C"
{
    unsigned int __stdcall long_add(int*,int*,int*);
}

int main()
{
    int testOne[3] = { 0x12345678,0x04000000,0xFFFFFFFF };
    int testTwo[3] = { 0x12345678,0xFFFFFFFF };
    int testOut[3] = {};

    long_add(testOne,testTwo,testOut);

    scanf;
}

组装功能:

include 'include\win32a.inc'

format MS COFF 

public long_add as '_long_add@32'

section '.code' code readable executable

proc long_add IN_NUM1,IN_NUM2,OUT_NUM

start:

        mov ecx,3
        clc
        
ADDING:

        mov eax,[IN_NUM1+(ecx-1)*4]
        mov ebx,[IN_NUM2+(ecx-1)*4]
        adc eax,ebx
        mov [OUT_NUM+(ecx-1)*4],eax
        loop ADDING
        
        ret
        endp

链接证明:https://i.stack.imgur.com/au0jy.png

编辑:我试图将数组地址移动到寄存器中,并最终得到以下代码:

format MS COFF 

public long_add as '_long_add@12'

section '.code' code readable executable

proc long_add IN_NUM1,3
        clc
        mov edi,[IN_NUM1]
        mov esi,[IN_NUM2]
        
ADDING:

        mov eax,[edi+(ecx-1)*4]
        mov ebx,[esi+(ecx-1)*4]
        adc eax,ebx
        push eax
        loop ADDING
        
        mov esi,[OUT_NUM]
        mov ecx,3
        
POPPING:
        
        pop eax
        mov [esi],eax
        inc esi
        loop POPPING
        
        ret
        endp

虽然此代码“正常运行”,如程序中没有崩溃,当我尝试添加{0x12345678、0x04000000、0xFFFFFFFF}和{0x87654321、0x02000000、0x00000001}时,它返回了{409,0,0}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...