Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.

Similar presentations


Presentation on theme: "Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer."— 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, 4 th 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 Organization3 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 Organization4 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 Organization5 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 Organization6 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 Organization7 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 Organization8 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 Note: jr is not restricted to the $ra register; may be used with other registers. PC + 4 = address of next instruction

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

10 ECE 445 - Computer Organization10 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 Organization11 The Stack 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 Stack Pointer

12 ECE 445 - Computer Organization12 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 Organization13 The Stack Pointer Stack Pointer ($sp)  Points to the last item pushed onto the stack.  Points to the last filled location on the stack. Stack $sp...... Push: places data on the stack Pop: “removes” data from the stack data lower addresses higher addresses 2048 2044 2040

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

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

16 ECE 445 - Computer Organization16 Example: Calling Sum1

17 ECE 445 - Computer Organization17 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 2000 1024 2004

18 ECE 445 - Computer Organization18 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 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 low address high address Stack...... data $sp e main

19 ECE 445 - Computer Organization19 Example: Calling CalculateF

20 ECE 445 - Computer Organization20 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 2000 1024 2004 Sum2( pa, pb, pc, pd, pe ) { px = pa + pb + pc + pd + pe*2; return( px ); } Prod1( px, pg ) { y = pg * px; return( y ); } 3000 4000 3004 4004

21 ECE 445 - Computer Organization21 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 CalculateF f = g*(a+b+c+d+e*2) Sum2 f = (a+b+c+d+e*2) Prod1 f = g*(sum) 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).

22 ECE 445 - Computer Organization22 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 low address high address Stack...... data $sp e g main Saved arguments from calling program

23 ECE 445 - Computer Organization23 CalculateF calls Sum2 low address high address Stack...... data $sp e g main Saved arguments from calling program saved return address ($ra of Main) saved registers local variables saved arguments procedure frame (activation record) of CalculateF The procedure frame (or activation record) is the space on the stack used by a function. e is pushed here by CalculateF 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

24 ECE 445 - Computer Organization24 Example: Calling a Procedure

25 5/30/2016Chapter 2 — Instructions: Language of the Computer 25 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 5/30/2016Chapter 2 — Instructions: Language of the Computer 26 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 Restore $s0 Result Return

27 Fall 2010ECE 445 - Computer Organization27 Questions?


Download ppt "Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer."

Similar presentations


Ads by Google