Author Topic: NASM and C++ object files  (Read 14177 times)

Offline Mezo40

  • Jr. Member
  • *
  • Posts: 8
NASM and C++ object files
« on: August 20, 2011, 04:13:56 AM »
Hi everyone, :)
Can i use an C++ object file that compiled with MSVC in the NASM code file? :)
-----------------------------------------------
Thanks in advance.  :)

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: NASM and C++ object files
« Reply #1 on: August 20, 2011, 11:53:33 AM »
Yes, provided you understand how MSVC mangles function names.  It is far easier to write functions and export them using regular C style interface where only a leading underscore will be prepended to the function name, ie:

#ifdef __cplusplus
    extern "C" int yourfunc();
#endif

which you can then call from you asm source as call _yourfunc

Note that in Linux the C calling convention does not include the leading underscore.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: NASM and C++ object files
« Reply #2 on: August 20, 2011, 05:06:28 PM »
As for objects themselves, as long as you link against your stdcxx library for some of the runtime stuff needed by C++ code, you should be fine. This is not a simple task though, each compiler has their own method of invoking methods, their own standard for where the object pointer must be located (sometimes ECX, sometimes EDX, and sometimes the stack), and each compiler will layout the object's memory (VMT, VDT, static method references, etc) in very different ways. If you are really brave, you might look into the source code for ObjASM32 and ATC (I learned a ton about OOP from Homer, Ultrano, and Biterider). In the end, you're probably going to need to reverse engineer some example objects with your MSVC compiler to learn how it structures it's objects (I suggest IDA Pro for this task). Start with basic static objects and work your way up to dynamic multi-inherited objects. Another good starting point would be to look into the COM+ interface. Many assembly coders make use of this framework for things like DirectX access and it is nothing more than a 3-tier OOP model.

Good Luck,
Bryant Keller

About Bryant Keller
bkeller@about.me

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: NASM and C++ object files
« Reply #3 on: August 20, 2011, 11:29:02 PM »
Note that in Linux the C calling convention does not include the leading underscore.

More specifically, ELF.

Offline Mezo40

  • Jr. Member
  • *
  • Posts: 8
Re: NASM and C++ object files
« Reply #4 on: August 26, 2011, 02:44:42 AM »
Thank very much for all your replies :D :)