gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > assembly to c++.

#3247 - Saj - Fri Feb 21, 2003 12:08 am

I've started coding in assembly (gba guy's tutorials) but I want to access my code with C++. ( i.e sprite movement, collision etc)


Do I create header files or create custom classes or what?

thanks :)



--------------------------------------------------
I can't touch the HAM, cos I'm a muslim. :)
--------------------------------------------------

#3250 - tepples - Fri Feb 21, 2003 1:34 am

Saj wrote:
I've started coding in assembly (gba guy's tutorials) but I want to access my code with C++. ( i.e sprite movement, collision etc)

Do I create header files

Yes.

But if you're mixing assembly and C++, watch out for calling conventions. When calling assembly code from C, you typically write the assembly code to use the platform's C calling convention. However, C++ has its own calling convention. To declare that the functions prototyped in a header file use the C calling convention, wrap the header file with extern "C" {...} :
Code:
extern "C" {
  #include "asm-functions.h"
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#3265 - Saj - Fri Feb 21, 2003 11:39 am

I'm not sure if that was what I meant...

I want to create my own header files that access the hardware directly.
(To optimise my my C++ using assembly)

Here are some quotes to show you what I mean
Quote:
touchstone : I think using C++ for gamecode and C/ASM for speed critical code such as rendering, music etc is a good mix.


Quote:
burre : I prefer OOP since it simplifies structuring my code. If it turns out to take to much of a hit on execution times, I'll just have to optimize in ASM (but I hope not).


I want to be able to do what these guy's are doing...:)

#3274 - tepples - Fri Feb 21, 2003 3:54 pm


  1. Write your assembly language source code in a file whose name ends in .s.
  2. Create a C prototype for each exported entry point in your asm source code. Place the prototypes in a header file called asm.h or similar.
  3. Include asm.h from your C++ source code. But because your asm functions use C calling conventions rather than C++ calling conventions, you'll have to wrap the include with a specification of calling conventions:
Code:
extern "C" {
#include "asm.h"
}

  • You'll have to compile the asm source code with an assembler that outputs files that GNU ld can understand. (GNU ld is the linker from GNU Binutils, which is normally distributed alongside GCC.) If you don't have a separate assembler, just use gcc to compile the asm files; it knows what to do.

    _________________
    -- Where is he?
    -- Who?
    -- You know, the human.
    -- I think he moved to Tilwick.


  • Last edited by tepples on Fri Feb 21, 2003 7:25 pm; edited 1 time in total

    #3282 - Saj - Fri Feb 21, 2003 7:07 pm

    Thanks for that tepples...I'll get to work with it straight away. :)