-1

My asm file

.386

.model flat,c

;----------------------------------------------
; Reference the variables from the cpp file 
;----------------------------------------------

extern global_char:byte
extern global_short:word
extern global_int:dword
extern global_ll:qword

.code

IntegerAddition proc

push ebp
mov ebp, esp

; Compute += global_char
mov al, [ebp+8]
add [global_char], al

; Compute += global_short
mov ax, [ebp+12]
add [global_short], ax

; Compute += global_int
mov eax, [ebp+16]
add [global_int], eax

; Compute += globall
mov eax, [ebp+20]
mov edx, [ebp+24]
add dword ptr[global_ll], eax
adc dword ptr[global_ll], edx

pop ebp
ret

IntegerAddition endp
end

My cpp file

#include <iostream>

using namespace std;

extern "C" char global_char = 10;
extern "C" short global_short = 100;
extern "C" int global_int = 1000;
extern "C" long long global_ll = 10000;

extern "C" void IntegerAddition(char valueA, short valueB, int valueC, long long valueD);

int main(void)
{
IntegerAddition(3, 5, 10, 20);

cout << "global_char: " << global_char << endl;
cout << "global_short: " << global_short << endl;
cout << "global_int: " << global_int << endl;
cout << "global_ll: " << global_ll << endl;

return 0;

}

My error messages

LNK4042 object specified more than once; extra ignored
LNK2019 unresolved external symbol _IntegerAddition refrenced in function _main
LNK1120 1 unresolved externals

If I comment out the function call, it builds with 0 errors (of course it doesn't run).

I have tried putting an _ in the asm file before the function name, no dice.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • But *why* would you want to do this? Why not just write portable C++? – Jesper Juhl Aug 07 '23 at 18:33
  • @JesperJuhl: The usual reason is as an exercise in learning assembly language. – Peter Cordes Aug 07 '23 at 18:36
  • 1
    @πάντα ῥεῖ - [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/q/12573816) is a poor choice of duplicate. It doesn't point out that 32-bit x86 Windows prefixes C names with `_` to make asm names, which appears to be the problem here. If that's not already obvious from the compiler error message, none of the answers on that Q&A mention the word "underscore", and none on the first page look like they're about a missing `_`. Added a NASM duplicate, which is the same problem with a different assembler (this is MASM). – Peter Cordes Aug 07 '23 at 18:39
  • @PeterCordes Fair point. But then OP could/should have mentioned that (IMHO). – Jesper Juhl Aug 07 '23 at 18:47
  • @JesperJuhl: I don't think every assembly [mcve] needs to justify its existence. (At least not in terms of why they're using assembly. In this case, there are a boatload of existing questions about linking C++ and asm, so some justification for not being a duplicate is necessary.) The details of the example have learning exercise written all over them, adding with 4 different type widths. If the function had been wrapping a single instruction you can't trivially use from C, `psadbw` or `popcnt`, as if someone maybe didn't realize they could use intrinsics, then it would be worth commenting. – Peter Cordes Aug 07 '23 at 18:58
  • Wait, I just noticed *I have tried putting an `_` in the asm file before the function name, no dice.* - are you sure you actually linked the `.obj` files from the asm and C++ files? Because that should work with `extern "C"`; we know the linker is looking for `_IntegerAddition` not stdcall `_IntegerAddition@20` or something. Maybe a duplicate of [external assembly file in visual studio](https://stackoverflow.com/q/33751509) as well. – Peter Cordes Aug 07 '23 at 19:03
  • Hi everyone, To answer one of your questions, this is part of a learning exercise. I was asked about 2 weeks ago to teach assembly language this Fall and part of the learning objectives include using C++ and Assembly. I was following an example from someone else, but can't seem to get it to run. The school uses Visual Studio 2019 and the example was using 2017. Don't know if that was part of the problem. I put an underscore on the function name, prototype, and function call. It has made no difference. – Jason_Coder Aug 08 '23 at 18:32
  • Hi everyone, I figured it out. Yes the underscores need to be there, but that wasn't the only problem. The problem I was having was both file names were the same. I thought this would be OK because they have different extensions. But no so! Thanks for all your help. I really appreciate it! – Jason_Coder Aug 09 '23 at 11:59

0 Answers0