Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - I

Similar presentations


Presentation on theme: "COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - I"— Presentation transcript:

1 COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - I http://www.cse.unsw.edu.au/~cs3221 April, 2004 Modified from notes by Saeid Nooshabadi Saeid@unsw.edu.au

2 COMP3221 lec15-function-I.2 Saeid Nooshabadi Overview °C functions °Bookkeeping for function call/return °Instruction support for functions °Nested function calls °C memory allocation: static, heap, stack °Conclusion

3 COMP3221 lec15-function-I.3 Saeid Nooshabadi Review °HLL decisions (if, case) and loops (while, for) use same assembly instructions Flag Setting Instructions: cmp, cmn, tst, teq in ARM Data Processing Instructions with Flag setting Feature: adds, subs, ands, in ARM Conditional branches: beq, bne, bgt, blt, etc in ARM Conditional Instructions: addeq, ldreq,etc in ARM Unconditional branches: b, bal, and mv pc, Rn in ARM Switch/Case: chained if-else or jump table + ldr pc, [ ] ldr pc, [ ] is VERY POWERFUL!

4 COMP3221 lec15-function-I.4 Saeid Nooshabadi Review: Branches: PC-relative addressing °Recall register r15 in the machine also called PC; °points to the currently executing instruction °Most instruction add 4 to it. (pc increments by 4 after execution of most instructions) °Branch changes it to a specific value °Branch adds to it 24-bit signed value (contained in the instruction) Shifted left by 2 bits °Labels => addresses memory 0: FFF... registers r14 r0 r15 = pc beq address b address –32MB +32MB 24 bits

5 COMP3221 lec15-function-I.5 Saeid Nooshabadi C functions main(void) { 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 = product + mcand; mlier = mlier -1; } return product; } What information must compiler/program keep track of?

6 COMP3221 lec15-function-I.6 Saeid Nooshabadi Basics of Function Call...(use regs) set up args jump to function access args... compute result......(use regs) set up return value jump back to caller Caller Callee access result...(use regs)

7 COMP3221 lec15-function-I.7 Saeid Nooshabadi Function Call Bookkeeping Registers for functions  lr = r14  a1, a2, a3, a4  a1  v1, v2,v3,v4,v5,v6,v7 °Procedure address °Return address °Arguments °Return value °Local variables °Registers (conflicts) =>ARM Procedure Call Standards (APCS) conventions for use of registers simplify bookkeeping

8 COMP3221 lec15-function-I.8 Saeid Nooshabadi APCS Register Convention: Summary register namesoftware nameuse and linkage r0 – r3a1 – a4 first 4 integer args scratch registers integer function results r4 – r11v1- v8local variables r9sbstatic variable base r10slstack limit r11fpframe pointer r12ipintra procedure-call scratch pointer r13spstack pointer r14lrreturn address r15pcprogram counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

9 COMP3221 lec15-function-I.9 Saeid Nooshabadi Instruction Support for Function Call?... sum(a,b);... /* a,b:v1,v2 */ } int sum(int x, int y) { return x+y; } address 1000 mov a1,v1 ; x = a 1004 mov a2, v2 ; y = b 1008 mov lr,#1016 ; lr = 1016 1012 b sum ; jump to sum 1016... 2000 sum: add a1,a1,a2 2004 mov pc, lr C ARMARM Why mov pc, lr vs. b 1016 to return? ; b 1016

10 COMP3221 lec15-function-I.10 Saeid Nooshabadi b vs mov pc, lr : why jump register? °Consider a function foo that is called from several different places in your program each call is performed by a b instruction -b foo but the return jump goes to many places main: b bar... b foo... bar:... b foo... foo:... b foo... mov pc, lr

11 COMP3221 lec15-function-I.11 Saeid Nooshabadi A Level of Indirection °Solves many problems in CS! °How do you make a jump instruction behave differently each time it is executed? indirection mov pc, lr returns control to last caller -recorded in a register °How do you make an instruction fetch a different element of an array each time though a loop? indirection update index address register of ldr, and str

12 COMP3221 lec15-function-I.12 Saeid Nooshabadi Accessing array elements => indirection int sumarray(int arr[]) { int i, sum; for(i=0;i<100;i=i+1) sum = sum + arr[i]; } movv1, #0; clear v0 adda2,a1,#400 ; beyond end of arr[] Loop:cmpa1,a2 bgeExit ldra3, [a1], #4 ; a3=arr[i], a1++ addv1,v1,a3 ; v1= v1+ arr[i] bLoop Exit:mov lr, pc

