DESN2000: Engineering Design & Professional Practice (EE&T)
Functions, subroutines and procedural call standard
School of Electrical Engineering & Telecommunications Graduate School of Biomedical Engineering
Copyright By PowCoder代写 加微信 powcoder
Biomedical Microsystems Lab
• Function call
• Stack operations
• ARM architecture procedure call standard (AAPCS)
• Function call examples
© 2022 UNSW Sydney
Functions & subroutines
• Advantages:
• Modularise the system. • Divide-and-conquer.
• A subroutine that returns a value is called a function.
Main%Task%
Sub%Task%2% Sub%Task%3%
Sub%Task%1.2%
Sub%Task%1%
Sub%Task%1.1%
Main %xxxxx% % %xxxxx%
% %BL%sub1% % %BL%sub2% % %BL%sub3%
sub1% %xxxxx%
% %BL%sub1.1%
% %BL%sub1.2% %
sub2% %xxxxx% %
sub3% %xxxxx% %
sub1.1 %xxxxx% sub1.2 %xxxxxx%
© 2022 UNSW Sydney
Functions and subroutines
• Need to:
1. Save and restore information between function / subroutine calls.
2. Pass information (arguments and return values) to / from subroutines.
• This is achieved via stacks.
• Need to follow rules when writing subroutines, e.g. how to handle register conflicts?
• ARM Architecture Procedure Call Standard (AAPCS) specifies these rules so that subroutines developed by different programmers can talk to each other.
• Functions/subroutines you develop should be AAPCS compliant.
© 2022 UNSW Sydney
• Special area in the memory with:
Has Last-In-First-Out (LIFO) data structure, with two operations: • PUSH
Variable length
Fixed starting address
• These operations typically happen at word level.
• Stack pointer (SP or R13) holds the address of either the top-most empty entry or top- most last filled entry in the stack.
• Each processor mode has its own stack pointer.
i.e. each mode has a different stack in memory
© 2022 UNSW Sydney
Data is PUSHED on to the stack using STR or STM (store multiple) instructions with stack pointer as the base register.
Data is POPPED from the stack using LDR and LDM (load multiple) instructions with stack pointer as the base register.
Upon each PUSH or POP, the stack pointer is updated to point to
either the next empty entry or the last filled entry
PUSH% POP% Stack%Pointer%
© 2022 UNSW Sydney
Stack: operations
• Load multiple:
• Store multiple:
opcode IA, IB, DA, DB Reg, usually R13 EQ, NE, etc
>{cond >{cond
> {ˆ} > {ˆ}
}
Stack frame
Temporaries Caller save reg (A1 – A4) Link reg (LR)
Extra arguments (≥ 5th)
Callee save reg (V1 – V8) Temporaries
Caller save reg (A1 – A4)
Func2() Stack frame
Stack pointer advances with function call, rewinds when returning.
© 2022 UNSW Sydney
C to assembly example
• Translate the following C code to assembly
int sumSquare(int x, int y) {
return mult(x, x) + y; }
• ARM assembly
STR LR, [SP, #-4]! STR A2, [SP, #-4]! MOV A2, A1
LDR A2, [SP], #4
ADD A1, A1, A2
LDR LR, [SP], #4
MOV PC, LR
; save return addr
; save A2 (y)… used locally ; setting up mult(x,x)
; call mult func
; restore y
; mult() + y
; get return addr
• Note: arguments passed via registers (A1 and A2).
© 2022 UNSW Sydney
Assembly example 1
• Translate this C code into assembly:
int Doh(int i, int j, int k, int l) {
Four arguments, A1 – A4
return i + j + l; }
• Assembly:
Doh ADD A1, A1, A2 ADD A1, A1, A4
MOV PC, LR
Return in A1
• Easiest case. Everything can be done using Ax registers and no further function call.
© 2022 UNSW Sydney
Assembly example 2
• Translate this C code into assembly:
int Doh(int i, int j, int k, int m, char c, int n) { return i + j + n; Return in A1
Six arguments: A1 – A4,
• Assembly:
Doh LDR IP, [SP, #4] ; get n
Not a POP op. Reading the argument from
caller’s frame.
ADD A1, A1, A2
ADD A1, A1, IP
MOV PC, LR
• Like example 1 but needs to pass 5th and 6th arguments via stack.
Decreasing address
© 2022 UNSW Sydney
Assembly example 3
Translate this C code into assembly:
int Doh(int i, int j, int k, int m, int c, int n) {
return Mult(i, j) + n;
Return in A1
Six arguments: A1 – A4,
i, j, k, m, c, n i*j + n
Nested function call. Doh() needs to: 1. Save LR on stack
2. Save register(s) used locally
3. Load the 6th arg n from stack
4. Do work, including calling Mult() 5. Reverse steps 2 – 1.
6. Return with result in A1.
Decreasing address
SP@ ’s caller
© 2022 UNSW Sydney
Assembly example 3
• Translate this C code into assembly:
int Doh(int i, int j, int k, int m, int c, int n) {
Six arguments: A1 – A4,
return Mult(i, j) + n;
Doh SUB SP, SP, #8 STR LR, [SP, #4] STR V1, [SP, #0]
Return in A1
; store LR
; store V1
LDRV1,[SP,#12] ;V1:=arg#6(n) BL Mult
ADD A1, A1, V1
LDR V1, [SP, #0]
LDR LR, [SP, #4]
ADD SP, SP, #8
MOV PC, LR
Decreasing address
SP@ ’s caller
© 2022 UNSW Sydney
Assembly example 3
• Translate this C code into assembly:
int Doh(int i, int j, int k, int m, int c, int n) { return Mult(i, j) + n; Return in A1
• Same thing but shorter
Doh STMFD SP!, {LR, V1}
LDRV1,[SP,#12] ;V1:=arg#6(n) BL Mult
ADD A1, A1, V1
Six arguments: A1 – A4,
LDMFD SP!, {LR, V1} MOV PC, LR
SP@ ’s caller LR Doh()’s
V1 Mult()’s
Decreasing address
© 2022 UNSW Sydney
Assembly example 4
• Translate this C code into assembly:
int main() {
int i, j, k, m;
i = mult(j, k);
m = mult(i, i);
int mult(int mcand, int mlier) { int product = 0;
while (mlier > 0) {
product += mcand;
mlier -= 1; }
return product; }
© 2022 UNSW Sydney
Assembly example 4
• Translate this C code into assembly:
int main() {
int i, j, k, m;
i = mult(j, k);
m = mult(i, i);
For mult():
• Does not call another function. Do
not need to save LR.
• A1– A4 suffices for body of mult()… no register needs to be saved.
• Return value placed in A1.
For main():
• Returns to the operating system. So LR needs to be saved before calling mult().
• Set up arguments for two mult() calls.
int mult(int mcand, int mlier) {
int product = 0;
while (mlier > 0) { •
product += mcand;
mlier -= 1; }
return product; }
© 2022 UNSW Sydney
Assembly example 4
• Assembly code for mult():
MOV A3, #0 CMP A2, #0 BEQ mult_fin ADD A3, A3, A1 SUB A2, A2, #1 B mult_loop MOV A1, A3 MOV PC, LR
; mlier > 0?
; prod += mcand ; mlier -= 1
; a1 = prod
© 2022 UNSW Sydney
Assembly example 4
• Assembly code for main():
main STR LR, [SP, #-4]! ; ; i=V1 j=V2 k=V3 m=V4
store ret addr
MOV A1, V2
MOV A2, V3
MOV V1, A1
MOV A1, V1
MOV A2, V1
MOV V4, A1
MOV A1, #0
LDR LR, [SP], #4
MOV PC, LR
; arg1=j ; arg2=k
; i = mult()
; arg1=i ; arg2=i
; m = mult()
; main() ret value ; restore ret addr
© 2022 UNSW Sydney
Assembly example 5
• Translate this C code into assembly:
int main() { int a = 42;
return 0; }
• Assembly code:
printf(“The meaning of life is %d\n”, a);
… Two args A1 (pointer to string) A2 (integer)
main STR LR, [SP, #-4]! ; save LR
LDR A1, =str
MOV A2, #42
BL printf
MOV A1, #0
LDR LR, [SP], #4
MOV PC, LR
; set up args
; main() ret value ; restore LR
str DCB “The meaning of life is %d\n”, 0
© 2022 UNSW Sydney
• Function call
• Stack operations
• ARM architecture procedure call standard (AAPCS)
• Function call examples
In Moodle:
• Start working on Lab (Due: end of your 3-hr lab)
• Start doing Week 5 exercise
© 2022 UNSW Sydney
References
[1] , ARM Assembly Language: Fundamentals and Techniques, CRC Press, 2015 (2nd Edition).
[2] ARM Architecture Reference Manual.
© 2022 UNSW Sydney
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com