Writing ARM and Thumb Assembly Language
ARM DUI 0068B
Copyright © 2000, 2001 ARM Limited. All rights reserved.
2-43
Stacking registers 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 safely be made without causing the return address to be lost. If you do this, you
can also return from a subroutine by popping the pc off the stack at exit, instead of
popping lr and then moving that value into the pc. For example:
subroutine STMFD sp!, {r5-r7,lr} ; Push work registers and lr
; code
BL somewhere_else
; code
LDMFD sp!, {r5-r7,pc} ; Pop work registers and pc
Note
Use this with care in mixed ARM and Thumb systems. In ARM architecture v4T
systems, you cannot change state by popping directly into the program counter.
In ARM architecture v5T and above, you can change state in this way.
See the
Interworking ARM and Thumb
chapter in
ADS Developer Guide
for further
information on mixing ARM and Thumb.