1 / 27

ECE 445 – Computer Organization

Procedure Calls and the Stack (Lectures #18). ECE 445 – Computer Organization. The slides included herein were taken from the materials accompanying Computer Organization and Design, 4 th Edition , by Patterson and Hennessey, and were used with permission from Morgan Kaufmann Publishers.

akiko
Download Presentation

ECE 445 – Computer Organization

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. Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer Organization and Design, 4th Edition, by Patterson and Hennessey, and were used with permission from Morgan Kaufmann Publishers.

  2. Material to be covered ... Chapter 2: Sections 8 – 9

  3. ECE 445 - Computer Organization Structured Programming Structured programming is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A structured program is composed of Main Program Subroutines (aka. functions and procedures)

  4. ECE 445 - Computer Organization Procedure Calling To execute a procedure, the program needs to: Pass parameters to the procedure (via registers) Transfer control to the procedure Acquire storage needed for the procedure Perform the desired function Return values to the calling procedure (via registers) Return control to the caller §2.8 Supporting Procedures in Computer Hardware

  5. ECE 445 - Computer Organization Registers and Procedure Calls Registers are faster than memory. Therefore, use registers to implement parameter passing and jumps. Registers $a0 - $a3 Parameter passing (from caller to callee) Registers $v0 - $v1 Return values (from callee to caller) Register $ra Return address Set by jal instruction; used by jr instruction

  6. ECE 445 - Computer Organization Registers and Procedure Calls Registers $s0 - $s7 preserved across a procedure call. Registers $t0 - $t9 not preserved across a procedure call. Special Purpose Registers $zero and $ra All other registers can be used for all purposes.

  7. ECE 445 - Computer Organization Registers and Procedure Calls MIPS Calling Convention Passes the first 4 arguments to a procedure in $a0 - $a3 Subsequent arguments passed on the stack Return values stored in $v0 and $v1

  8. ECE 445 - Computer Organization Special Jump Instructions Jump and Link (jal) Store PC + 4 in $ra $ra = return address register Uses the J-type Instruction Format Jump Register (jr) Copy $ra into PC Jumps to address in register $ra Returns from subroutine Uses the R-type Instruction Format PC + 4 = address of next instruction Note: jr is not restricted to the $ra register; may be used with other registers.

  9. ECE 445 - Computer Organization Example: Procedure Call main { .. proc1( arguments ); .. } proc1( parameters ) { .. proc2( arguments ); .. } proc2( parameters ) { .. .. } 1000 jal proc1 $ra = old PC+4 = 1028 1024 2000 jal proc2 $ra = old PC+4 = 2052 must save $ra = old PC_main before jumping to proc2. 2048 jr $ra 3000 jr $ra

  10. ECE 445 - Computer Organization The Stack The stack is located in memory. It is a Last-In, First-Out (LIFO) data structure. In the MIPS architecture it “grows” downward in memory, from higher addresses to lower addresses. The last item pushed on the stack is at the “top” The “top” of the stack is at the lowest address

  11. ECE 445 - Computer Organization The Stack Stack Pointer As data is pushed onto the stack it grows downward in memory – to lower addresses Push: places data on the stack Pop: “removes” data from the stack

  12. ECE 445 - Computer Organization The Stack is used when ... more than 4 parameters need to be passed to the procedure more than 2 values are returned by the procedure nested procedure calls are required registers must be saved across procedure calls excess spilling occurs

  13. ECE 445 - Computer Organization The Stack Pointer Stack Pointer ($sp) Points to the last item pushed onto the stack. Points to the last filled location on the stack. higher addresses Stack 2048 $sp data 2044 2040 . . . Push: places data on the stack Pop: “removes” data from the stack lower addresses

  14. ECE 445 - Computer Organization Push Operation higher addresses Stack 2048 $sp data [$t0] 2044 addi $sp, $sp, -4 sw $t0, 0($sp) $sp - 4 2040 . . . lower addresses

  15. ECE 445 - Computer Organization Pop Operation higher addresses Stack 2048 $sp + 4 data data [$t0] $sp 2044 lw $t0, 0($sp) addi $sp, $sp, 4 2040 . . . lower addresses

  16. ECE 445 - Computer Organization Example: Calling Sum1

  17. ECE 445 - Computer Organization Sum1: f = a+b+c+d+e main { .. f = Sum1( a, b, c, d, e ); .. print( f ); } Sum1( pa, pb, pc, pd, pe ) { pf = pa + pb + pc + pd + pe; return( pf ); } 1000 1024 2000 2004

  18. ECE 445 - Computer Organization Sum1: f = a+b+c+d+e lw $t0, 0($sp) add $t0, $t0, $a0 add $t0, $t0, $a1 add $t0, $t0, $a2 add $t0, $t0, $a3 add $v0, $t0, $zero jr $ra Main function calls Sum1 Arguments passed to Sum1 a, b, c, d → $a0 - $a3 e → last element on stack Sum1 calculates the sum using $t0 Resulting value returned to Main function in register $v0 Stack high address Sum1: # f=e # f=e+a # f=e+a+b # f=e+a+b+c # f=e+a+b+c+d # return value # return control data main e $sp . . . low address

  19. ECE 445 - Computer Organization Example: Calling CalculateF

  20. ECE 445 - Computer Organization CalculateF: f = g*(a+b+c+d+e*2) main { .. f = CalculateF( a, b, c, d, e, g ); .. print( f ); } CalculateF( pa, pb, pc, pd, pe, pg ) { x = Sum2( a, b, c, d, e ); pf = Prod1( x, g ); return( pf ); } 1000 3000 Sum2( pa, pb, pc, pd, pe ) { px = pa + pb + pc + pd + pe*2; return( px ); } Prod1( px, pg ) { y = pg * px; return( y ); } 1024 3004 2000 4000 2004 4004

  21. ECE 445 - Computer Organization CalculateF: f = g*(a+b+c+d+e*2) • The main program calls CalculateF to carry out the arithmetic operation. • CalculateF calls Sum2 to carry out the addition. • CalculateF then calls Prod1 to carry out the multiplication. Main calls Before calling Sum2: 1. Save return address (of main) on the stack. 2. Save argument e on the stack. Before returning to Main: 1. Restore return address (of main) from the stack. 2. Restore stack (i.e. stack pointer). CalculateF f = g*(a+b+c+d+e*2) calls calls Sum2 f = (a+b+c+d+e*2) Prod1 f = g*(sum)

  22. ECE 445 - Computer Organization Main calls CalculateF Main program pushes arguments (e and g) onto the stack. Main function executes jal CalculateF $ra = return address of Main PC = CalculateF Stack high address data e main $sp g Saved arguments from calling program . . . low address

  23. ECE 445 - Computer Organization CalculateF pushes return address of Main ($ra) onto the stack. CalculateF pushes argument (e) onto the stack. CalculateF executes jal Sum2 $ra = return address of CalculateF PC = Sum2 CalculateF calls Sum2 Stack high address data e main Saved arguments from calling program g saved return address ($ra of Main) procedure frame (activation record) of CalculateF saved registers local variables The procedure frame (or activation record) is the space on the stack used by a function. $sp saved arguments . . . e is pushed here by CalculateF low address

  24. ECE 445 - Computer Organization Example: Calling a Procedure

  25. Chapter 2 — Instructions: Language of the Computer Example: Procedure Call C code: int leaf_example (int g, h, i, j){ int f; f = (g + h) - (i + j); return f;} Arguments g, …, j in $a0, …, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0

  26. Chapter 2 — Instructions: Language of the Computer Example: Procedure Call MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return

  27. ECE 445 - Computer Organization Questions?

More Related