Content originally posted in LPCWare by MikeSimmonds on Thu Jul 24 10:13:49 MST 2014
The AA7M (Arm Architecture manual version 7M) is the 'cpu' reference and details all the instruction formats and syntaxes. It gives a programmers overview, and more (that aren't of interest to programmers).
It is free from the arm info site. But it is a lot to take at a gulp so maybe google for a starters page.
Anyway, the lr is just another 32-bit register BUT, a bl or blx (branch with LINK) works just like a b (thats branch) of bx but puts the 'return address' in to the ONE AND ONLY link register (aka r14).
So a simple call can end with a simple bx lr (branch to the address in lr).
However, when you make a nested call, your new bl ... will OVERWRITE the ONE AND ONLY link register
with YOUR return address. The nested function returns to YOUR return address, but when you try to return to YOUR caller, its link address has been lost. Totally different scheme to X86, AVR, 8051, etc. etc. But much
more efficient for single level calls.
ARM never stacks/unstacks (pushes/pops) anything unless you (or the "C" compiler) tells it to.
[Interrupts are a different kettle of fish--they do push lots of stuff]
As for pop {...,pc} when you pushed lr. It is a shortcut for the longer sequence pop {...,lr} followed by bx lr.
You don't care at this point about what is actually in the lr, as long as you branch back to where it said.
Indeed (and somewhat dangerously) ANY intruction that updates the pc (r15) will cause a change of execution point. Eg add pc, r7,r0 sub pc, pc, 0x1000, mov pc, any number. DONT DO THIS -- there are gotchas. [1. the pc read in an instruction is here + 8] [2. the actual value has to have the botom bit set! (to indicate THUNB-2 code even though the cortex can't do ARM code -- forget and you get a usage fault.
so to jump to absolute 0x4100 yoy would move pc, 0x4101. You don't normally bother with this as the assember knows about this and (.thumb_func) and deals with it] That was a very long gotcha 2 wasn't it.
Anyway, now you know why you have to save the lr!
Cheers, Mike.