supporting functions procedures what is needed
play

Supporting Functions (procedures) What is needed? Functions: - PowerPoint PPT Presentation

Supporting Functions (procedures) What is needed? Functions: Analogy of a spy secret plan, acquire resources, perform task, cover tracks, return with result Program has to place params for functions access transfer


  1. Supporting Functions (procedures)

  2. What is needed? • Functions: Analogy of a spy • secret plan, acquire resources, perform task, cover tracks, return with result • Program has to • place params for function’s access • transfer control to procedure • acquire storage resources for the function • perform function’s instructions • place result for calling program’s access • return control to point of origin

  3. Using registers • Registers are fast! $a0 – $a3 : argument registers • $v0-$v1 : value registers • $ra : return address register •

  4. Jump-Link and Program Counter • Jump-and-link instruction jumps to addr, store next instruction’s addr in $ra : the return address • • jal ProcedureAddress • Program Counter ( PC ) • address of current instruction • � , jal stores PC + 4 in $ra to setup procedure return • � , another instruction: jr $ra • jumps to address in $ra

  5. Setup for Executing Functions • Caller puts params in $a0 – $a3 • Uses jal X to jump to callee procedure X • stores PC + 4 in $ra as well • Callee performs its instructions • Places results in $v0 – $v1 • Returns to caller by $jr $ra

  6. To summarize: 32

  7. Procedure needs more registers. Overwrite? • More than $a0 – $a4 & $v0,$v1 may be needed • $t0 – $t9, $s0 – $s7 • Those needed by caller must be restored to their original value • Use Stack for storing/restoring these registers Stack Pointer $sp Stack Last-In First-Out

  8. Push and Pop operations Push Push (grow) (shrink) LOW SP (subtract) SP SP (add) HIGH Stack (in memory) MIPS provides $sp

  9. Compiling a C “leaf” procedure int leaf_example (int g, int h, int i, int j) { int f; f = (g + h) - (i + j); return f; }

  10. The Stack Pointer $sp s 0 t 0 t 1 $sp $sp Before During After

  11. Temporary registers • $t0 - $t9 are not preserved by callee • $s0 - $s7 are preserved by callee • So only 8 registers need to be saved

  12. Nested procedures • A procedure may call another • Also, recursive procedure • May over-write arg. or return-address registers • Solution: push them on the stack as well

Recommend


More recommend