views:

122

answers:

2

Trying to understand how to link a function that is defined in a struct, the function is in the assembly code, and am trying to call it from c. I think am missing a step cause when I call the function, I get an unresolved external symbol...

;Assembly.asm
.686p
.mmx
.xmm
.model flat

include Definitions.inc

.code

?Initialize@Foo@@SIXPAUFee@@@Z proc
    jmp $
?Initialize@Foo@@SIXPAUFee@@@Z endp

end



//CFile.c
struct Fee
{
   signed long id; 
}

struct Foo
{
   static void Initialize(Fee *);
}


int startup(Fee * init)
{
  Foo::Initialize(init); //<-- This is unresolved
  return 0;
}
+3  A: 

Your assembly code defines a function whose decorated name decodes to

public: static void __fastcall Foo::InitializeCurrentCpu(struct Fee *)

As obtained through the undname.exe utility. Foo::InitializeCurrentCpu() won't be a match for Foo::Initialize(), the name doesn't match. Nor does the calling convention.

Write this code in C++ first and look at the .map file for the correct decorated name. Or declare the function with extern "C" to suppress C++ decoration.

Hans Passant
just realise I made a typo its ?Initialize@Foo@@SIXPAUFee@@@Z proc and not ?InitializeCurrentCpu@Foo@@SIXPAUFee@@@Z proc
Fredrick
You still have a mismatch on the calling convention.
Hans Passant
Where exactly do I make the change? In the Foo struct? should the format be exactly like the sample you wrote it... I am getting something like public: static void __cdecl Foo::Initialize(struct Fee *)
Fredrick
Worked... I just needed to add __fastcall instead of __cdecl
Fredrick
A: 

Whenever you need to figure how how to access C or C++ constructs from assembly language, create a small function in C or C++ and have the compiler print out the assembly language for that function.

Some benefits are: 1) It shows the preamble and post-amble required by the language and compiler; second, it shows you how the compiler accesses things.

This is always how I begin interfacing assembly and C or C++.

Thomas Matthews
For some reason the compiler was saying the function is like void __cdecl Foo but even with that failed... till I tried void __fastcall Foo, what is the difference... I guess I should get familiar with those terms first...
Fredrick