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 > What is r7 used for?

#18882 - niallz - Wed Apr 07, 2004 7:15 pm

Hi,

I'm just doing some work here, and I've noticed that GCC uses r7 (in thumb mode) when entering and exiting a function for saving the stack pointer. Does anyone know why it does this?

Thanks

Niall

#18885 - Lupin - Wed Apr 07, 2004 7:29 pm

because you can not access the other registers in thumb mode (only from r0-r7)
_________________
Team Pokeme
My blog and PM ASM tutorials

#18887 - jma - Wed Apr 07, 2004 7:47 pm

This is done because the SP register can change (allocating local variable memory on the stack). However, parameters being passed are always going to be on the stack, minus the ones already in registers. These are accessed by the compiler using R7 -- the original position of the stack.

Jeff
_________________
massung@gmail.com
http://www.retrobyte.org

#18888 - poslundc - Wed Apr 07, 2004 7:52 pm

GCC is most likely using r7 as a frame pointer. According to the ARM procedure call standards, any of r4 to r7 can be used as a frame pointer when in Thumb mode. Frame pointers aren't really a necessity, though, so I can't say for certain why it's implementing it.

And Lupin, you are incorrect.

Edit: I am more-or-less saying the same thing that Jeff is... frame pointers are most useful in variadic routines, but I'm not certain why GCC would generate them for non-variadic routines.

Dan.

#18892 - Lupin - Wed Apr 07, 2004 8:15 pm

Uhm, why does GCC use r13 for all other functions than thumb functions?

Sorry for posting wrong answer...
_________________
Team Pokeme
My blog and PM ASM tutorials

#18895 - poslundc - Wed Apr 07, 2004 8:57 pm

Lupin wrote:
Uhm, why does GCC use r13 for all other functions than thumb functions?


r13 is the stack pointer, not the frame pointer. It is used as the stack pointer in Thumb mode as well as well as ARM mode.

Quote:
Sorry for posting wrong answer...


You can access all of the registers in Thumb mode. You just can't access all of the registers with every instruction. Please look into these things before you post your answers with such certainty.

Dan.

#18908 - torne - Thu Apr 08, 2004 12:42 am

It uses R7 as the frame pointer in Thumb mode, yes. The reason it generates one for non-variadic functions is because you have not specified -fomit-frame-pointer. Debugging code without a frame pointer can be more difficult on some architectures; I've not used gdb on ARM so no idea if this is the case.