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 > how can i call assembler subroutines from C?

#168357 - moonlightcheese - Fri Apr 24, 2009 6:57 pm

i've tried asking at OSdev.org and there appears to be a problem with the assembler in devKitPro... here's the code.

load.s
Code:
.global load

load:
   mov      r0, #5
   ret

load.h
Code:
extern short load(short address);


ret is a pretty common instruction, but dkp apparently doesn't like it:
c:/public/projects/CS3243_os_proj/elf_load/elf_v.02/source/load.s:8: Error: bad instruction `ret'
make[1]: *** [load.o] Error 1
make: *** [build] Error 2

so i tried this instruction to return

mov pc, lr

and this doesn't work either. execution simply halts. it doesn't seem like it should be this difficult just to call an assembly subroutine and return a value to C... what am i doing wrong?

#168358 - Mighty Max - Fri Apr 24, 2009 7:23 pm

try BX lr
_________________
GBAMP Multiboot

#168359 - moonlightcheese - Fri Apr 24, 2009 7:31 pm

Mighty Max wrote:
try BX lr

i should have posted here first... you guys always have the answers lol.

thanks!

#168363 - Ruben - Sat Apr 25, 2009 2:14 am

And just to give you a minor explanation as to why mov pc, lr won't work..

My guess is that you called ARM from THUMB or vice-versa. Simply doing mov pc, lr means that it will try to load its next opcode from foo+1, which is not aligned which stuffs it up. Using BX LR will take care of that.

#168364 - Dwedit - Sat Apr 25, 2009 2:32 am

Well, that and RET is not on the ARM or THUMB instruction set.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."