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.

ASM > noob question regarding compilation of C++ and ASM in DKP

#149878 - mr_munk - Sat Jan 26, 2008 10:58 am

Hi,

I have tried my best to research this on the forum but still cannot find / understand the information that I need.

I have the following asm function copied from an example:

Code:

.arm
.section .text
.align 2
.global redfill
redfill:
      mov r0, #0x4000000
      mov r1, #0x400
      add r1, r1, #3
      str r1, [r0]    
      mov r0, #0x6000000
      mov r1, #0xFF
      mov r2, #0x9600
loop1:
      strh r1, [r0], #2
      subs r2, r2, #1
      bne loop1
infin:
      b infin

this resides in source/redfill.s

I also have a function prototype in include/redfill.h
Code:

extern void redfill(void);


Finally I try to call the function in my main.cpp file
Code:

//asm function
   redfill();


and get a compiler error - 'undefined reference to redfill()'

can someone tell me where I am going wrong ?

#149880 - Kyoufu Kawa - Sat Jan 26, 2008 11:33 am

Forgot to #include redfill.h?

#149881 - mr_munk - Sat Jan 26, 2008 11:37 am

Hi, thanks for your reply, I should have mentioned in my original post that I did include the header in my cpp

#149882 - Dwedit - Sat Jan 26, 2008 11:55 am

You must declare C or ASM functions inside an extern "C" block...

extern "C"{
void foo();
}

The reason you do this is because the real C++ name for any function is mangled to indicate the types of the arguments and return type.
For example,
void foo()
gets renamed to "_Z3foov" instead of "foo".
By using an extern "C" block, you are telling the C++ compiler that this symbol name comes from a C or ASM source file, and shouldn't be mangled.

This also means that if you were to name your ASM function "_Z3foov", you could call it from C++ under the name foo. While pre-mangling names technically works, different compilers do not usually use the same mangling convention, so you should use extern "C" instead.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#149883 - mr_munk - Sat Jan 26, 2008 12:06 pm

Thank you very much for your comprehensive answer Dwedit :)

#149885 - mr_munk - Sat Jan 26, 2008 12:10 pm

Not that I want to pre-mangle my function definitions but out of curiosity - could you break down _Z3foov to show how the compiler encodes the arguments and return types?

I'm guessing v at the end indicates void
what about the _Z3 ?

#149890 - nipil - Sat Jan 26, 2008 1:59 pm

I think this page could provide you with insightful information. Considering dkp is based on gcc (afaik), take a look at the corresponding sections.

#149935 - mr_munk - Sun Jan 27, 2008 5:11 pm

Will check it out thanks :)