Embedded Systems with ARM Cortex-M Microcontrollers in Assembly Language and C (Dr. )
Chapter 8 Passing Parameters to Subroutines via Registers
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-I
ECE 3375b (A.Reyhani)
Link Register (LR) 32 bits
Low Registers
Link Register (LR) holds the return address of a subroutine
The processor copies LR to PC after the program is finished.
General Purpose Register
High Registers
Link Register
Register Bank
Special Registers
Special Purpose Register
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Link Register (LR)
void foo(void) ;
int main(void{ ●●●
void foo (void) { ●●●
4 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Link Register (LR)
void foo(void) ;
int main(void{ ●●●
void foo (void) { ●●●
BL foo ●●●
Transfer control to callee
ECE 3375b (A.Reyhani)
5 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Link Register (LR)
void foo(void) ;
int main(void{ ●●●
void foo (void) { ●●●
ECE 3375b (A.Reyhani)
BL foo ●●●
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Link Register (LR)
void foo(void) ;
int main(void{ ●●●
void foo (void) { ●●●
0x08000214
0x08000280
0x08000210
0x08000214
0x08000280 foo PROC ● ● ●
BL foo ●●●
Transfer control to callee
ECE 3375b (A.Reyhani)
LR = PCcurrent + 4
PC = of foo()
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Link Register (LR)
void foo(void) ;
int main(void{ ●●●
void foo (void) { ●●●
0x08000214
0x08000214
0x08000210
0x08000214
Resume suspended caller
0x08000280 foo PROC ● ● ●
BL foo ●●●
ECE 3375b (A.Reyhani)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Procedure Call Standard
What is it?
Contract between a calling subroutine (caller) and a called subroutine (callee)
Why need it?
Allows subroutines to be separately written, compiled, assembled but work together Allows C program calls an assembly function, or vice versa
This talk focuses on
How to pass arguments to a subroutine?
How to return a result from a subroutine?
9 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Passing Arguments and Returning Value
R0 R1 R2 R3
32-bit Argument 1
32-bit Argument 2
32-bit Argument 3
64-bit Argument 2
32-bit Argument 4
Extra arguments are pushed to the stack by the caller. The caller is responsible to pop them out of the stack after the subroutine returns.
64-bit Argument 1
128-bit Argument
Subroutine
32-bit Return Value
64-bit Return Value
128-bit Return Value
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
Passing arguments to a subroutine
int32_t sum(uint8_t a8, int8_t b8, uint16_t c16, uint16_t d16);
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
Return the sum in register r0
int32_t sum(uint8_t a8, int8_t b8, uint16_t c16, uint16_t d16);
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Passing Arguments and Returning Value
int32_t sum(uint8_t a8, int8_t b8, uint16_t c16, uint16_t d16); s = sum(1, 2, 3, 4);
Caller Callee
MOVSr0,#1 ;a8 MOVSr1,#2 ;b8 MOVS r2, #3 ; c16 MOVS r3, #4 ; d16 BL sum
ADD r0, r0, r1 ; a8 + b8 ADDr0,r0,r2 ;addc16 ADDr0,r0,r3 ;addd16 BX LR
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Returning Value
int32_t s32;
int32_t sum(uint8_t a8, int8_t b8, uint16_t c16, uint16_t d16);
s32 = sum(1, 2, 3, 4) + 100;
ADDS r0, r0, #100
#1 ; 1st argument a8 #2 ; 2nd argument b8 #3 ; 3rd argument c16 #4 ; 4th argument d16
; result is returned in r0
14 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I ECE 3375b (A.Reyhani)
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
Passing arguments to a subroutine
int64_t sum(int64_t a, int64_t b); Callee
ADDS r0, r2, r0 ; Adding lower 32 bits ADC r1, r3, r1 ; Adding upper 32 bits BX LR ; Return in r1:r0 ENDP
Upper 32 bits
Lower 32 bits
r0 Addend1 r2 Addend2
ECE 3375b (A.Reyhani)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
Returning sum from a subroutine
int64_t sum(int64_t a, int64_t b); Callee
sum[63:32]
ADDS r0, r2, r0 ; Adding lower 32 bits ADC r1, r3, r1 ; Adding upper 32 bits BX LR ; Return in r1:r0 ENDP
Upper 32 bits
Lower 32 bits
r0 Addend1 r2 Addend2
ECE 3375b (A.Reyhani)
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
r13 (SP) r14 (LR) r15 (PC)
Passing arguments to a subroutine
int32_t sum6(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6) sum6(1, 2, 3, 4, 5, 6);
MOVS r0, #5
MOVS r1, #6
PUSH {r0, r1} ; push a5, a6 MOVS r0, #1
MOVS r1, #2
MOVS r2, #3
MOVS r3, #4
ADDS sp, sp, #8 ; pop a4, a6
ADD r0, r0, r1
; sum = a1 + a2
; sum += a3
; sum += a4
ADD r0, r0, r2
ADD r0, r0, r3
LDRD r1, r2, [sp] ; load a5, a6
ADD r0, r0, r1
ADD r0, r0, r2
; sum += a5
; sum += a6
; return in r0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
r13 (SP) r14 (LR) r15 (PC)
sp+8 Passing arguments sp+4
to a subroutine
int32_t sum6(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6) sum6(1, 2, 3, 4, 5, 6);
MOVS r0, #5
MOVS r1, #6
PUSH {r0, r1} ; push a5, a6 MOVS r0, #1
MOVS r1, #2
MOVS r2, #3
MOVS r3, #4
ADDS sp, sp, #8 ; pop a4, a6
ADD r0, r0, r1
; sum = a1 + a2
; sum += a3
; sum += a4
ADD r0, r0, r2
ADD r0, r0, r3
LDRD r1, r2, [sp] ; load a5, a6
ADD r0, r0, r1
ADD r0, r0, r2
; sum += a5
; sum += a6
; return in r0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9
r10 r11 r12
r13 (SP) r14 (LR) r15 (PC)
Passing arguments to a subroutine
int32_t sum6(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, int32_t a6) sum6(1, 2, 3, 4, 5, 6);
MOVS r0, #5
MOVS r1, #6
PUSH {r0, r1} ; push a5, a6 MOVS r0, #1
MOVS r1, #2
MOVS r2, #3
MOVS r3, #4
ADDS sp, sp, #8 ; pop a4, a6
ADD r0, r0, r1
; sum = a1 + a2
; sum += a3
; sum += a4
ADD r0, r0, r2
ADD r0, r0, r3
LDRD r1, r2, [sp] ; load a5, a6
ADD r0, r0, r1
ADD r0, r0, r2
; sum += a5
; sum += a6
; return in r0
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Returning Value
uint32_t s32;
uint32_t sum(uint8_t a8, uint8_t b8, uint16_t c16, uint16_t d16);
s32 = sum(1, 2, 3, 4) + 100;
#1 ; 1st argument a8 #2 ; 2nd argument b8 #3 ; 3rd argument c16 #4 ; 4th argument d16
ADDS r0, r0, #100
LDR r4, =s32 ; Get memory address of s32 STR r0, [r4] ; Save returned result to s32
; result is returned in r0
20 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I ECE 3375b (A.Reyhani)
ARM Procedure Call Standard
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
21 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I ECE 3375b (A.Reyhani)
Callee Saved Registers vs Caller Saved Registers
Callee can freely modify R0, R1, R2, and R3 If caller expects their values are retained, caller should push them onto the stack before calling the callee
Caller expects these values are retained.
If Callee modifies them, callee must restore their values upon leaving the function.
Special Purpose Register
Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Caller Saved Registers
Low Registers
Callee Saved Registers
High Registers
General Purpose Register
ECE 3375b (A.Reyhani)
ARM Procedure Call Standard
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
23 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
Embedded Systems with ARM Cortex-M
Sum of Square: 𝒙𝟐 + 𝒚𝟐 R1: second argument
R0: first argument
int SSQ(int x, int y){ int z;
z = x*x + y*y;
return z; }
R0: Return Value 24 Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
ECE 3375b (A.Reyhani)
MUL R2,…
MUL R3,…
0x08000128
25 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Example: SSQ(3, 4)
MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
ECE 3375b (A.Reyhani)
MUL R2,…
MUL R3,…
0x08000128
26 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Example: SSQ(3, 4)
BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
ECE 3375b (A.Reyhani)
MUL R2,…
MUL R3,…
0x0800012C
27 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134 0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000130
28 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
Address of the next instruction after the branch is saved into LR.
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134 0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x0800013C
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128
0x0800012C
0x08000130
0x08000134 0x08000138
0x0800013C
0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x0800013C
30 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1
ADD R2,R2,R3 MOV R0,R2
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x08000140
31 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1
ADD R2,R2,R3
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x08000144
32 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x08000148
33 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x0800014C
34 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
Copy LR to PC when returning from a subroutine!
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x08000134
35 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128 0x0800012C
0x08000130
0x08000134
0x08000138
0x0800013C 0x08000140
0x08000144
0x08000148
0x0800014C
MUL R2,…
MUL R3,…
0x08000134
0x08000134
36 Embedded Systems with ARM Cortex-M Microcontrollers (Dr. Y. Zhu): Chapter 8-I
ECE 3375b (A.Reyhani)
Example: SSQ(3, 4)
MOV R0,#3 MOV R1,#4 BL SSQ MOV R2,R0 B ENDL …
MUL R2,R0,R0
MUL R3,R1,R1 ADD R2,R2,R3 MOV R0,R2
ENDP ENDL …
R0 R1 R2 R3
0x08000128 0x0800012C
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com