I have some x86 code in a .asm file and am trying to use it from C++:
C++
#include <iostream>
extern "C" int addInts(int a, int b);
int main() {
int a = 1;
int b = 2;
int result = addInts(a, b);
std::cout << "Result :\t" << result << std::endl;
return 0;
}
Asm
.386
.MODEL FLAT, C
.CODE
addInts PROC
PUSH EBP
MOV EBP, ESP
MOV EAX, [EBP+8]
MOV ECX, [EBP+12]
ADD EAX, ECX
POP EBP
RET
addInts ENDP
END
Attempting to run this results in:
LNK4042 (warn) object specified more than once, ignoring extras (asm object file)
LNK2019 (error) unresolved external symbol _addInts referenced in function _main (asm object file)
Followed by a final act of defiance taking the form of fatal error LNK1120 due to the unresolved external (solution executable)
I'm using Visual Studio 2019 with MSVC v142 and MASM. My other, self-contained assembly code has had no issues, and another function I've written involving reading int arrays in x86 from C++ worked fine. I really can't see what's going wrong here, if its a problem with my code, some esoteric setting, or something else entirely.
If I change the last line of the Asm code to END addInts
then the program just runs and immediately exits with nothing in std::cout
.
The solution file has no entry point defined in linker settings, which was what I did for the last piece of code that called asm from C++.
The asm file is included in the build, using Microsoft Macro Assembler.
The cpp file is set to compile as C++, just in case.