Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 8 Preserve Environment via Stack
ECE3375B: Microprocessors and Microcomputers Electrical and Computer Engineering Western University
Dr. Leod (Section 1, Dr. (Section 2,
Copyright By PowCoder代写 加微信 powcoder
How to call a subroutine?
How to return the control back to the caller?
How to pass arguments into a subroutine?
How to return a value in a subroutine?
How to preserve the running environment for the caller?
2 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
A Last-In-First-Out memory model
Only allow to access the most recently added item
Also called the top of the stack
Key operations:
push (add item to stack)
pop (remove top item from stack)
3 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Stack Growth Convention:
Ascending vs Descending Memory
Memory Address
0xFFFFFFFF
Stack base
0x00000000
0xFFFFFFFF
Stack base
PUSH Stack grows POP downwards
0x00000000
Descending stack: Stack grows towards low memory address
Stack grows upwards
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Ascending stack: Stack grows towards high memory address
Stack Growth Convention:
Full vs Empty
Stack base
Stack base
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Stack Pointer (SP)
Stack Pointer (SP)
Full stack: SP points to the last item pushed onto the stack
Empty stack: SP points to the next free space on the stack
Cortex-M/A Stack
Cortex-M/A uses full descending stack Example:
Memory Address
0xFFFFFFFF
PUSH/POP {r0,r6,r3}
Stack pointer (SP, aka R13)
decremented on PUSH
SP = SP – 4 * # of registers
incremented on POP
SP = SP + 4 * # of registers
Stack Pointer (SP)
Stack base
SP starts at
0x20000200 for STM32-Discovery by default (can be
stack grow downwards
changed in startup.s)
0x3FFFFFF8 for ARMv7
0x00000000
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Full Descending Stack
High Memory Addresses
Stack base
PUSH {register_list} equivalent to:
STMDB SP!, {register_list}
DB: Decrement Before
POP {register_list} equivalent to:
LDMIA SP!, {register_list}
IA: Increment After
Stack Pointer (SP)
Top of Stack
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Stack grows toward low memory addresses.
Low Memory Addresses
Stack Implementation
Stock Name
Equivalent
Alternative
Equivalent
Alternative
Full Descending(FD)
STMFD SP!,list
STMDB SP!,list
LDMFD SP!,list
LDMIA SP!,list
Empty Descending(ED)
STMED SP!,list
STMDA SP!,list
LDMED SP!,list
LDMIB SP!,list
Full Ascending(FA)
STMFA SP!,list
STMIB SP!,list
LDMFA SP!,list
LDMDA SP!,list
Empty Ascending(EA)
STMEA SP!,list
STMIA SP!,list
LDMEA SP!,list
LDMDB SP!,list
8 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Typical Usage of Stack
Why need stack?
Saving the original contents of processor’s registers at the beginning a subroutine (Contents are restored at the end of a subroutine)
Storing local variables in a subroutine
Passing extra arguments to a subroutine
Saving processor’s registers upon an interrupt
Sound complex? No worry! We will learn them.
9 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
SP = SP-4 ⟶ descending stack (*SP) = Rd ⟶ full stack
Push multiple registers
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.
10 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
PUSH {r6, r7, r8}
They are equivalent.
PUSH {r8, r7, r6}
Rd = (*SP) ⟶ SP = SP + 4 ⟶
Pop multiple registers
full stack Stack shrinks
The order in which registers listed in the register list does not matter.
When popping multiple registers, these registers are automatically sorted by name and the lowest- numbered register is loaded from the lowest memory address, i.e. is loaded first.
11 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
POP {r6, r7, r8}
They are equivalent.
POP {r8, r7, r6}
POP {r6} POP {r7} POP {r8}
Cortex-M/A Stack
PUSH {register list}
POP {register list}
Eligible registers for PUSH: r0 – r12, LR
Eligible registers for POP: r0 – r12, LR, PC
Order specified in the list does not matter
Largest-numbered register is always pushed first and popped last Lowest-numbered register stored at lowest memory address
r10 r11 r12
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
Full Descending Stack
PUSH {r3, r1, r7, r2}
13 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Full Descending Stack
PUSH {r3, r1, r7, r2}
Largest-numbered register is pushed first.
14 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Full Descending Stack
PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2}
15 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Largest-numbered register is pushed first but popped last.
Full Descending Stack
PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2}
Pop to the smallest- numbered register first.
16 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Example: swap R1 & R2
0x11111111
0x22222222
0x20000200
PUSH {R1} PUSH {R2} POP {R1} POP {R2}
0x20000200
0x200001FC
0x200001F8
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: swap R1 & R2
0x11111111
0x22222222
0x200001FC
PUSH {R2} POP {R1} POP {R2}
0x20000200
0x200001FC
0x200001F8
0x11111111
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: swap R1 & R2
0x11111111
0x22222222
0x200001F8
0x20000200
0x200001FC
0x200001F8
0x11111111
0x22222222
POP {R1} POP {R2}
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: swap R1 & R2
0x22222222
0x22222222
0x200001FC
PUSH {R1} PUSH {R2} POP {R1} POP {R2}
0x20000200
0x200001FC
0x200001F8
0x11111111
0x22222222
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: swap R1 & R2
0x22222222
0x11111111
0x20000200
PUSH {R1} PUSH {R2} POP {R1} POP {R2}
0x20000200
0x200001FC
0x200001F8
0x11111111
0x22222222
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Are the values of R1 and R2 swapped? PUSH {R1, R2}
POP {R2, R1}
Answer: No.
But you can:
PUSH {R1, R2}
PUSH {R1} PUSH {R2} POP {R1, R2}
22 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Subroutine
A subroutine, also called a function or a procedure, single-entry, single-exit
Return to caller after it exits
When a subroutine is called, the Link Register (LR) holds the memory address of the next instruction to be executed after the subroutine exits.
23 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Link Register-Repeated for Reference
Link Register (LR) holds the return address of the current subroutine call.
Low Registers
General Purpose Register
High Registers
Special Purpose Register
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Call a Subroutine
Caller Program
Subroutine/Callee
MOV r4, #100 …
ADD r4, r4, #1 ; r4 = 101, not 11
foo PROC …
MOV r4, #10 ; foo changes r4 …
25 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Calling a Subroutine-Repeated for Reference
Step 1: LR = PC + 4 Step 2: PC = label
label is name of subroutine
Compiler translates label to memory address
After call, LR holds return address (the instruction following the call)
26 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Caller Program
MOV r4, #100 …
Subroutine/Callee
foo PROC …
MOV r4, #10 …
Exiting a Subroutine-Repeated for Reference
Caller Program
MOV r4, #100 …
Subroutine/Callee
foo PROC …
MOV r4, #10 …
BX LR ENDP
Branch and Exchange
BX LR PC = LR
27 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
ARM Procedure Call Standard-Repeated for Reference
Subroutine Preserved
Argument 1 and return value
If return has 64 bits, then r0:r1 hold it. If argument 1 has 64 bits, r0:r1 hold it.
Argument 2
Argument 3
If the return has 128 bits, r0-r3 hold it.
Argument 4
If more than 4 arguments, use the stack
General-purpose V1
Variable register 1 holds a local variable.
General-purpose V2
Variable register 2 holds a local variable.
General-purpose V3
Variable register 3 holds a local variable.
General-purpose V4
Variable register 4 holds a local variable.
General-purpose V5
Variable register 5 holds a local variable.
Platform specific/V6
Usage is platform-dependent.
General-purpose V7
Variable register 7 holds a local variable.
General-purpose V8
Variable register 8 holds a local variable.
Intra-procedure-call register
It holds intermediate values between a procedure and the sub-procedure it calls.
Stack pointer
SP has to be the same after a subroutine has completed.
Link register
LR does not have to contain the same value after a subroutine has completed.
Program counter
Do not directly change PC
28 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Caller-saved Registers vs Callee-saved Registers-Repeated for Reference
Caller-saved registers
Low Registers
Callee-saved registers
High Registers
Callee-saved registers
General Purpose Register
Not saved by subroutine Hold arguments/result
Caller expects their values are retained
Callee must save and store it if callee modifies it
Special Purpose Register
Register Bank
Special Registers
ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
Caller Program
Subroutine/Callee
MOV r4, #100 …
ADD r4, r4, #1 ; r4 = 101, not 11
PUSH {r4} ; preserve r4 …
MOV r4, #10 ; foo changes r4 …
POP {r4} ; Recover r4
BX LR ENDP
Caller expects callee does not modify r4! Callee should preserve r4!
30 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
Caller Program
Subroutine/ should save these registers if caller needs to re-use their original values:
• R12 • CPSR
Callee should Preserve
• R4–R11 • R14 (LR) • R13 (SP)
31 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
What is wrong in foo()?
Caller Program
Subroutine foo
Subroutine bar
MOV r4, #100 …
ADD r4, r4, #1
MOV r4, #10 …
BL bar …
POP {r4} BX LR
bar PROC …
BX LR ENDP
32 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
What is wrong in foo()?
Caller Program
Subroutine foo
Subroutine bar
MOV r4, #100 …
ADD r4, r4, #1
MOV r4, #10 …
BL bar …
POP {r4} BX LR
bar PROC …
BX LR ENDP
33 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
What is wrong in foo()? Solution #1
Caller Program
Subroutine foo
Subroutine bar
MOV r4, #100 …
ADD r4, r4, #1
PUSH {r4, LR}
MOV r4, #10 …
POP {r4, LR} BX LR
bar PROC …
BX LR ENDP
34 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Stacks and Subroutines
35 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Preserve Runtime Environment via Stack
What is wrong in foo()? Solution #2
Caller Program
Subroutine foo
Subroutine bar
MOV r4, #100 …
ADD r4, r4, #1
PUSH {r4, LR}
MOV r4, #10 …
POP {r4, PC} BX LR
bar PROC …
BX LR ENDP
36 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Subroutine Calling Another Subroutine
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
Function MAIN
Function QUAD
Function SQ
37 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
SQ MUL R0,R0 BX LR
Subroutine Calling Another Subroutine
QUAD PROC PUSH {LR}
POP {LR} BX LR
ENDL … ENDP
BX LR ENDP
Function MAIN
Function QUAD
Function SQ
38 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II ECE 3375b (A.Reyhani)
Example: R0 = R04
BL QUAD B ENDL
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x20000200
0x08000138
39 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x20000200
0x0800013C
40 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x20000200
0x08000140
0x0800014C
Link Register (LR)
41 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0 BX LR
QUAD PUSH {LR}
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x200001FC
0x08000140
0x08000150
42 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x200001FC
0x08000154
0x08000144
43 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x200001FC
0x08000154
0x08000148
44 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BX LR ENDL …
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x200001FC
0x08000154
0x08000154
45 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0 BX LR
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
0x200001FC
0x08000158
0x08000144
46 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-II
ECE 3375b (A.Reyhani)
Example: R0 = R04
0x08000140
SQ MUL R0,R0
QUAD PUSH {LR} BL SQ
BL SQ POP {LR} BX LR
QUAD PUSH {LR}
0x20000200 0x200001FC 0x200001F8
0x08000138 0x0800013C 0x08000140 0x08000144 0x08000148 0x0800014C 0x08000150 0x08000154 0x08000158 0x0800015C
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com