13 COMP3221 lec15-function-I.13 Saeid Nooshabadi Instruction Support for Functions? °Single instruction to branch and save return address: branch and link ( bl ): °Before: 1008 mov lr, #1016 ;lr = 1016 1012 b sum ;goto sum °After: 1012 bl sum ; lr = 1016,goto sum °Why bl ? Make the common case fast and elegance

14 COMP3221 lec15-function-I.14 Saeid Nooshabadi Nested Procedures (#1/2)...sumSquare(a,b)... int sumSquare(int x, int y) { return mult (x,x)+ y; } °Need to save sumSquare return address saved in lr by bl sumSquare instruction, before call to mult Otherwise bl mult overwrites lr °One word per procedure in memory ? e.g., str lr, [sp,sumSquareRA]; sp = r13 °Recursive procedures could overwrite saved area => need safe area per function invocation => stack

15 COMP3221 lec15-function-I.15 Saeid Nooshabadi Nested Procedures (#2/2) °In general, may need to save some other info in addition to lr. °When a C program is run, there are 3 important memory areas allocated: Static: Variables declared once per program, cease to exist only after execution completes Heap: Variables declared dynamically (such as counters in for loops, or by function malloc() ) Stack: Space to be used by procedure during execution; this is where we can save register values

16 COMP3221 lec15-function-I.16 Saeid Nooshabadi “What’s This Stuff Good For?” Breathing Observation Bubble: BOB pipes air from a tank under the handlebars into an acrylic dome, replacing a diver's face mask and breathing apparatus. Wireless technology lets riders talk to other BOBsters darting through the water nearby, as well as to armchair divers above in a boat or back on shore. Saving energy from not having to kick, divers can stay submerged almost an hour with the BOB. Like most modern scuba gear, the BOB features a computer that tells riders when to come up and calculates decompression times for a safe return to the surface. One Digital Day, 1998 www.intel.com/onedigitalday What do applications (“apps”) like these mean for reliability requirements of our technology?

17 COMP3221 lec15-function-I.17 Saeid Nooshabadi C memory allocation seen by the Program 0  Address Code Program Static Variables declared once per program Heap Explicitly created space, e.g., malloc(); C pointers Stack Space for saved procedure information sp stack pointer Static base sb

18 COMP3221 lec15-function-I.18 Saeid Nooshabadi Stack Operations °PUSH v1 °POP v1 sub sp,sp, #4 ; space on stack str v1, [sp,#0] ; save x ldr v1, [sp,#0] ; restore x add sp, sp,#4 ; => stack space SP v1 61 75... What does sp point to? Decreasing address

19 COMP3221 lec15-function-I.19 Saeid Nooshabadi Stack Operations (Better Way) °PUSH v1 °POP v1 str v1, [sp,#-4]! ; space on stack ; and save x ldr v1, [sp], #4 ; restore x ; and reclaim stack space SP v1 61 75... What does sp point to? Decreasing address

20 COMP3221 lec15-function-I.20 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees Typical Structure of a Stack Frame °compiler calculates total frame size (F) °at start of call, subtract F from SP °at end, add F to SP °where are args from caller? °where are args for callee Caller Frame Callee Frame

21 COMP3221 lec15-function-I.21 Saeid Nooshabadi “And in Conclusion …” (#1/2) °ARM Assembly language instructions Unconditional branches: b, mov pc, rx, bl °Operands Registers (word = 32 bits); a1 - a3, v1 – v8, ls, sb, fp, sp, lr

22 COMP3221 lec15-function-I.22 Saeid Nooshabadi “And in Conclusion …” (#2/2) °Functions, procedures one of main ways to give a program structure, reuse code °mov pc, Rn required instruction; most add bl (or equivalent) to make common case fast °Registers make programs fast, but make procedure/function call/return tricky °ARM SW convention divides registers for passing arguments, return address, return value, stack pointer

23 COMP3221 lec15-function-I.23 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II http://www.cse.unsw.edu.au/~cs3221 September, 2003 Saeid Nooshabadi Saeid@unsw.edu.au

24 COMP3221 lec15-function-I.24 Saeid Nooshabadi Overview °Resolving Registers Conflicts °Caller /Callee Responsibilities °Frame/Stack pointer °Conclusion

25 COMP3221 lec15-function-I.25 Saeid Nooshabadi Review: Basics of Function Call...(use regs) set up args jump to function access result...(use regs) access args... compute result......(use regs) set up return value jump back to caller Caller Callee

26 COMP3221 lec15-function-I.26 Saeid Nooshabadi Review: Function Call Bookkeeping Registers for functions  lr = r14  a1, a2, a3, a4  a1  v1, v2,v3,v4,v5,v6,v7 °Procedure address °Return address °Arguments °Return value °Local variables °Registers (conflicts) =>ARM Procedure Call Standards (APCS) conventions for use of registers simplify bookkeeping

27 COMP3221 lec15-function-I.27 Saeid Nooshabadi Review: Instruction Support for Functions? °Single instruction to branch and save return address: branch and link ( bl ): 1012 bl sum ; lr = 1016,goto sum

28 COMP3221 lec15-function-I.28 Saeid Nooshabadi Review: APCS Register Convention: Summary register namesoftware nameuse and linkage r0 – r3a1 – a4 first 4 integer args scratch registers integer function results r4 – r11v1- v8local variables r9sbstatic variable base r10slstack limit r11fpframe pointer r12ipintra procedure-call scratch pointer r13spstack pointer r14lrreturn address r15pcprogram counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

29 COMP3221 lec15-function-I.29 Saeid Nooshabadi Review: Nested Procedures °A caller function is itself called from another function. °We need to store lr for the caller before it can call another function. °In general, may need to save some other info in addition to lr. °But; Where do we save these info?

30 COMP3221 lec15-function-I.30 Saeid Nooshabadi Review: C memory allocation map 0  Address Code Program Static Variables declared once per program Heap Explicitly created space, e.g., malloc(); C pointers Stack Space for saved procedure information sp stack pointer Static base sb

31 COMP3221 lec15-function-I.31 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees Review: Typical Structure of a Stack Frame °compiler calculates total frame size (F) °at start of call, subtract F from SP °at end, add F to SP °where are args from caller? °where are args for callee Caller Frame Callee Frame

32 COMP3221 lec15-function-I.32 Saeid Nooshabadi Basic Structure of a Function entry_label: sub sp,sp, #fsize ; create space on stack str lr,[sp, #fsize-4]; save lr ; save other regs... ;restore other regs ldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack mov pc, lr Epilogue Prologue Body lr

33 COMP3221 lec15-function-I.33 Saeid Nooshabadi Compiling nested C func into ARM int sumSquare(int x, int y) { return mult (x,x)+ y; } sumSquare: sub sp,sp,#8 ; space on stack str lr,[sp,#4] ; save ret addr str a2,[sp,#0] ; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#0] ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4] ; get ret addr add sp,sp,#8 ; => stack space mov pc, lr C Epilogue Prologue Body ARMARM

34 COMP3221 lec15-function-I.34 Saeid Nooshabadi Compiling nested C func into ARM (Better way) int sumSquare(int x, int y) { return mult (x,x)+ y; } sumSquare: str lr,[sp,#-4]!; save ret addr str a2,[sp,#-4]!; save y mov a2,a1 ; mult(x,x) bl mult ; call mult ldr a2,[sp,#4]! ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4]! ; get ret addr ; => stack space mov pc, lr C Epilogue Prologue Body ARMARM

35 COMP3221 lec15-function-I.35 Saeid Nooshabadi Function Call Bookkeeping: thus far °Procedure addressx °Return address xlr °Arguments xa1 – a4 °Return values xa1 – a4 °Local variables xv1 – v8 °Registers what if they are reused? what if there aren’t enough?

36 COMP3221 lec15-function-I.36 Saeid Nooshabadi Readings for Week #6 °Steve Furber: ARM System On-Chip; 2nd Ed, Addison- Wesley, 2000, ISBN: 0-201-67519-6. Chapters 6 °Experiment 3 Documentation

37 COMP3221 lec15-function-I.37 Saeid Nooshabadi Exceeding limits of registers °Recall: assembly language has fixed number of operands, HLL doesn’t °Local variables: v1,..., v8 What if more than 8 words of local variables? °Arguments; a1,..., a4 What if more than 4 words of arguments? °Place extra variables and extra arguments onto stack ( sp ) °Use scratch registers and data transfers to access these variables

38 COMP3221 lec15-function-I.38 Saeid Nooshabadi Register Conflicts °Procedure A calls Procedure B A referred to as is “calling procedure” or “caller” B referred to as is “called procedure” or “callee” °Both A and B want to use the 15 registers => must cooperate

39 COMP3221 lec15-function-I.39 Saeid Nooshabadi Register Conflicts: 2 options (A calls B) 1) Called procedure/callee (B) leaves registers the way it found them (except lr ); its B’s job to save it before using it and then restore it: “callee saves” Since B only saves what it changes, more accurate is “callee saves (what it uses)” °2) B can use any register it wants; Calling procedure/caller A must save any register it wants to use after call of B: “caller saves” Since A knows what it needs after call, more accurate is “caller saves (if it wants to)” °Is either optimal?

40 COMP3221 lec15-function-I.40 Saeid Nooshabadi ARM Solution to Register Conflicts °Divide registers into groups Local variables / Callee Saved registers ( v1 – v8 ) Scratch registers / Argument / Caller Save registers ( a1 – a3 ) Some caller saved (if wants) and some callee saved (if used) °Caller (A) save/restore scratch / argument ( a1 – a4 ) if needs them after the call; also lr  callee can use ( a1 – a4 ) and lr °Callee (B) must save/restore local variables / callee saved registers ( v1 – v8 ) if it uses them  caller can leave them unsaved Procedure that doesn’t call another tries to use only scratch / argument registers ( a1 – a4 )

41 COMP3221 lec15-function-I.41 Saeid Nooshabadi Register Conventions (#1/5) °Caller: the calling function °Callee: the function being called °When callee returns from executing, the caller needs to know which registers may have changed and which are guaranteed to be unchanged. °Register Conventions: A set of generally accepted rules as to which registers will be unchanged after a procedure call ( bl ) and which may be changed.

42 COMP3221 lec15-function-I.42 Saeid Nooshabadi Register Conventions (#2/5) °Three views of registers a1 – a4 °a1 – a4 : Change. These are expected to contain new return values. Or °a1 – a4 : Change. These are volatile argument registers. Or °a1 – a4 : Change. They’re called scratch: any procedure may change them at any time.

43 COMP3221 lec15-function-I.43 Saeid Nooshabadi Register Conventions (#3/5) °v1 – v8 : No Change. Very important, that’s why they’re called callee saved registers / local variable. If the callee changes these in any way, it must restore the original values before returning. °sp : No Change. The stack pointer must point to the same place before and after the bl call, or else the caller won’t be able to restore values from the stack. Grows downward, sp points to last full location °lr : Change. The bl call itself will change this register. °ip : Change. In most variants of APCS ip is a scratch register.

44 COMP3221 lec15-function-I.44 Saeid Nooshabadi Register Conventions (#4/5) °What do these conventions mean? If function A calls function B, then function A must save any scratch registers a1 – a4 that it may be using onto the stack before making a bl call. Remember: Caller needs to save only registers it is using, not all scratch registers. It also needs to store lr if A is in turn is called by another function.

45 COMP3221 lec15-function-I.45 Saeid Nooshabadi Register Conventions (#5/5) °Note that, even though the callee may not return with different values in the callee saved registers v1 – v8, it can use them by : save v1 – v8 on the stack use these eight registers restore v1 – v8 from the stack °The difference is that, with the scratch registers a1 – a4, the callee doesn’t need to save them onto the stack.

46 COMP3221 lec15-function-I.46 Saeid Nooshabadi sp on entry  Register Save Area Local Variables sp during  Arguments for Callees Recall: Typical Structure of a Stack Frame °compiler calculates total frame size (F) for the caller °at start of procedure, subtract F from SP °at end, add F to SP °where are args from caller? °where are the args for callee? Caller Frame Callee Frame

47 COMP3221 lec15-function-I.47 Saeid Nooshabadi Callees’ & Callers’ Rights (Summary) °Callees’ Right Right to use a1 – a4 registers freely Right to assume args are passed correctly in a1 – a4 °Callers’ Rights Right to use v1 – v8 registers without fear of being overwritten by Callee Right to assume return values will be returned correctly in a1 – a4 Keep this slide in mind for exam

48 COMP3221 lec15-function-I.48 Saeid Nooshabadi Callee’s Responsibilities ( Summary) 1.If using v1 – v8 or big local structs, slide sp down to reserve memory: e.g. sub sp, sp, #32 2.If using v1 – v8, save before using: e.g. str v1, [sp, #28] 3.Receive args in a1 – a4, additional args on stack 4.Run the procedure body 5.If not void, put return values in a1 – a4 6.If applicable, undo steps 2-1 e.g. ldr v1, [sp,#28] add sp, sp, #32 7.mov pc, lr Keep this slide in mind for exam

49 COMP3221 lec15-function-I.49 Saeid Nooshabadi Caller’s Responsibilities ( Summary) 1.Slide sp down to reserve memory: e.g. sub sp, sp, #28 2.Save lr on stack before bl to callee clobbers it: e.g. str lr, [sp, #24] 3.If you still need their values after the function call, save a1 – a4 on stack or copy to v1 – v8 registers. Callee can overwrite “a” registers, but not “v” registers. e.g. str a1, [sp, #20] 4.Put first 4 words of args in a1 – a4, at most 1 arg per word, additional args go on stack: “arg 5” is [sp, #0] 5.bl to the desired function 6.Receive return values in a1 – a4 7.Undo steps 3-1: e.g. ldr a1, [sp, #20], ldr lr, [sp, #24], add sp, sp, #28 Keep this slide in mind for exam

50 COMP3221 lec15-function-I.50 Saeid Nooshabadi Compile using pencil and paper Example 1 (#1/2) int Doh(int i, int j, int k, int l){ return i+j+l; } Doh: _______________ add a1, _________________ _________________

51 COMP3221 lec15-function-I.51 Saeid Nooshabadi Compile using pencil and paper Example 1 (#2/2) int Doh(int i, int j, int k, int l){ return i+j+l; } Doh: _______________ add a1, _______ _______________ add a1, a2, a1 a4, a1 mov pc lr “a” Regs Safe For Callee

52 COMP3221 lec15-function-I.52 Saeid Nooshabadi Compile using pencil and paper Example 2 (#1/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add a1, _______ _______________ _______________

53 COMP3221 lec15-function-I.53 Saeid Nooshabadi Compile using pencil and paper Example 2 (#2/2) int Doh(int i, int j, int k, int m, char c, int n){ return i+j+n; } Doh: _______________ add a1, ______ ldr ip,[sp,#4] add a1, a1, ip a1,a2 mov pc lr 6th argument “a” & ip Regs Safe For Callee

54 COMP3221 lec15-function-I.54 Saeid Nooshabadi Hard Compilation Problem (#1/2) int Doh(int i, int j, int k, int m, char c, int n){return mult(i,j)+n;} int mult(int m, int n);/* returns m  n */ Doh: ______________ ______________ bl ___________ add ___________ _______________

55 COMP3221 lec15-function-I.55 Saeid Nooshabadi Slow-motion Replay (#1/2) int Doh(int i, int j, int k, int m, char c, int n){ return sum(i,j)+n;} int mult(int m, int n);/* returns m  n */ Doh: ______________ ______________ bl ___________ add ___________ _______________ str lr, [sp] sub sp, sp,#4 ldr lr, [sp] add sp, sp, #4 mov pc, lr mult ldr,[sp,#8] v1 2.Saving lr => must move sp 1.Calling Sum => save lr, a Regs 3.Need n after funccall => v1 a1, a1, v1

56 COMP3221 lec15-function-I.56 Saeid Nooshabadi Stack Memory Allocation Revisited °Caller frame Save caller-saved regs Pass arguments (4 regs) bl °Callee frame set fp @first word of frame fp = Caller’s sp - 4 (fixed point) Save registers on stack and update sp as needed Accessing the local variables is always with reference to fp Return saved register from stack using fp Callee Saved Registers fp low high Address stack grows Local Variables sp... Argument 5 Argument 6 place for a1-a4 °GCC uses frame pointer, ARM compilers by default don’t (allocate extra temporary register ( ip ), less bookkeeping on procedure call)

57 COMP3221 lec15-function-I.57 Saeid Nooshabadi “And in Conclusion …’’ °ARM SW convention divides registers into those calling procedure save/restore and those called procedure save/restore Assigns registers to arguments, return address, return value, stack pointer °Optional Frame pointer fp reduces bookkeeping on procedure call

58 COMP3221 lec15-function-I.58 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III http://www.cse.unsw.edu.au/~cs3221 September, 2003 Saeid Nooshabadi Saeid@unsw.edu.au

59 COMP3221 lec15-function-I.59 Saeid Nooshabadi Overview °Why Procedure Conventions? °Basic Structure of a Function °Example: Recursive Function °Instruction Support for Function °Block Store and Load °Conclusion

60 COMP3221 lec15-function-I.60 Saeid Nooshabadi Review: APCS Register Convention: Summary register namesoftware nameuse and linkage r0 – r3a1 – a4 first 4 integer args scratch registers integer function results r4 – r11v1- v8local variables r9sbstatic variable base r10slstack limit r11fpframe pointer r12ipintra procedure-call scratch pointer r13spstack pointer r14lrreturn address r15pcprogram counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

61 COMP3221 lec15-function-I.61 Saeid Nooshabadi Review: Function Call Bookkeeping °Big Ideas: Follow the procedure conventions and nobody gets hurt. Data is just 1’s and 0’s, what it represents depends on what you do with it Function Call Bookkeeping: Caller Saved Registers are saved by the caller, that is, the function that includes the bl instruction Callee Saved Registers are saved by the callee, that is, the function that includes the mov pc, lr instruction Some functions are both a caller and a callee

62 COMP3221 lec15-function-I.62 Saeid Nooshabadi Review: Caller & Callee Saved Registers Caller Saved Registers: Return address lr Arguments a1, a2, a3, a4 Return value a1, a2, a3, a4 Callee Saved Registers: v Registers v1 – v8

63 COMP3221 lec15-function-I.63 Saeid Nooshabadi Review: StackMemory Allocation on Call C Procedure Call Frame °Pass arguments (4 regs) °If called from another functions, save lr °Save caller-saved regs °Save additional Arguments °bl °Save old sp and fp & set fp 1st word of frame (old sp-4 ) °Save callee-saved regs Callee Saved Registers Argument 5 Argument 6 fp low high Address stack grows Local Variables sp... Caller Saved Registers

64 COMP3221 lec15-function-I.64 Saeid Nooshabadi Review: Memory Deallocation on Return °Move return value into a1 °Restore callee-saved regs from the stack °Restore old sp and fp from stack °mov pc, lr °Restore caller-saved regs °If saved lr, restore it... fp low high Address stack grows sp

65 COMP3221 lec15-function-I.65 Saeid Nooshabadi Why Procedure Conventions? (#1/2) °Think of procedure conventions as a contract between the Caller and the Callee If both parties abide by a contract, everyone is happy ( : ) ) If either party breaks a contract, disaster and litigation result ( : O ) °Similarly, if the Caller and Callee obey the procedure conventions, there are significant benefits. If they don’t, disaster and program crashes result

66 COMP3221 lec15-function-I.66 Saeid Nooshabadi Why Procedure Conventions? (#2/2) °Benefits of Obeying Procedure Conventions: People who have never seen or even communicated with each other can write functions that work together Recursion functions work correctly

67 COMP3221 lec15-function-I.67 Saeid Nooshabadi Basic Structure of a Function entry_label: sub sp,sp, #fsize ; create space on stack str lr,[sp, #fsize-4]; save lr ; save other regs... ;restore other regs ldr lr, [sp,#fsize-4];restore lr add sp, sp, #fsize ;reclaim space on stack mov pc, lr Epilogue Prologue Body lr

68 COMP3221 lec15-function-I.68 Saeid Nooshabadi Example: Compile This (#1/5) main() { int i,j,k,m; /* i-m:v1–v4 */ i = mult(j,k);... ; m = mult(i,i);... return 0 } int mult (int mcand, int mlier){ int product; product = 0; while (mlier > 0) { product += mcand; mlier -= 1; } return product; }

69 COMP3221 lec15-function-I.69 Saeid Nooshabadi Example: Compile This (#2/5) __start: str lr, [sp,#-4]!; store return ; address mov a1,v2 ; arg1 = j mov a2,v3 ; arg2 = k bl mult ; call mult mov v1, a1 ; i = mult()... mov a1, v1 ; arg1 = i mov a2, v1 ; arg2 = i bl mult ; call mult mov v4, a1 ; m = mult()... ldr lr, [sp,#4]! ; restore return address mov pc, lr

70 COMP3221 lec15-function-I.70 Saeid Nooshabadi Example: Compile This (#3/5) °Notes: main function returns to O/S, so mov pc, lr, so there’s need to save lr onto stack all variables used in main function are callee saved registers ( “v”), so there’s no need to save these onto stack

71 COMP3221 lec15-function-I.71 Saeid Nooshabadi Example: Compile This (#4/5) mult: mov a3, #0 ; prod=0 Loop: cmp a2,#0; mlier > 0? beq Fin; no=>Fin add a3,a3,a1; prod+=mcand sub a2,a2,#1; mlier-=1 b Loop; goto Loop Fin: mov a1,a3 ; a1=prod mov pc, lr; return

72 COMP3221 lec15-function-I.72 Saeid Nooshabadi Example: Compile This (#5/5) °Notes: no bl calls are made from mult and we don’t use any callee saved ( “v” ) registers, so we don’t need to save anything onto stack Scratch registers a1 – a3 are used for intermediate calculations a2 is modified directly (instead of copying into a another scratch register) since we are free to change it result is put into a1 before returning

73 COMP3221 lec15-function-I.73 Saeid Nooshabadi Fibonacci Rabbits °Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. °How many pairs will there be in one year? Fibonacci’s Puzzle Italian, mathematician Leonardo of Pisa (also known as Fibonacci) 1202.

74 COMP3221 lec15-function-I.74 Saeid Nooshabadi Fibonacci Rabbits (Solution) 1.At the end of the first month, they mate, but there is still one only 1 pair. 2.At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field. 3.At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field. 4.At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.

75 COMP3221 lec15-function-I.75 Saeid Nooshabadi Fibonacci Rabbits (Solution Animated) Month  012345 Rabbit 0112358 Numbers        

76 COMP3221 lec15-function-I.76 Saeid Nooshabadi Fibonacci Rabbits (Solution in Picture) The number of pairs of rabbits in the field at the start of each month is 1, 1, 2, 3, 5, 8, 13, 21, 34,...

77 COMP3221 lec15-function-I.77 Saeid Nooshabadi Example: Fibonacci Numbers (#1/6) °The Fibonacci numbers are defined as follows: °F(n) = F(n – 1) + F(n – 2) F(0) and F(1) are defined to be 1 °In C, this could be written: int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2)); }

78 COMP3221 lec15-function-I.78 Saeid Nooshabadi fib: ___________________ ; Save the return address ; Save v1_& Push the ; stack frame___ str lr, [sp,#-4]! str v1, [sp,#-4]! °Now, let’s translate this to ARM! °You will need space for three words on the stack °The function will use v1 °Write the Prologue: Example: Fibonacci Numbers (#2/6)

79 COMP3221 lec15-function-I.79 Saeid Nooshabadi fin: ldr v1, [sp,#4]!___ ldr lr, [sp,#4]!___ mov pc,lr__________ ; Restore v1__________ ; Restore return address ; Pop the stack frame___ ; Return to caller_______ °Now write the Epilogue: Example: Fibonacci Numbers (#3/6)

80 COMP3221 lec15-function-I.80 Saeid Nooshabadi cmp a1, #0 cmpne a1, #1 moveq, a1, #1 beqfin_____ Continued on next slide... °Finally, write the body. The C code is below. Start by translating the lines indicated in the comments int fib(int n) { if(n == 0) { return 1; } /*Translate Me!*/ if(n == 1) { return 1; } /*Translate Me!*/ return (fib(n - 1) + fib(n - 2)); } ; if (n == 0)... ; if (n == 1)... ;..._____________ ; return 1__________ Example: Fibonacci Numbers (#4/6)

81 COMP3221 lec15-function-I.81 Saeid Nooshabadi ; Need a1 after bl ; a1 = n – 1_______ ; fib(n – 1) ______ ; Save return value ; Restore a1_______ ; a1 = n – 2_______ str a1, [sp, #-4]! sub a1, a1, #1_____ bl fib_____________ mov v1, a1_________ ldr a1, [sp, #4]!__ sub a1, a1, #2_____ Continued on next slide... °Almost there, but be careful, this part is tricky! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers (#5/6)

82 COMP3221 lec15-function-I.82 Saeid Nooshabadi bl fib__________ add a1, a1, v1__ ;To the epilogue and beyond... ; fib(n-2)_______________ ; a1 = fib(n-1) + fib(n-2) °Remember that is v1 Callee Save and a1 is caller saved! int fib(int n) {... return (fib(n - 1) + fib(n - 2)); } Example: Fibonacci Numbers (#6/6)

83 COMP3221 lec15-function-I.83 Saeid Nooshabadi Stack Growth and Shrinkage F(5) sp lr v1 a1 F(4) sp lr v1 a1 F(3) sp v1 a1 lr F(2) sp v1 a1 lr F(1) sp v1 lr 2 F(1) sp v1 lr + 1 3 F(2) sp v1 a1 lr F(1) sp v1 lr 1 F(0) sp v1 lr + 1 + 2 5 F(3) sp v1 a1 lr F(2) sp v1 a1 lr F(1) sp v1 lr 1 F(0) sp v1 lr + 1 1 F(0) sp v1 lr + 1 2 F(1) sp v1 lr + 1 + 3 8 sp int fib(int n) { if(n == 0) { return 1; } if(n == 1) { return 1; } return (fib(n - 1) + fib(n - 2); }

84 COMP3221 lec15-function-I.84 Saeid Nooshabadi Instruction Support for Stack °Consider the following code: str lr, [sp,#-4]! str fp, [sp,#-4]! str v3, [sp,#-4]! str v2, [sp,#-4]! str v1, [sp,#-4]! str a2, [sp,#-4]! str a1, [sp,#-4]! Epilogue Prologue Body... ldr a1, [sp,#4]! ldr a2, [sp,#4]! ldr v1, [sp,#4]! ldr v2, [sp,#4]! ldr v3, [sp,#4]! ldr fp, [sp,#4]! ldr lr, [sp,#4]! stmfd sp!, {a1-a2,v1-v3,fp,lr} ldmfd sp!, {a1-a2,v1-v3,fp,lr} Old SP fp v3 v2 v1 a2 SP lr a1 fp v3 v2 v1 a2 SP lr a1 Store Multiple Full Descending Load Multiple Full Descending

85 COMP3221 lec15-function-I.85 Saeid Nooshabadi Block Copy via Stack Operation °The contents of registers r0 to r6 need to be swapped around thus: r0 moved into r3 r1 moved into r4 r2 moved into r6 r3 moved into r5 r4 moved into r0 r5 moved into r1 r6 moved into r2 °Write a segment of code that uses full descending stack operations to carry this out, and hence requires no use of any other registers for temporary storage.

86 COMP3221 lec15-function-I.86 Saeid Nooshabadi Block Copy Sample Solution ldmfd sp!, {r3,r4,r6} r3 = r0 r4 = r1 r6 = r2 r5 r4 sp r6 r3 ldmfd sp!, {r5} r5 = r3 r5 sp r6 r4 ldmfd sp!, {r0-r2} r0 = r4 r1 = r5 r2 = r6 sp stmfd sp!, {r0-r6} Old sp r5 r4 r3 r2 r1 sp r6 r0

87 COMP3221 lec15-function-I.87 Saeid Nooshabadi Direct functionality of Block Data Transfer °When LDM / STM are not being used to implement stacks, it is clearer to specify exactly what functionality of the instruction is: i.e. specify whether to increment / decrement the base pointer, before or after the memory access. °In order to do this, LDM / STM support a further syntax in addition to the stack one: STMIA / LDMIA : Increment After STMIB / LDMIB : Increment Before STMDA / LDMDA : Decrement After STMDB / LDMDB : Decrement Before For details See Chapter 3, page 61 – 62 Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN: 0-201-67519-6.

88 COMP3221 lec15-function-I.88 Saeid Nooshabadi “And in Conclusion …’’ °ARM SW convention divides registers into those calling procedure save/restore and those called procedure save/restore Assigns registers to arguments, return address, return value, stack pointer °Optional Frame pointer fp reduces bookkeeping on procedure call °Use Stack Block copy Instructions stmfd & ldmfd to store and retrieve multiple registers to/from from stack.


Download ppt "COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functions in C/ Assembly - I"

Similar presentations


Ads by Google