1 / 69

April, 2004 Modified from notes by Saeid Nooshabadi Saeid@unsw.au

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. Overview. C functions Bookkeeping for function call/return Instruction support for functions

Download Presentation

April, 2004 Modified from notes by Saeid Nooshabadi Saeid@unsw.au

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 3221 Microprocessors and Embedded Systems Lectures 6 : Functionsin C/ Assembly - Ihttp://www.cse.unsw.edu.au/~cs3221 April, 2004 Modified from notes by Saeid Nooshabadi Saeid@unsw.edu.au

  2. Overview • C functions • Bookkeeping for function call/return • Instruction support for functions • Nested function calls • C memory allocation: static, heap, stack • Conclusion

  3. C functions What information mustcompiler/program keep track of? 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; }

  4. Basics of Function Call Caller Callee ...(use regs) set up args jump to function access args ... compute result ... ...(use regs) set up return value jump back to caller access result ...(use regs)

  5. 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 Function Call Bookkeeping

  6. APCS Register Convention: Summary register name software name use and linkage r0 – r3 a1 – a4 first 4 integer args scratch registers integer function results r4 – r11 v1- v8 local variables r9 sb static variable base r10 sl stack limit r11 fp frame pointer r12 ip intra procedure-call scratch pointer r13 sp stack pointer r14 lr return address r15 pc program counter Red are SW conventions for compilation, blue are HW ARM Procedure Call Standard (APCS)

  7. Instruction Support for Function Call? C ... sum(a,b);... /* a,b:v1,v2 */}int sum(int x, int y) { return x+y;} address1000mov a1,v1 ; x = a1004mov a2, v2 ; y = b1008mov lr,#1016 ; lr = 10161012b sum; jump to sum1016 ... 2000sum: add a1,a1,a22004mov pc, lr A R M ; b 1016 Why mov pc, lr vs. b 1016 to return?

  8. 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

  9. 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

  10. Accessing array elements => indirection int sumarray(int arr[]) {int i, sum; for(i=0;i<100;i=i+1) sum = sum + arr[i]; } mov v1, #0 ; clear v0 add a2,a1,#400 ; beyond end of arr[]Loop: cmp a1,a2 bge Exit ldr a3, [a1], #4 ; a3=arr[i], a1++ add v1,v1,a3 ; v1= v1+ arr[i] b LoopExit: mov lr, pc

  11. Instruction Support for Functions? • Single instruction to branch and save return address: branch and link (bl): • Before:1008mov lr, #1016;lr = 10161012b sum;goto sum • After:1012bl sum; lr = 1016,goto sum • Why bl? Make the common case fast • and elegance

  12. Nested Procedures (#1/2) ...sumSquare(a,b)... int sumSquare(int x, int y) { return mult(x,x)+ y;} • Need to save sumSquarereturn address saved in lr by blsumSquare instruction,before call to mult • Otherwise blmult 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

  13. 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

  14. Space for saved procedure information sp stack pointer Explicitly created space, e.g., malloc(); C pointers Static base sb Variables declared once per program Code Static Heap Stack Program C memory allocation seen by the Program ¥ Address 0

  15. Stack Operations • PUSH v1 • POP v1 61 v1 • sub sp,sp, #4; space on stack • str v1, [sp,#0]; save x ... 75 SP 61 Decreasing address • ldr v1, [sp,#0] ; restore x • add sp, sp,#4; => stack space What does sp point to?

  16. Stack Operations (Better Way) • PUSH v1 • POP v1 61 v1 • str v1, [sp,#-4]!; space on stack • ; and save x ... 75 SP 61 Decreasing address • ldr v1, [sp], #4; restore x • ; and reclaim stack space What does sp point to?

  17. 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 sp on entry  Register Save Area Caller Frame Local Variables Arguments for Callees sp during  Callee Frame

  18. “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

  19. “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

  20. 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?

  21. Space for saved procedure information sp stack pointer Explicitly created space, e.g., malloc(); C pointers Static base sb Variables declared once per program Code Static Heap Stack Program Review: C memory allocation map ¥ Address 0

  22. 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 sp on entry  Register Save Area Caller Frame Local Variables Arguments for Callees sp during  Callee Frame

  23. Basic Structure of a Function Prologue entry_label:sub sp,sp, #fsize ; create space on stackstr 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 lr Body Epilogue

  24. Compiling nested C func into ARM C int sumSquare(int x, int y) { return mult(x,x)+ y;} sumSquare:sub sp,sp,#8; space on stackstr lr,[sp,#4]; save ret addrstr a2,[sp,#0] ; save ymova2,a1 ; mult(x,x)bl mult; call multldr a2,[sp,#0] ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4]; get ret addr add sp,sp,#8; => stack spacemov pc, lr Prologue A R M Body Epilogue

  25. Compiling nested C func into ARM (Better way) C int sumSquare(int x, int y) { return mult(x,x)+ y;} sumSquare:str lr,[sp,#-4]!; save ret addrstr a2,[sp,#-4]!; save ymova2,a1 ; mult(x,x)bl mult; call multldr a2,[sp,#4]! ; restore y add a1,a1,a2 ; mult()+y ldr lr,[sp,#4]!; get ret addr ; => stack spacemov pc, lr Prologue A R M Body Epilogue

  26. Function Call Bookkeeping: thus far • Procedure address x • Return address x lr • Arguments x a1 – a4 • Return values x a1 – a4 • Local variables x v1 – v8 • Registers • what if they are reused? • what if there aren’t enough?

  27. 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

  28. 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

  29. 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

  30. 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?

  31. 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)

  32. 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.

  33. 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.

  34. 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.

  35. Register Conventions (#4/5) • What do these conventions mean? • If function A calls function B, then function A must save any scratch registers a1 – a4that 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.

  36. 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.

  37. 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? sp on entry  Register Save Area Caller Frame Local Variables Arguments for Callees sp during  Callee Frame

  38. Callees’ & Callers’ Rights (Summary) • Callees’ Right • Right to use a1 – a4registers 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

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

  40. Caller’s Responsibilities (Summary) • Slide sp down to reserve memory: e.g. sub sp, sp, #28 • Save lron stack before bl to callee clobbers it: e.g. str lr, [sp, #24] • 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] • 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] • bl to the desired function • Receive return values in a1 – a4 • 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

  41. 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, _________________ _________________

  42. 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

  43. 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, _______ _______________ _______________

  44. 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; } 6th argument Doh: _______________ add a1, ______ • ldr ip,[sp,#4] • add a1, a1, ip • a1,a2 • mov pc lr “a” & ip Regs Safe For Callee

  45. add ___________ _______________ _______________ _______________ _______________ Doh: ______________ ______________ ______________ ______________ bl___________ 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 */

  46. add ___________ _______________ _______________ _______________ _______________ Doh: ______________ ______________ ______________ ______________ bl ___________ 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 */ a1, a1, v1 sub sp, sp,#4 str lr, [sp] ldr lr, [sp] add sp, sp, #4 ldr ,[sp,#8] v1 mov pc, lr mult 1.Calling Sum => save lr, a Regs 2.Saving lr => must move sp 3.Need n after funccall => v1

  47. ... Address high place for a1-a4 stack grows Argument 6 Argument 5 fp sp Callee Saved Registers Local Variables low 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 • GCC uses frame pointer, ARM compilers by default don’t • (allocate extra temporary register (ip), less bookkeeping on procedure call)

  48. Basic Structure of a Function Prologue entry_label:sub sp,sp, #fsize ; create space on stackstr 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 lr Body Epilogue

  49. 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; }

  50. Example: Compile This (#2/5) __start: str lr, [sp,#-4]!; store return ; address mov a1,v2 ; arg1 = jmov a2,v3 ; arg2 = k bl mult ; call multmov v1, a1 ; i = mult()... mov a1, v1 ; arg1 = imov a2, v1 ; arg2 = i bl mult ; call multmov v4, a1 ; m = mult()... ldr lr, [sp,#4]! ; restore return address mov pc, lr

More Related