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 > Function call returning....

#10691 - regularkid - Fri Sep 12, 2003 6:43 pm

Quick question:

When you use a 'bl' or 'bx' instruction to call a function, does it automatically save/push the return address on the stack, or do you have to do this manually. Basically, i'm asking if I do nested function calls in ASM, do I need to worry about saving the return address for each one on the stack, or is this done automatically? Thanks!
_________________
- RegularKid

#10693 - sajiimori - Fri Sep 12, 2003 6:57 pm

bl saves the return address in r14, not on the stack. bx doesn't save anything at all, and "bx lr" is often used for returning.

#10694 - DekuTree64 - Fri Sep 12, 2003 8:51 pm

You do need bx for calling thumb code from arm/arm code from thumb, and for that, use
mov lr, pc
bx r0 (or whatever reg your function's address is in)
pc points to 2 instructions ahead, so that will put the address of the instruction after the bx in lr, which is where you want to return to. You do need to push lr onto the stack before calling other functions though, since you're overwriting it with the new return address. Usually it's easiest to just push everything you need to right at the start, and pop it all back fight before you return.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#10695 - regularkid - Fri Sep 12, 2003 8:58 pm

Cool, that helps. Thanks!
_________________
- RegularKid