400 likes | 412 Views
Explore the significance of subroutines in programming, reasons to use them, and essential concepts involved in subroutine implementation. Learn about open and closed subroutines, parameter passing, control transfer, and subroutine structure. Discover how compilers and programmers track information, manage register usage, stack allocation, and function call bookkeeping.
E N D
Subroutines • reasons for subroutines • repeat same code, or similar code with slightly different parameters • hide design decisions or design complexity • partition off code likely to change • provide for separate compilation • provide for reuse
Subroutines • "open" subroutine = macro • resolved before assembly by textual substitution, • body of routine is placed in-line at call site with parameter substitution • "closed" subroutine = standard notion • branch/execute/return at run time • one copy of body which accesses its formal parameters
Subroutines • Three main concepts involved: • Transferring control from calling program to a subroutine and back • Passing parameter values to a subroutine and results back from the subroutine • Writing subroutine code that is independent of the calling program • In addition, sometimes a subroutine must allocate space for local variables.
Subroutines • Transferring control from calling program to a subroutine and back • Necessary to save the return address, then branch to the subroutine. • This can be accomplished in ARM using the branch and link instruction bl • blsubr_name • To transfer control back to the calling program, can use the branch and exchange instruction bx • bxlr • Alternatively, you can pop the lr register into the pc
Subroutines • Placing a return address in a link register works as long as there are no nested subroutines. • For nested subroutines, it is necessary to save the return address to the stack • subroutine calls and returns are LIFO, and older processors typically save the return address of a caller by pushing it onto a memory stack • older processors typically pass parameters by pushing them onto the same memory stack that holds the return addresses
Subroutines • Parameters: • actual parameters - values or addresses passed to subroutine • formal parameters - parameter names appearing in subroutine
General structure of a subroutine main: // call // return
C functions main() {inta,b,c; ... c = sum(a,b); // a, b, c: r0, r1, r2 } /* really dumb sum function */ int sum(int x, int y) { return x+y; } What information mustcompiler/programmer keep track of? What instructions can accomplish the return?
Function Call Bookkeeping • Registers play a major role in keeping track of information for function calls • The stack is also used(more on this later)
Register Usage Register Arguments into function; Result(s) from function; otherwise corruptible (Additional parameters passed on stack) r0 r1 r2 r3 r4 r5 Temporary register variables. Must be preserved r6 r7 r8 r9/sb - Stack base r10/sl - Stack limit if software stack checking selected r11 r12 Stack PointerLink Register Program Counter r13/sp - SP should always be 8-byte (2 word) aligned r14/lr - R14 can be used as a temporary once value stacked r15/pc
Register Usage • Temporary Variables • Functions should first try to use registers r0 - r3 for any required temporary variables, since they do not have to be preserved • Registers r4 - r11 are the second choice, but they must be saved at function entry and restored prior to the function return • When more space for temporaries is required, allocate space on the stack.
Register Usage Reasons for using registers or stack space for temporary variables instead of creating fixed locations with labels: Memory conservation: the stack space will be released when the function returns so that it can be reused for other needs Reentrancy: every entry of the function allocates a unique set of memory locations for its temporary variables; therefore, if the function is interrupted, and the interrupting code calls (or reenters) the same function, it is ensured that each invocation will modify only its own set of temporary
Register Usage: Preserving Registers • Functions that modify registers r4 - r11 are required to preserve and restore their original content. • The best way to preserve and restore the registers is to push the registers onto the stack immediately upon entering the function, and to pop them back just before the function returns. • However, since the stack is stored in memory, and since memory cycles are the primary performance bottleneck, it is important to preserve as few registers as possible.
Register Usage: Preserving Registers • For nested function calls, e.g function A calls function B, the call to function B will overwrite the current return address in lr; thus, function A must push the content of lr at its entry and pop it back just before it returns. In this case, the function can pop lr into the pc, thus eliminating the bxlrinstruction.
Register Usage: Preserving Registers Function f1 modifies only registers r0 – r3, and does not call any other functions. Does f1 need to preserve any registers? If so, which ones? How does the function return to the caller? f1: … … … bxlr
Register Usage: Preserving Registers Function f2 does not call any functions; it modifies one or more registers other than r0 – r3; their original content must be preserved. f2: push {r4, … } … … pop {r4, …} bxlr
Register Usage: Preserving Registers • Function f3 contains a call to a second function, f4. The bl instruction used to call f4 modifies lr, which contains f3's return address, so we must preserve lr on the stack. That value is then pop'd into the PC to cause a return. Also, since f4 may modify r0 – r3, • f3's parameters should be moved to r4 – r7 for use, • r4 – r11 should be used for any temporary variables, and • if f3 uses any of r4 – r11, then they should be preserved on entry to f3 and restored just prior to return. • f3: • push {r4, …, lr } • mov r4, r0 // preserve parameter in r4 • … • bl f4 // may modify r0 - r3 • … • pop {r4, …, pc}
Register Usage • The compiler has a set of rules known as a Procedure Call Standard that determines how to pass parameters to a function (see AAPCS) • CPSR flags may be corrupted by function calls. Assembler code which links with compiled code must follow the AAPCS at external interfaces • The AAPCS is part of the new Application Binary Interface (ABI) for the ARM Architecture
Register Conventions • 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. • caller/callee need to save only volatile/saved registers they are using, not all registers.
Saved Register Conventions • r4-r11 (v1-v8): Restore if you change. Very important. If the callee changes these in any way, it must restore the original values before returning. • sp: Restore if you change. The stack pointer must point to the same place before and after the bl call, or else the caller will not be able to restore values from the stack.
Volatile Register Conventions • lr: Can Change. The bx call itself will change this register. Caller needs to save on stack if nested call. • r0-r3 (a1-a4): Can change. These are volatile argument registers. Caller needs to save if they’ll need them after the call. e.g., r0 will change if there is a return value • r12 (ip) may be used by a linker as a scratch register between a routine and any subroutine it calls. It can also be used within a routine to hold intermediate values between subroutine calls.
Instruction Support for Functions bx- performs a branch by copying the contents of a general register, Rn, into the program counter, PC. The branch causes a pipeline flush and refill from the address specified by Rn. • Instead of providing a label to jump to, the BX instruction provides a register which contains an address to jump to • Only useful if we know the exact address to jump • Very useful for function calls: • bl stores return address in register (lr) • bxlr jumps back to that address
Example: main() { ... sum(a,b); // a,b:r4,r5 ... } int sum(int x, int y) { return x + y;} address1000 mov r0, r4 // x = a1004 mov r1, r5 // y = b 1008 bl sum // lr = 1012 branch to sum1012 ... 2000 sum: add r0, r0, r12004 bxlr // mov pc, lr i.e., return Note: returns to address 1012 C A R M
C functions .text .global main .type main, %function main: push {lr} mov r0, #37 // put x in r0 mov r1, #55 // put y in r1 bl sum . . . movr0, #0 pop {pc} .global sum .type main, %function sum: push {lr} add r0, r0, r1 pop {pc}
Nested Procedures intsumSquare(int x, int y) { return mult(x,x)+ y; } • Some subroutine called sumSquare, and now sumSquare is calling mult. • There’s a value in lr that sumSquare wants to jump back to, but this will be overwritten by the call to mult. • Need to save sumSquare’s return address before the call to mult.
Nested Procedures • In general, it may be necessary 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. e.g., C globals • Heap: Variables declared dynamically • Stack: Space to be used by subroutines during execution; this is where we can save register values
Using the Stack • We have a register sp which always points to the last used space in the stack. • To use the stack, we decrement this pointer by the amount of space we need and then fill it with info. • Consider the following C function: intsumSquare(int x, int y) { return mult(x, x) + y;}
Using the Stack • intsumSquare(int x, int y) • { return mult(x, x)+ y; • } sumSquare: add sp,sp,#-8 // space on stackstrlr, [sp,#4] // save ret addrstr r1, [sp] // save y "push" movr1, r0 // mult(x,x)blmult // call mult ldrr1, [sp] // restore y add r0,r0,r1 // mult()+y ldrlr, [sp, #4] // get ret addradd sp, sp, #8 // restore stack bxlrmult: ... "pop"
C functions x86 example of swap routine in C void main() { main: void swap(); pushl %ebp ! save old bp inta,b; movl %esp,%ebp ! set new bp as ! current sp a = 5; b = 44; subl $8,%esp ! sub for a and b swap(&a,&b); movl$5,-4(%ebp) ! initialize a } movl $44,-8(%ebp) ! initialize b leal-8(%ebp),%eax ! form addr of b pushl%eax ! push onto stack leal-4(%ebp),%eax ! form addr of a pushl%eax ! push onto stack call swap ! call addl$8,%esp ! clean parms off ! stack leave ret
C functions x86 example output for swap routine in C (sp called esp, fp called ebp) void swap(x,y) swap: int *x,*y; pushl%ebp ! save old bp { movl%esp,%ebp ! set new bp as current sp int temp; subl$4,%esp ! sub for temp temp = *x; movl8(%ebp),%eax ! move addr x into eax *x = *y; movl(%eax),%edx ! indirectly get value in a *y = temp; movl%edx,-4(%ebp) ! store into temp return; movl8(%ebp),%eax ! move addr x into eax } movl12(%ebp),%edx ! move addr y into edx movl(%edx),%ecx ! indirectly get value in b movl%ecx,(%eax) ! store indirectly into a movl12(%ebp),%eax ! move addr y into eax movl-4(%ebp),%edx ! move temp into edx movl%edx,(%eax) ! store indirectly into b leave ret
C functions ARM code for swap() void swap(int *x, int *y) { swap: // addr of x is in r0 inttemp; // addr of y is in r1 temp = *x; push {lr} // save return address *x = *y;ldrr2, [r0] // move x into r2 *y = temp;ldrr3, [r1] // move y into r3 return;strr3, [r0] // store indirectly into x } str r2, [r1] // store into y mov r0, #0 pop {pc}
Basic Generic Structure of a Function entry_label: add sp,sp,-framesizestrlr,[sp, #framesize-4] //save lr // save other regs if necessary ... restore other regs if necessaryldrlr,[sp, framesize-4] // restore lradd sp,sp, #framesizebxlr Prologue Body (call other functions…) Epilogue
Basic Structure of a Subroutine in ARM entry_label: push {// registers necessary registers, including lr} add sp,sp,-framesize // if space needed for temp vars ... pop { // registers that were pushed}add sp, sp, #framesizebxlr// alternatively, if lr was pushed, can pop lr into pc Prologue Body (call other functions…) Epilogue
Example main() {int x, y; /* x: r0, y: r1 */...m = mult(y,y); ... } intmult (int multiplicand, int multiplier) {int product; product = 0; while (multiplier > 0) { product += multiplicand; multiplier -= 1; } return product; }
Example .global main .type main, %function x .reqr4 // rename register r4 y .reqr5 // rename register r5 prod .reqr3 // rename register r3 main: push {r4, r5, lr} // save registers mov x, #7 // put x in r4 mov y, #5 // put y in r5 /* set up registers for call to mult */ movr0, x movr1, y blmult // call mult movr3,r0 // m = mult(x, y) done: … movr0, #0 pop {r4, r5, pc}
Example .global mult .type mult, %function mult: push {lr} product .req r2 multiplicand .req r0 multiplier .req r1 mov product, #0 cmp multiplier, #0 ble finished loop: add product, product, multiplicand sub multiplier, multiplier, #1 cmpmultiplier, #0 bgtloop finished: mov r0, product pop {pc}
Techniques for passing parameters in ARM In ARM, parameters can be passed to subroutines in three ways: • Through registers • Through memory • Via the stack
Passing Parameters in registers • This is what we have done so far • Fast way of transferring data
Passing Parameters by Reference • Parameters can easily be stored in program memory and then loaded as they are used • Send the address of the data to the subroutine • More efficient in terms of register usage for some types of data, e.g. a long string of characters or an array of int values
Passing Parameters on the stack • Similar to passing parameters in memory • The subroutine uses a dedicated register for a pointer into memory – the stack pointer, register r13. • Data is pushed onto the stack before the subroutine call • The subroutine gets the data off the stack. • The results are then stored back onto the stack to be retrieved by the calling routine.