1 / 24

Functions

Learn how functions are implemented in C, including activation records, function calls, and the use of dynamic linking and return addresses.

cpetrovich
Download Presentation

Functions

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

  2. JSR Instruction • Jumps to a location (like a branch but unconditional),and saves current PC (addr of next instruction) in R7. • saving the return address is called “linking” • target address is PC-relative (PC + Sext(IR[10:0])) • bit 11 specifies addressing mode • if =1, PC-relative: target address = PC + Sext(IR[10:0]) • if =0, register: target address = contents of register IR[8:6]

  3. JSR NOTE: PC has already been incrementedduring instruction fetch stage.

  4. JSRR Instruction • Just like JSR, except Register addressing mode. • target address is Base Register • bit 11 specifies addressing mode • What important feature does JSRR providethat JSR does not?

  5. JSRR NOTE: PC has already been incrementedduring instruction fetch stage.

  6. Returning from a Function • RET (JMP R7) gets us back to the calling routine.

  7. Example: Negate the value in R0 • 2sComp NOT R0, R0 ; flip bits ADD R0, R0, #1 ; add one RET ; return to caller • To call from a program (within 1024 instructions): • ; need to compute R4 = R1 - R3 ADD R0, R3, #0 ; copy R3 to R0JSR 2sComp ; negate ADD R4, R1, R0 ; add to R1 ... • Note: Caller should save R0 if we’ll need it later!

  8. Function • Smaller, simpler, subcomponent of program • Provides abstraction • hide low-level details • give high-level structure to program,easier to understand overall program flow • enables separable, independent development • C functions • zero or multiple arguments passed in • single result returned (optional) • return value is always a particular type • In other languages, called procedures, subroutines, ...

  9. Example of High-Level Structure • main(){ SetupBoard(); /* place pieces on board */ DetermineSides(); /* choose black/white */ • /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet());} Structure of programis evident, even withoutknowing implementation.

  10. Functions in C • Declaration (also called prototype) • int Factorial(int n); • Function call -- used in expression • a = x + Factorial(f + g); type ofreturn value name offunction types of allarguments 1. evaluate arguments 2, execute function 3. use return value in expression

  11. Function Definition • State type, name, types of arguments • must match function declaration • give name to each argument (doesn't have to match declaration) • int Factorial(int n) • { • int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; • } gives control back tocalling function and returns value

  12. Why Declaration? • Since function definition also includesreturn and argument types, why is declaration needed? • Use might be seen before definition. Compiler needs to know return and arg types and number of arguments. • Definition might be in a different file, written by a different programmer. • include a "header" file with function declarations only • compile separately, link together to make executable

  13. Example • double ValueInDollars(double amount, double rate); • main(){ ... dollars = ValueInDollars(francs, DOLLARS_PER_FRANC); printf("%f francs equals %f dollars.\n", francs, dollars); ... • } • double ValueInDollars(double amount, double rate) • { • return amount * rate; • } declaration function call (invocation) definition

  14. Implementing Functions: Overview • Activation record • information about each function,including arguments and local variables • stored on run-time stack Calling function Called function push new activation recordcopy values into argumentscall function execute codeput result in activation record pop activation record from stackreturn get result from stack

  15. Run-Time Stack • Recall that local variables are storedon the run-time stack in an activation record • Frame pointer (R5) points to the beginning of aregion of activation record that stores local variables forthe current function • When a new function is called, its activation record is pushed on the stack; when it returns, its activation record is popped off of the stack.

  16. Run-Time Stack Memory Memory Memory R6 R5 Watt R6 R6 R5 R5 main main main Before call During call After call

  17. Activation Record • int NoName(int a, int b){ int w, x, y; . . . return y;} y x w dynamic link return address return value a b locals R5 bookkeeping args Name Type Offset Scope a b w x y int int int int int 4 5 0 -1 -2 NoName NoName NoName NoName NoName

  18. Activation Record Bookkeeping • Return value • space for value returned by function • allocated even if function does not return a value • Return address • save pointer to next instruction in calling function • convenient location to store R7 in case another function (JSR)is called • Dynamic link • caller’s frame pointer • used to pop this activation record from stack

  19. Example Function Call • int Volta(int q, int r) { int k; int m; ... return k;}int Watt(int a){ int w; ... w = Volta(w,10); ... return w;}

  20. Calling the Function q r w dyn link ret addr ret val a 25 10 25 • w = Volta(w, 10); • ; push second argAND R0, R0, #0ADD R0, R0, #10ADD R6, R6, #-1STR R0, R6, #0; push first argumentLDR R0, R5, #0ADD R6, R6, #-1STR R0, R6, #0; call subroutineJSR Volta new R6 R6 R5 xFD00 Note: Caller needs to know number and type of arguments,doesn't know about local variables.

  21. Starting the Callee Function m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x3100 25 10 25 • ; leave space for return valueADD R6, R6, #-1; push return addressADD R6, R6, #-1STR R7, R6, #0; push dyn link (caller’s frame ptr)ADD R6, R6, #-1STR R5, R6, #0; set new frame pointerADD R5, R6, #-1; allocate space for localsADD R6, R6, #-2 new R6 new R5 R6 R5 xFD00

  22. Ending the Callee Function m k dyn link ret addr ret val q r w dyn link ret addr ret val a -43 217 xFCFB x3100 217 25 10 25 • return k; • ; copy k into return valueLDR R0, R5, #0STR R0, R5, #3; pop local variablesADD R6, R5, #1; pop dynamic link (into R5)LDR R5, R6, #0ADD R6, R6, #1; pop return addr (into R7)LDR R7, R6, #0ADD R6, R6, #1; return control to callerRET R6 R5 new R6 new R5 xFD00

  23. Resuming the Caller Function ret val q r w dyn link ret addr ret val a 217 25 10 217 • w = Volta(w,10); • JSR Volta; load return value (top of stack)LDR R0, R6, #0; perform assignmentSTR R0, R5, #0; pop return valueADD R6, R6, #1; pop argumentsADD R6, R6, #2 R6 new R6 R5 xFD00

  24. Summary of LC-3 Function Call Implementation • Caller pushes arguments (last to first). • Caller invokes subroutine (JSR). • Callee allocates return value, pushes R7 and R5. • Callee allocates space for local variables. • Callee executes function code. • Callee stores result into return value slot. • Callee pops local vars, pops R5, pops R7. • Callee returns (JMP R7). • Caller loads return value and pops arguments. • Caller resumes computation…

More Related