Returning from a subroutine is simply jump/branch to the return address:
function f1( ) function f2( ) // Subroutine { +------->{ ... | ... | ... | ... | ... call | ... | f2( ) ----------+ ... | (Ret Addr) ... <--------------+ ... | ... | ... V ... +------ return; // Branch to the return address } } |
Translate the function call f2( ) in ARM assembler:
|
Quiz: what instruction will cause function f2( ) to branch to the return address (Ret Addr):
f1: f2: // Subroutine ... +-------> ... ... | ... | LR = (Ret Addr) ... | ... | ... call | ... | bl f2 ----------+ ... | (Ret Addr) ... <--------------+ ... | ... | ... V ... +------ return; // Branch to the return address } } |
Hints:
|
Answer: mov pc, lr !!!
f1: f2: // Subroutine ... +-------> ... ... | ... | LR = (Ret Addr) ... | ... | ... call | ... | bl f2 ----------+ ... | (Ret Addr) ... <--------------+ ... | ... | ... V ... +------ mov pc,lr // Branch to the return address } } |
Hints:
|
Function call expressed in a high level programming language:
main( ) void myMethod( ) { { ... ... myMethod( ); ... ... ... ... } ... myMethod( ); ... ... } |
How function calls are implemented in assembler:
main: myMethod: ... ... ... ... bl myMethod ... ... ... ... mov pc, lr ... bl myMethod ... ... |
In the next few slides, I will show a concrete example (using concrete memory addresses how the bl instruction and the mov pc, lr instruction together implements the function call + return mechanis
Let's execute the program starting at main: using some specific return addresses:
main: myMethod: PC--> ... ... ... ... bl myMethod ... 8000: ... ... ... mov pc, lr ... bl myMethod 9000: ... ... |
LR = ...
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The main program will reach the first bl myMethod instruction:
main: myMethod: ... ... ... ... PC--> bl myMethod ... 8000: ... ... ... mov pc, lr ... bl myMethod 9000: ... ... |
LR = ...
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The bl myMethod instruction will save 8000 in the LR register and branch to myMethod:
main: myMethod: ... PC--> ... ... ... bl myMethod ... 8000: ... ... ... mov pc, lr ... bl myMethod 9000: ... ... |
LR = 8000
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The myMethod execution will reach the instruction mov pc, lr:
main: myMethod: ... ... ... ... bl myMethod ... 8000: ... ... ... PC--> mov pc, lr ... bl myMethod 9000: ... ... |
LR = 8000 <---- mov pc, lr will branch to address 8000 !!!
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
Result of executing the mov pc, lr instruction:
main: myMethod: ... ... ... ... bl myMethod ... PC--> ... ... ... mov pc, lr ... bl myMethod 9000: ... ... |
LR = 8000
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The main program will reach the second bl myMethod instruction:
main: myMethod: ... ... ... ... bl myMethod ... 8000: ... ... ... mov pc, lr ... PC--> bl myMethod 9000: ... ... |
LR = 8000
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The bl myMethod instruction will now save 9000 in the LR register and branch to myMethod:
main: myMethod: ... PC--> ... ... ... bl myMethod ... 8000: ... ... ... mov pc, lr ... bl myMethod 9000: ... ... |
LR = 9000
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
The myMethod execution will reach the instruction mov pc, lr:
main: myMethod: ... ... ... ... bl myMethod ... 8000: ... ... ... PC--> mov pc, lr ... bl myMethod 9000: ... ... |
LR = 9000 <---- mov pc, lr will branch to address 9000 !!!
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s
Result of executing the mov pc, lr instruction:
main: myMethod: ... ... ... ... bl myMethod ... 8000: ... ... ... mov pc, lr ... bl myMethod PC--> ... ... |
LR = 9000 --- Demo time....
DEMO: /home/cs255001/demo/asm/8-sub/bl+rts.s