{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chap10-func-2 - Overview Resolving Registers Conicts...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Overview ° Resolving Registers Conflicts ° Caller /Callee Responsibilities ° Frame/Stack pointer ° Conclusion 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 Review: Instruction Support for Functions? ° Single instruction to branch and save return address: branch and link ( bl ): 1012 bl sum ; lr = 1016,goto sum
Background image of page 2
Review: 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) 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?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 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
Background image of page 4
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 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 A R M
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 A R M Function Call Bookkeeping: thus far ° Procedure address x ° Return address x lr ° Arguments x a1 – a4 ° Return values x
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}