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 > Stack Pointer basics?

#46216 - Digeraticus - Tue Jun 21, 2005 7:55 pm

I was wondering if anyone knew where I could find out about the stack pointer, how to use it, and what it is good for. Thanks.

#46228 - arundel - Tue Jun 21, 2005 11:23 pm

There's not a lot to say. The stack pointer is a standard register that points to a memory location (n=32bit register).

PUSH n:
mov reg(n), [SP]
sub SP, 4

POP n:
mov [SP], reg[n]
add SP, 4

The stack layout itself is very simple. It's like a pile of sheets of paper. You put every new sheet of paper on top of the pile.

Of course you can manipulate the stack pointer without limitations.
_________________
http://www.nausicaa.net

#46230 - DekuTree64 - Tue Jun 21, 2005 11:58 pm

Actually, the stack on GBA is full-descending, so you need to subtract BEFORE storing a value to it.

Push a register:
Code:
sub sp, sp, #4
str reg, [sp]

// or more efficient, pre-decrement and write back to sp
str reg, [sp, #-4]!


Pop a register:
Code:
ldr reg, [sp]
add sp, sp, #4

// or equivalently, post-increment with the load
ldr reg, [sp], #4


Easiest is to use the load/store multiple instructions:

Code:
stmfd sp!, {reg1, reg2, reg3}
ldmfd sp!, {reg1, reg2, reg3}


Or equivalently in THUMB:

Code:
push {reg1, reg2, reg3}
pop {reg1, reg2, reg3}

_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#46250 - Digeraticus - Wed Jun 22, 2005 8:40 am

I was hoping to find out more about things like Stack Frames, parameter passing via the stack, and how you are supposed to save registers to the stack before entering a new subroutine.

Are we supposed to keep track of which registers we save and load, or does the hardware do part of that job?

#46253 - Cearn - Wed Jun 22, 2005 9:42 am

Not sure about stack frames, but if you want to know how parameters are passed, you may want to take a look at the AAPCS (it's a pdf btw).

The short of it is: the first four arguments are put in r0-r3, the rest (if any) go on the stack. r0-r3 are scratch registers, meaning that the caller of a function expects them to be clobbered (goes for r12 to IIRC). The called function doesn't have to save those. If you need the other registers (r4 and on) the called function should push/pop them. If a function calls another function, then r14 (the link register, lr) must be stacked as well.

If you want to see how the compiler does this, compile with -S and browse the generated assembly. It can be quite instructive.

#46392 - Digeraticus - Fri Jun 24, 2005 2:54 am

Can we get any more info on medium to advanced stack uses? I guess I underestimated these stack topics...

#46410 - sajiimori - Fri Jun 24, 2005 10:31 am

Computer science books on algorithms, data structures, and compilers will discuss stacks at length. ARM-specific details can be found in the ARM documentation. Let us know if you have any specific questions.

#46435 - Miked0801 - Fri Jun 24, 2005 6:50 pm

Stack - Last if First Out data storage structure.

The default (r13) is used for temporary variable storage, register saving, and local variable declarations. The default stack starts at the end (almost) of IWRAM and grows backwarads through memory so that the top of memory can be safly reserved for static and global declarations.

Anything else :)

#46441 - Digeraticus - Fri Jun 24, 2005 9:22 pm

Miked, I made it pretty clear that I want to know about parameter passing via the stack, using the stack for temporary variables, and stack frames... I guess I know all the basic stuff, and when I encountered something I hadn?t heard about before, I assumed it was basic when I read this thread. But from people?s responses, I guess using a stack in these ways isn?t as basic as I thought.

So if I could get more info on this stuff, that?d be nice. If you think I?d get a better response in a new thread titled ?Advanced Stack Techniques?, I can post it...

#46443 - poslundc - Fri Jun 24, 2005 10:10 pm

I think everyone is having trouble figuring out what exactly it is you want to know. There are no real advanced "techniques" to using a stack. It's just that: a stack. Creating a thread with a different title won't change that.

On the GBA, when you want to store data for a routine, you put it on the stack, usually using stmfd. Then when you want to pop stuff off, you use ldmfd. You can access specific offsets into the stack using str and ldr if you keep track of where you've placed your stuff.

Ensuring that the SP is at the same point when your subroutine exits as it was when it began maintains the stack frame for your routine, as specified in the AAPCS.

This topic goes into detail about how the AAPCS specifies behaviour for variadic routines, if that's what you're looking for.

Dan.

#46445 - Miked0801 - Fri Jun 24, 2005 10:21 pm

Oh wait, he's talking using: mov r13,13 to double the speed at which stack accesses occur. I get it.

:)