CS代考 DESN2000: Engineering Design & Professional Practice (EE&T)

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
> {ˆ} > {ˆ}
} {!}, < register_list } {!}, < register_list • Advantages of using LDM / STM over LDR / STR for multiple data transfers: • Reduced code size. • Faster execution – only one instruction fetched from the memory. • With LDM / STM: • Stack pointer (R13) should not be used in register_list. • Cannot have LR and PC in the register_list at the same time. e.g. R0-R5 Write back © 2022 UNSW Sydney Stack: different types • Stack types: • Descending / Ascending: whether the stack grows downwards (address decreases as stack grows) or upwards (address increases as stack grows). • Full / Empty: whether the stack pointer points to the last filled element or next empty space on the stack. • Four stack types: 1. Full Descending (FD) 2. Full Ascending (FA) 3. Empty Descending (ED) 4. Empty Ascending (EA) This is the default stack type • These suffixes can be used as addressing modes in LDM / STM instructions. • LDMFD (load multiple, full descending) is equivalent to LDMIA (load multiple, increment after). © 2022 UNSW Sydney Stack: different types This is the default stack type Full/Descending* Empty/Descending* (FD)* (ED)* Subroutines - Stack Empty/Ascending* (EA)* Full/Ascending* Stack Type Full descending STMFD (STMDB) LDMFD (LDMIA) Full ascending STMFA (STMIB) LDMFA (LDMDA) Empty descending STMED (STMDA) LDMED (LDMIB) Empty ascending STMEA (STMIA) LDMEA (LDMDB) • Pick one type and use (push / pop) operations consistently. Current SP © 2022 UNSW Sydney Current SP Increasing Increasing Decreasing*address* Decreasing*address* Increasing*address* Increasing*address* Function call basics • Two parties: Caller, Callee. • Functions have return values, subroutines do not. • Caller has to set up the arguments. • Callee has to set up the return value. • Function call: • Achieved using BL (branch & link). • BL automatically saves the current program counter (PC) in the link register (LR). • Returning is achieved by MOV MOV PC, LR which restores PC with the saved LR value. 2: MOV PC, LR " Set"up"arguments" " Branch"to"callee" " Access"the"result" " xxxxx" xxxxx" " Access"arguments" " Perform"func6on"body" " Set"up"return"value" " Return"back"to"caller" © 2022 UNSW Sydney Nested function calls • More complicated situation: Func1 → Func2, then Func2 → Func3. • If function calls another function: • Everything before applies. • Plus saving LR on the stack in Func2, before calling Func3, so that we can return up the calling chain. Func1& Func2& " Save"LR"on"the"stack" " " MOV"PC,"LR" " " Save"LR"on"the"stack" " " Restore"LR"from"the" stack" MOV"PC,"LR" Nested& Func+on&Calls& © 2022 UNSW Sydney Function calls & register conflicts • Only 15 registers sharable between caller and callee. • Caller needs registers to pass arguments. Callee needs registers to return values. • Callee should not corrupt any registers that caller might use after the function call. • To help resolve these conflicts, register groups are defined with usage rules: • Scratch / argument: A1 – A4 • Local variables: V1 – V8 User/System Supervisor Interrupt -IRQ Fast Interrupt - FIQ R13_ ABORT R13 _UNDEF R14_ ABORT R14 _UNDEF © 2022 UNSW Sydney Function calls & register conflicts • Optionally save A1 – A4 on stack if it wants to use them after the function call (callee might corrupt them). These are caller-save registers. • Uses A1 – A4 to pass arguments to callee. • Additional arguments are passed via the stack. • Saves LR before BL. Because LR holds the return address, and BL modifies LR. • Uses A1 to transfer the return value. • Save V1 – V8 on stack if it uses them, then restore before returning (Caller assumes these are unchanged on return). These are callee-save registers. • Return to caller by performing MOV PC, LR. © 2022 UNSW Sydney Function calls & register conflicts Which registers may have changed after the BL instruction? • A1 – A4 can be changed. Callee func2 can modify A1 – A4 at any time and A1 is used to transfer the return value. • V1 – V8 remain unchanged. If callee func2 uses V1 – V8, it has to save them on stack and restore the original content prior to returning. • SP remains unchanged. Callee’s stack frame is removed when returning. • LR has changed. • IP (R12) can be changed. This is a scratch register. © 2022 UNSW Sydney AAPCS register conventions The foregoing convention is specified by the ARM Architecture Procedure Call Standard. Register name Software name Usage R0 – R3 A1 – A4 First 4 int arguments Scratch registers Function results Local variables Static variable base Stack limit Frame pointer Intra-procedure call scratch register Stack pointer Return address Program counter static int x = 0; Memory is finite, stack size is limited Pointer to local variables on the stack Not saved by caller nor restored by callee Blue: software convention (programmer’s responsibility) Red: Hardware assisted © 2022 UNSW Sydney Setting up function calls • Generalizing, every function must perform 3 tasks: Create a stack frame to » backup registers that conflict with the caller (i.e. used by both caller and callee) » Save the LR, if the current function calls another function » pass arguments (if A1 – A4 insufficient) Perform operations (function body). Remove the stack frame and return to caller. © 2022 UNSW Sydney Stack operations for function calls Example: using a full-descending (FD) stack, save V1, V2 and LR to memory. Creating stack frame • Version 1 SUB SP, SP, #12 STR LR, [SP, #8] STR V2, [SP, #4] STR V1, [SP, #0] ; reserve space for 3 registers ; save LR Removing stack frame • Version 1 LDR V1, [SP, #0] LDR V2, [SP, #4] LDR LR, [SP, #8] ADD SP, SP, #12 • Version 2 – using store multiple decrement-before STMDB SP!, {V1,V2,LR} Decreasing address • Version 2 – using load multiple increment-after LDMIA SP!, {V1, V2, LR} ; restore V1 ; restore V2 ; restore LR ; remove space © 2022 UNSW Sydney Stack operations for function calls Example: using a full-descending (FD) stack, save V1, V2 and LR to memory. Creating stack frame Removing stack frame • Version 3 LDR V1, [SP], #4 LDR V2, [SP], #4 LDR LR, [SP], #4 STR LR, [SP, #-4]! STR V2, [SP, #-4]! STR V1, [SP, #-4]! ; restore V1 ; restore V2 ; restore LR Post-indexed increment of SP... Read mem then add 4 to address Pre-indexed decrement of SP... Subtract address by 4 then write mem © 2022 UNSW Sydney Basic assembly structure of a function • Template for function: Function name Save stack frame STMDB SP!, {V1, V2, ..., LR} ; creating stack frame xxxxxx Do something ; function body LDMIA SP!, {V1, V2, ..., LR} ; removing stack frame MOV PC, LR Restore stack frame ; return to caller Return to caller Can also be written as: LDMIA SP! { V1, V2, ... PC } ... Restore LR into PC directly • Expand this structure for more complicated cases • When calling additional functions (perhaps restoring A1 ~ A4 after callee returns). • Passing more than 4 integer arguments (put args 5th onwards on the stack). © 2022 UNSW Sydney Summary of stack frame Func1() called Func2(), and processor is executing Func2(). Stack frame might look like the following, in the most general case: Main memory Func1() Callee save reg (V1 – V8) Needed if using any of V1 - V8 If 8 registers aren’t enough, swap to/from mem Might need if calling another function (passing arg(s) and/or getting return value) Needed if calling another function Might need if callee takes > 4 integer arguments
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