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 > Working with the stack

#11129 - Lupin - Fri Sep 26, 2003 7:45 pm

I want to write an function which uses more then just 11 registers (I think that's the amount of registers I could use freely, right?), so I have to use the stack. I know I could use....

stmfd sp!,{r10-r12,r3}
...to store r10-r12 + r3 onto the stack...
....and....

ldmfd sp!,{r10-r12,r3}
...to load r10-r12 + r3 from my stack

But the problem is, the ldm instruction would of course overwrite my current values of r10-r12,r3, but I need to save these values on another position of the stack.

Could someone please explain me how I could do all this? An stm/ldm example using offsets would be cool, so that I could free like 8 bytes for 1 data set and access these 8 bytes directly and have another data set which is like 16 bytes big and which I could access directly too.

This stack thing is pretty confusing...

#11136 - DekuTree64 - Sat Sep 27, 2003 3:12 am

To store 2 sets and then load the first one, you'd do something like
stmfd sp!, {r3, r10-r12}
@do stuff
stmfd sp!, {r3, r10-r12}
add r0, sp, #16
ldmia r0, {r3, r10-r12} @or ldmfd, different syntax for the same opcode

One more thing, the order you specify the regs for stm/ldm doesn't matter, they're always stored in the order of their numbers, with the higher numbers at higher addresses, wether you're using increment or decrement, so after this, [sp] will hold the value in r3, [sp + 4] will be r10, etc., so it actually goes decrement sp, store r12, dec sp, store r11, and so on. Stmia would go store r3, inc sp, store r10, etc.
Then ldmfd does load r3, inc sp, load r10, inc sp...

Does that clear things up a little?
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku