| |||
| Home > Writing ARM Assembly Language > Stack operations for nested subroutines | |||
Stack operations are very useful at subroutine entry and exit. At the start of a subroutine, any working registers required can be stored on the stack, and at exit they can be popped off again.
In addition, if the link register is pushed onto the stack at entry, additional subroutine calls can be made safely without causing the return address to be lost. If you do this, you can also return from a subroutine by popping PC off the stack at exit, instead of popping LR and then moving that value into PC. For example:
subroutine PUSH {r5-r7,lr} ; Push work registers and lr
; code
BL somewhere_else
; code
POP {r5-r7,pc} ; Pop work registers and pc
Use this with care in mixed ARM and Thumb systems. In ARMv4T
systems, you cannot change state by popping directly into PC. In
these cases you must pop the address into a temporary register and
use the BX instruction.
In ARMv5T and later, you can change state in this way.