Professional Documents
Culture Documents
CH 9 Mid
CH 9 Mid
l A subroutine is a block of code that is called from different places from within a main program or
other subroutines.
l Saves code space in that the subroutine code does not have to be repeated in the program areas
that need it;
l Only the code for the subroutine call is repeated.
l A subroutine can have
l parameters that control its operation
l local variables for computation.
l A subroutine may pass a return value back to the caller.
l Space in data memory must be reserved for parameters, local variables, and the return value.
Subroutine
Using Subroutines
• When using subroutines we need to know the following:
• Where is the NEXT instruction s address
• How to remember the RETURN address
• Subroutines are based on MPU instructions and use STACK
Stack grows
upwards
Stack base PUSH POP
Stack top
Stack top
0x00000000 0x00000000
• decremented on PUSH
• incremented on POP
Stack
• SP starts at 0x20000600 for Pointer Stack top
PUSH POP
stack grow
downwards
0x00000000
11
Stack
PUSH {Rd}
• SP = SP-4 ⟶ descending stack
• (*SP) = Rd ⟶ full stack
• The order in which registers listed in the register list does not matter.
• When pushing multiple registers, these registers are automatically
sorted by name and the lowest-numbered register is stored to the
lowest memory address, i.e. is stored last.
12
Example: swap R1 & R2
R1 0x11111111
R2 0x22222222
memory
13
Example: swap R1 & R2
R1 0x11111111
R2 0x22222222
memory
14
Example: swap R1 & R2
R1 0x11111111
R2 0x22222222
memory
15
Example: swap R1 & R2
R1 0x22222222
R2 0x22222222
memory
16
Example: swap R1 & R2
R1 0x22222222
R2 0x11111111
memory
17
Quiz Descending SP
Are the values of R1 and R2 swapped?
Highest Memory
PUSH {R1, R2}
PUSH {r2}
POP {R2, R1} PUSH {r1}
Lowest Memory
18
Quiz Descending SP
Are the values of R1 and R2 swapped?
Highest Memory
PUSH {R1, R2} ; Push R2 then R1 PUSH {r2}
POP {R2, R1} ; POP R1 then R2 PUSH {r1}
Lowest Memory
Answer: No.
19
Back to Subroutine
• In general, a subroutines, also called a function or a procedure,
• single-entry, single-exit
• Return to caller after it exits
• In ARM:
• When a subroutine is called, the Link Register (LR) holds the memory address
of the next instruction to be executed after the subroutine exits.
20
Link Register
32 bits
21
Call a Subroutine
• Notes: Subroutine/Callee
• label is name of subroutine foo PROC
...
• Compiler translates label to memory address MOV r4, #10
• After call, LR holds return address (the ...
instruction following the call) BX LR
ENDP
Subroutine/Callee
foo PROC
...
MOV r4, #10
...
BX LR
ENDP
24
ARM Procedure Call Standard Subroutine
Register Usage Notes
Preserved
If return has 64 bits, then r0:r1 hold it. If argument 1 has
r0 Argument 1 and return value No
64 bits, r0:r1 hold it.
r1 Argument 2 No
r2 Argument 3 No If the return has 128 bits, r0-r3 hold it.
r3 Argument 4 No If more than 4 arguments, use the stack
r4 General-purpose V1 Yes Variable register 1 holds a local variable.
r5 General-purpose V2 Yes Variable register 2 holds a local variable.
r6 General-purpose V3 Yes Variable register 3 holds a local variable.
r7 General-purpose V4 Yes Variable register 4 holds a local variable.
r8 General-purpose V5 YES Variable register 5 holds a local variable.
r9 Platform specific/V6 No Usage is platform-dependent.
r10 General-purpose V7 Yes Variable register 7 holds a local variable.
r11 General-purpose V8 Yes Variable register 8 holds a local variable.
It holds intermediate values between a procedure and
r12 (IP) Intra-procedure-call register No
the sub-procedure it calls.
r13 (SP) Stack pointer Yes SP has to be the same after a subroutine has completed.
LR does not have to contain the same value after a
r14 (LR) Link register No
subroutine has completed.
r15 (PC) Program counter N/A Do not directly change PC
25
Link Register
32 bits
R0
Scratch registers, not saved by
R1 subroutine. Hold
R2 arguments/result
Low R3
Registers
R4
R5
R6
General
Purpose
Callee must save them.
R7
Register
Caller expects these values
R8 are retained .
R9
High
32 bits
Registers R10
R11 xPSR
Link Register R12 BASEPRI
Special
R13 (SP) R13 (MSP) R13 (PSP) PRIMASK Purpose
Register
R14 (LR) FAULTMASK
R15 (PC) CONTROL
26
Preserve Runtime Environment via Stack
Caller Program Subroutine/Callee
foo PROC
MOV r4, #100 PUSH {r4} ; preserve r4
... ...
BL foo MOV r4, #10 ; foo changes r4
... ...
ADD r4, r4, #1 ; r4 = 101, not 11 POP {r4} ; Recover r4
BX LR
ENDP
27
Stacks and Subroutines
Where is the ADD INSTRUCTION
Return Address saved?
ADD INSTRUCTION
Stacks and Subroutines Saving PC1+4: ADD INSTRUCTION
ADD INSTRUCTION
Subroutine Calling Another Subroutine
Function SQ
Function MAIN Function QUAD
30
Subroutine Calling Another Subroutine
Function QUAD
31
Example: R0 = R04 Descending Stack
BX LR B ENDL 0x08000140
SP 0x20000200
SQ MUL R0,R0 0x08000144
LR
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000138
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
32
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x200001FC
B ENDL 0x200001F8
R0 0x02
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x20000200
SQ MUL R0,R0 0x08000144
LR
BX LR 0x08000148
QUAD PUSH {LR} PC 0x0800013C
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
33
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x200001FC
B ENDL 0x200001F8
R0 0x02
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x20000200
SQ MUL R0,R0 0x08000144
LR 0x08000140
BX LR 0x08000148
QUAD PUSH {LR} PC 0x0800014C
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
Preserve POP {LR} 0x08000158
BX LR Link Register (LR) BX LR 0x0800015C
ENDL ...
34
Example: R0 = R04
MOV R0,#2 Note: xxxxxxxx 0x20000200
BL QUAD The address is saved 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x02
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000140
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000150
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
35
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x02
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000154
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000144
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
36
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x04
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000154
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000148
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
37
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x04
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000154
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000154
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
38
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x04
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000158
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000144
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
39
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x10
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000158
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000148
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
40
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x10
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x200001FC
SQ MUL R0,R0 0x08000144
LR 0x08000158
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000158
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
41
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x10
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x20000200
SQ MUL R0,R0 0x08000144
LR 0x08000140
BX LR 0x08000148
QUAD PUSH {LR} PC 0x0800015C
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
Recover POP {LR} 0x08000158
BX LR Link Register (LR) BX LR 0x0800015C
ENDL ...
42
Example: R0 = R04
MOV R0,#2 xxxxxxxx 0x20000200
BL QUAD 0x08000140 0x200001FC
B ENDL 0x200001F8
R0 0x10
MOV R0,#2 0x08000138
SQ
MUL R0,R0 BL QUAD 0x0800013C
BX LR B ENDL 0x08000140
SP 0x20000200
SQ MUL R0,R0 0x08000144
LR 0x08000140
BX LR 0x08000148
QUAD PUSH {LR} PC 0x08000140
QUAD PUSH {LR} 0x0800014C
BL SQ
BL SQ 0x08000150
BL SQ
BL SQ 0x08000154
POP {LR}
POP {LR} 0x08000158
BX LR
BX LR 0x0800015C
ENDL ...
43
Initializing the stack pointer (SP)
• Before using the stack, software has
to define stack space and initialize
the stack pointer (SP).
• The assembly file startup.s defines
stack space and initialize SP.
44
STACK
Example: R0 = R04 R0
8000140
0x20000200
0x200001FC
0x200001F8