"Error LNK2019: unresolved external symbol _long_add@12 referenced in function _main" when using an Assembly function in C++

2

I'm trying to use an Assembly(FASM) program as a function in C++, but it doesn't seem to be working, even though I linked the compiled object. Any advice?

C++ program:

#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, 0x04000000, 0xFFFFFFFF };
    int testOut[3] = {};

    long_add(testOne, testTwo, testOut);

    scanf;
}

Assembly function:

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

Link Proof: https://i.stack.imgur.com/au0jy.png

Edit: I tried to moving array addresses into registers, and ended up with the code below:

format MS COFF 

public long_add as '_long_add@12'

section '.code' code readable executable

proc long_add IN_NUM1, IN_NUM2, OUT_NUM

start:

        mov ecx, 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

And while this code "works", as in the program doesn't crash, when I try to add {0x12345678, 0x04000000, 0xFFFFFFFF} and { 0x87654321, 0x02000000, 0x00000001 }, it returns {409, 0, 0}.

c++
visual-studio
assembly
fasm
asked on Stack Overflow Nov 3, 2020 by Maxim Khannaov • edited Nov 4, 2020 by Maxim Khannaov

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0