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 > Symbol Variables (Stack and Register)

#16081 - DarkPhantom - Sat Feb 07, 2004 8:17 pm

I've noticed that GCC likes to use the registers for local variables rather than just putting them all on the stack. Now that I think about this it makes perfect sense (obviously) but I didn't expect it because I've never programmed for a processor with so many general purpose registers before.

My question is, how do you setup variable names with GAS? In TASM on the 80x86 you can use the LOCAL keyword to setup stack variables which you reference by name and this saves the sanity of any programmer not having to remember what he put at what offset since the assembler takes care of that. I assume that there must be a way to assign symbolic names both to stack offsets as well as registers with GAS. Anybody know? Thx!

#16083 - torne - Sat Feb 07, 2004 8:55 pm

You can't assign symbolic names to stack offsets in any sensible way; it can be done with symbols, but it's not really designed for it. You can create aliases for registers, though: see the info documentation and look for the .req directive (under ARM-dependent info).

I've written quite huge amounts of assembly code for ARM without actually ever using local variables, though; anything that has more live temporaries than you have integer registers free on ARM is probably too complicated in the first place. The only time I really use stack offsets is to get parameters for functions that take more than 4 arguments.

I'm working on a high-level assembler which will allow you to use names for registers without specifying which register they are (automatically binds to any suitable register in the most efficient way possible), and can use named local variables, but it's not anywhere near suitable for release yet. http://whitefang.fitz.cam.ac.uk/proposal/ if you're interested.

#16086 - DarkPhantom - Sat Feb 07, 2004 9:07 pm

torne wrote:
I've written quite huge amounts of assembly code for ARM without actually ever using local variables, though; anything that has more live temporaries than you have integer registers free on ARM is probably too complicated in the first place. The only time I really use stack offsets is to get parameters for functions that take more than 4 arguments.


Cool. But what about recursive functions? Just push your regs to the stack before the call and then pop them?

#16090 - poslundc - Sat Feb 07, 2004 9:46 pm

DarkPhantom wrote:
Cool. But what about recursive functions? Just push your regs to the stack before the call and then pop them?


The only registers you may need to save before calling a function (because they may be overwritten) are r0-r3, r12, and r14 (lr).

Conversely, any function you write is expected to preserve the values in r4-r11, which is why if you need them in a function you should push them onto the stack at the beginning and pop them off at the end (as well as lr if you want to free it up for use).

The only difference with recursive functions is that since you know the function will be calling itself, you can make some of your own optimizations and plan which registers you'll preserve and clobber. Then you just push and pop them before and after the call as you describe.

Dan.

#16095 - torne - Sun Feb 08, 2004 3:00 am

poslundc wrote:
The only registers you may need to save before calling a function (because they may be overwritten) are r0-r3, r12, and r14 (lr).

Exactly; this is why I only use them for temporaries that don't live across function calls (I reorder instructions if neccecary).