CSE Lecture In Class Example Handout

Similar documents
CSE Lecture In Class Example Handout

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Functions in MIPS. Functions in MIPS 1

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Lecture 5. Announcements: Today: Finish up functions in MIPS

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Today. Putting it all together

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Course Administration

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

ECE260: Fundamentals of Computer Engineering

MIPS Functions and the Runtime Stack

Instruction Set Architectures (4)

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

MIPS Functions and Instruction Formats

ECE232: Hardware Organization and Design

Lecture 5: Procedure Calls

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

SPIM Procedure Calls

Lecture 5: Procedure Calls

CS64 Week 5 Lecture 1. Kyle Dewey

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Instructions: Assembly Language

Review of Activation Frames. FP of caller Y X Return value A B C

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Chapter 2A Instructions: Language of the Computer

Functions and Procedures

COMP2611: Computer Organization MIPS function and recursion

MIPS Assembly (Functions)

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Lectures 3-4: MIPS instructions

Implementing Procedure Calls

Lecture 7: Procedures and Program Execution Preview

CS 61c: Great Ideas in Computer Architecture

Thomas Polzer Institut für Technische Informatik

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

EE 361 University of Hawaii Fall

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

ECE369. Chapter 2 ECE369

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Procedure Call and Return Procedure call

Lecture 7: Procedures

CENG3420 Lecture 03 Review

ECE331: Hardware Organization and Design

MIPS Procedure Calls. Lecture 6 CS301

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CS 316: Procedure Calls/Pipelining

CS/COE1541: Introduction to Computer Architecture

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CS61C : Machine Structures

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

CS61C : Machine Structures

We can emit stack-machine-style code for expressions via recursion

Compilers and computer architecture: A realistic compiler to MIPS

Anne Bracy CS 3410 Computer Science Cornell University

Code Generation. Lecture 12

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Storage in Programs. largest. address. address

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Computer Architecture

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CS153: Compilers Lecture 8: Compiling Calls

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Anne Bracy CS 3410 Computer Science Cornell University

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

CA Compiler Construction

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

ECE 30 Introduction to Computer Engineering

ECE260: Fundamentals of Computer Engineering

Lecture 6: Assembly Programs

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

More C functions and Big Picture [MIPSc Notes]

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Compiling Code, Procedures and Stacks

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Transcription:

CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A [ i+1]) // $s0 = A swap (&A[i], &A[i+1]) // $t0 = 4*i We will assume that: - The address of A is contained in $s0 ($16) - The index (4 x i) will be contained in $t0 ($8) Answer: The Caller: // Calculate address of A(i) add $s1, $s0, $t0 // $s1 address of array element i in $s1 // Load data lw $t2, 0($s1) // load A(i) into temporary register $t2 lw $t3, 4($s1) // load A(i+1) into temporary register $t3 // Check condition ble $t2, $t3, else // is A(i) <= A(i+1)? If so, don t swap // if >, fall through to here addi $a0, $t0, 0 // load address of x into argument register (i.e. A(i)) addi $a1, $t0, 4 // load address of y into argument register (i.e. A(i+1)) else: // Call Swap function jal swap // PC address of swap; $ra $31 = PC + 4 // Note that swap is generic i.e. b/c of the way data is passed in, we do not assume the values // to be swapped are in contiguous memory locations so two distinct physical addresses are // passed in Swap: lw $t0, 0($a0) // $t0 = mem(x) use temporary register lw $t1 0($a1) // $t1 = mem(y) use temporary register sw 0($a0), $t1 // do swap sw 0($a1), $t0 // do swap jr $ra // $ra should have PC = PC + 4 // PC = PC + 4 should be the next address after jump to swap

Part B: Procedures with Callee Saving (old exam question): Assume that you have written the following C code: //------------------------------------------------------------------- int variable1 = 10; // global variable int variable2 = 20; // global variable //------------------------------------------------------------------- int (void) { int i = 1; // assigned to register s0 int j = 2; // assigned to register s1 int k = 3; // assigned to register a3 int m; int n; m = addfournumbers(i, j); n = i + j; // 1 + 2 = 3 printf( m is %d\n, m); // printf modifies no registers printf( n is %d\n, n); // printf modifies no registers printf( k is %d\n, k); // printf modifies no registers //--------------------------------------------------------------------- int addfournumbers(int x, int y) { int i; // assigned to register s0 int j; // assigned to register s1 int k; // assigned to register s2 i = x + y; // 1 + 2 = 3 j = variable1 + variable2; // 10 + 20 = 30 k = i + j; // 3 + 30 = 33 return k; //--------------------------------------------------------------------- The output of the printf statements in is: m is 33 n is 3 k is 3 Assume this program was compiled into MIPS assembly language with the register conventions described on Slide 12 of Lecture 07/08. Also, note that in the comments of the program, I have indicated that certain variables will be assigned to certain registers when this program is compiled and assembled. Using a callee calling convention, answer the questions below:

Q-i: Ideally, how many arguments to the function addfournumbers must be saved on the stack? 0. By default, arguments should be copied into registers. Q-ii: What (if anything) should the assembly language for () do right before calling addfournumbers? Copy values of s registers into argument registers; save value of k (in $a3) onto the stack Q-iii: What is the first thing that the assembly language for addfournumbers should do upon entry into the function call? Callee save the s registers Q-iv: What is the value of register number 2 (i.e. 0010 2 ) after completes (assuming there were no other function calls, no interrupts, no context switches, etc.) 33. Register 2 = v0. It should not have changed. (different answer if you assume printf returns value) Q-v: Does the return address register ($ra) need to be saved on the stack for this program? Justify your answer. (Assume () does not return). No if no other procedures are called.

Part C: Procedures with Callee Saving (old exam question): Assume you have the following C code: int (void) { int x = 10; # x maps to $s1 int y = 20; # y maps to $s2 int z = 30; # z maps to $s3 int a; # a maps to $t0 int b; # b maps to $t1 int c; # c maps to $t2 c = x + y; a = multiply(x, z); b = c + x; int multiply(int a, int b) { int q; # q maps to $t0 int z; # z maps to $t1 q = add(); z = a*b; return z; int add() { int m = 5; # m maps to $t4 int n = 4; # n maps to $t5 int y; y = a + b; return y; Assuming the MIPS calling convention, answer questions A-E. Note no assembly code/machine instructions are required in your answers; simple explanations are sufficient.

Q-i: What, if anything must () do before calling multiply? - Save $t2 to stack, needed upon return. - Also, copy $s1 to $a0 and copy $s3 to $a1 Q-ii: Does multiply need to save anything to the stack? If so, what? - $31 - The s registers associated with () - The argument registers passed into multiply before calling add() Q-iii: Assume that multiply returns its value to () per the MIPS register convention. What machine instructions might we see at to completely facilitate the function return? - We have to copy the value in $t1 to $2. - We would call a jal instruction - We would adjust the stack pointer, restored saved registers. Q-iv: What line of code should the return address register point to at? - b = c + x Other answers were considered correct based on stated assumptions. Q-v: What line of code should the return address register point to at? - b = c + x Other answers were considered correct based on stated assumptions.

Part D: More Complex Example: Let s write the MIPS code for the following: for(i=1; i<5; i++) { int function(int, int) { Assume: A(i) = B*d(i); A(i) = A(i-1); Addr. of A = $18 if(d(i) >= e) { e = A(i); Addr. of d = $19 e = function(a,i); return e; B = $20 e = $21 Question/Comment My Solution Comment 1 st, want to initialize loop variables. What registers should we use, how should we do it? addi $16, $0, 1 addi $17, $0, 5 (We pass in starting address of A and i ) # Initialize i to 1 # Initialize $17 to 5 (in both cases, saved registers are used we want this data available post function call) 2 nd, calculate address of d(i) and load. What kind of registers should we use? Loop: sll $8, $16, 2 add $8, $19, $8 lw $9, 0($8) # store i*4 in $8 (temp register OK) # add start of d to i*4 to get address of d(i) # load d(i) needs to be in register to do math Calculate B*d(i) mult $10, $9, $20 # store result in temp to write back to memory Calculate address of A(i) sll $11, $16, 2 add $11, $11, $18 CANNOT do: add $11, $8, $18 # Same as above # We overwrote # But, would have been better to save i*4 Why? Lower CPI Store result into A(i) sw $10, 0($11) # Store result into a(i) Now, need to check whether or not d(i) >= e. How? Assume no ble. slt $1, $9, $22 bne $0, $1, start again # Check if $9 < $22 (i.e. d(i) < e) # Still OK to use $9 not overwritten # (temp does not mean goes away immediately) # if d(i) < e, $1 = 1 # if d(i) >= e, $1 = 0 (and we want to call function) # (if $1!= 0, do not want to call function)

Given the above setup, what comes next? (Falls through to the next function call). Assume argument registers, what setup code is needed? add $4, $18, $0 add $5, $16, $0 x: jal function # load address of (A) into an argument register # load i into an argument register # call function; $31 x + 4 (if x = PC of jal) Finish rest of code: What to do? Copy return value to $21. Update counter, check counter. Where is start again at? add $21, $0, $2 sa: addi $16, $16, 1 bne $16, $17, loop # returned value reassigned to $21 # update i by 1 (array index) # if i < 5, loop A better way: Could make array index multiple of 4 Assume you will reference A(i-1) with lw 0($x). What 4 instruction sequence is required? func: subi $5, $5, 1 sll $8, $5, $2 add $9, $4, $8 lw $10, 0($9) Function Code # subtract 1 from i # multiply i by 4 note # add start of address to (i-1) # load A(i-1) Finish up function. sw $10, 4($9) add $2, $10, $0 # store A(i-1) in A(i) # put A(i-1) into return register ($2) Return jr $31 # PC = contents of $31

Part E: Nested Function Calls int (void) { foo1() { foo2() { i = 5; # i = $16 a = 17; # a = $16 x = 25; # x = $16 j = 6; # j = $17 b = 24; # b = $17 y = 12; # y = $17 k = foo1(); j = j + 1; foo2(); Let s consider how we might use the stack to support these nested calls. Question: How do we make sure that data for i, j ($16, $17) is preserved here? Answer: Use a stack. By convention, the stack grows up: Low Memory Address (0)! $sp! Let s look at (): - Assume we want to save $17 and $16 o (we ll use the stack pointer) - Also, anything else we want to save? o $31 if nested calls. - How? o subi $sp, $sp, 12 # make space for 3 data words High Memory Address (N)! o Example: assume $sp = 100, therefore $sp = 100 12 = 88 - Then, store results: $sp new! 88! o sw 8($sp), $16 # address: 8 + $sp = 8 + 88 = 96 o sw 4($sp), $17 # address: 4 + $sp = 4 + 88 = 92 o sw 0($sp), $31 # address: 0 + $sp = 0 + 88 = 88 92! 96! $sp old! 100! Now, in Foo1() assume A and B are needed past Foo2() how do we save them? - We can do the same as before o Update $sp by 12 and save Similarly, can do the same for Foo2()

Now, assume that we are returning from Foo1() to (). What do we do? - The stack pointer should equal the value before the Foo1() call (i.e. 88) lw $31, 0($sp) # $31 memory(0 + 88) (LIFO) lw $17, 4($sp) # $17 memory(4 + 88) lw $16, 8($sp) # $16 memory(8 + 88) Finally, update $sp: addi $sp, $sp, 12 ($sp now = 100 again) Let s talk about the Frame Pointer too: (2) On function call, $sp decremented! (3) Also a register called Frame Pointer! (1) $sp! (frame pointer) points to the beginning of the stack (ish) or the first word in frame of a procedure Why use a? - Stack used to store variables local to procedure that may not fit into registers - $sp can change during procedure (e.g. as just seen) o Results in different offsets that may make procedure harder to understand - is stable base register for local memory references For example: $sp (in foo1() = 88)! $16! $17! (might be here)! $31! generally > $sp! More Generally:! Low Memory Address (0)! $sp! Saved saved regs!! Saved $ra,! Saved args! High Memory Address (N)! -"Therefore procedure might reference extra function argument as 0()! -"What if 2 saved arguments? What next?! -"With this convention: lw $t0, 4()! Because $sp can change dynamically, often easier/intuitive to reference extra arguments via stable although can use $sp with a little extra math

Part F: Recursive Function Calls 2 (n<1 check)! 1 (immediately inside function)! 4 (put "1# in return register)! 5 (return)! 3 (recursive function call)! 6 (multiply function values)! Let s consider how we might use the stack to support these nested calls. We ll also make use of the frame pointer (). Code Section # Address Label MIPS Instruction Comments 1 0 Fact: subi $sp, $sp, 12 Make room for 3 pieces of data on the stack;, $sp, and 1 local argument 4 sw 8($sp), $ra If $sp = 88, M(88 + 8) value of $ra 8 sw 4($sp), If $sp = 88, M(88 + 4) value of 12 subi,, 12 Update the frame pointer 2 16 bgtz $a0, L2 If N > 0 (i.e. not < 1) we re not done we assume N is in $a0 4 20 addi $v0, $0, 1 We eventually finish and want to return 1, therefore put 1 in return register 24 j L1 Jump Jump to to return return code code 3 3 28 28 L2: L2: sw sw $a0, $a0, 0() 0() Save Save argument N to N to stack stack (we!ll (we ll need need it when it when we we return) return) 32 32 subi subi $a0, $a0, $a0, $a0, 1 1 Decrement N (N N (N = N = N 1), 1), put put result result in in $a0 $a0 36 36 jal jal Fact Fact Call Call Factorial() again again 6 6 40 40 lw lw $t0, $t0, 0($f0) 0($f0) Load Load N (saved N at at *** *** to to stack) stack) 44 44 mult mult $v0, $v0, $v0, $v0, $t0 $t0 Store Store result result in in $v0 $v0 5 5 48 48 L1: L1: lw lw $ra, $ra, 8($sp) 8($sp) Restore return return address 52 52 lw lw,, 4($sp) 4($sp) Restore frame frame pointer pointer 56 56 addi addi $sp, $sp, $sp, $sp, 12 12 Pop Pop stack stack 60 60 jr $ra jr $ra Return Return from from factorial

Part B: PLACE COMPLETED FACTORIAL UP ON THE SCREEN. 5 If so, store old value of N (data 8a More of 9 $ra is in 10a More Let!s walk through this code (and memory) assuming that we call the factorial function with the number that needs to be saved), ref! the same! factorial! of the same! 2. For your reference, I!ve included tables below that you can use to keep track of both instruction execution Code Part B: Trace: PLACE and the COMPLETED contents of memory. FACTORIAL We!ll UP assume ON THE that SCREEN. $sp is initially 112 and that is 124. Let!s 1 st walk through this code (and memory) Call to Factorial 2 nd assuming that we call the factorial Call to Factorial 3 rd function with the number Call to Factorial Code 2. For your reference, I!ve included tables below that you can use to keep track of both instruction Addr Trace: What Happens Addr What Happens Addr What Happens execution 0 $sp and = $sp-12; the contents $sp! 100 of memory. 0 We!ll $sp = assume $sp-12; that $sp! $sp 88 is initially 0 112 $sp and = $sp-12; that $sp is 124. 1 st Call to Factorial 2 nd Call to Factorial 3 rd Call to Factorial! 76 4 N > 1?! Addr 4 What M(100+8) Happens = M(108)! $ra Addr 4 What M(96) Happens! $ra ($ra=40) Addr 4 What M(84) Happens! $ra ($ra=40) Code 08 M(100+4) Trace: = M(104) 8 M(92)! ( =112) 8 M(80)! ( =100) 12 1 st $sp = $sp-12; $sp! 100 0 Call to = -12; Factorial! 112 12 2 nd $sp = $sp-12; $sp! 88 0 Call to = -12; Factorial! 100 312 rd $sp = $sp-12; $sp! 76 4 M(100+8) = M(108)! $ra 4 M(96)! $ra ($ra=40) 4 Call M(84) to = -12; Factorial! $ra ($ra=40)! 88 816 Addr M(100+4) 2 What is greater Happens = than M(104) 0! 816 Addr M(92) 1 What is greater! Happens than ( 0 =112) Addr 816 What M(80) 0 is NOT Happens! greater ( than =100) 0 12 28 0 M( $sp = = -12; / $sp-12; 112)! N $sp (store!! 112 100 #) 12 28 0 M( $sp = -12; / $sp-12; 100)! N $sp (store! 100 88 #) 012 $sp (start = -12; to $sp-12; return) $sp! 88 76 16 32 4 2 N M(100+8) is = greater N-1 (new = than M(108) arg 0 = 1)! $ra 16 32 4 1 N M(96) is = greater N-1!(new $ra than arg ($ra=40) 0 = 41610c Now M(84) 0 is meet NOT! $ra exit greater criteria! ($ra=40) than 0 28 36 8 M( jal M(100+4) Fact / 112) ($ra = =! M(104) 40 N (store )! #) 28 36 8 M( jal M(92) Fact /! 100) ($ra =! 40 ( N 10 (store ) =112) #) 8 M(80) (start to! return) ( =100) 32 12 N = N-1 = -12; (new arg =! 1) 112 32 12 N = = N-1-12; (new arg =! 0) 100 12 = -12;! 88 6, 7 Calculate number to pass to 12 Restore saved variable, calculate value to return: $v0 36 16 jal 2 Fact is greater ($ra = than 40 0 16 1 is greater than 0 16 0 is NOT greater than 0 function, call factorial again! 10 ) 36 jal Fact ($ra = 40 10 ) from old call, stored N; calculated value becomes $v0! 28 M( / 112)! N (store #) 28 M( / 100)! N (store #) (start to return) 32 N = N-1 (new arg = 1) 32 N = N-1 (new arg = 0) Return from 3 rd Call Return from 2 nd Call Return form 1 st Call 36 jal Fact ($ra = 40 10 ) 36 jal Fact ($ra = 40 10 ) Addr What Happens Addr What Happens Addr What Happens 20 addi $v0, $0, Return from 3 rd 1 40 lw $t0, 0(); Call Return from 2 nd 40 lw $t0, 0(); (return 1) $t0! M(100); $t0 Call! 1 Return $t0 form! M(112); 1 st Call $t0! 2 24 Addr j What L1 Happens 44 Addr $v0 What! Happens 1x1 Addr 44 What $v0! Happens 1x2 24 j L1 40 $v0 lw $t0, = return 0(); address reg. 40 lw $v0 $t0,! 0(); $v0 x $t0 48 52 $ra! M($sp+8) M($sp+4)! M(84) M(80) 48 52 $ra $t0! M($sp+8) M($sp+4) M(100); $t0!! M(96) M(92) 1 48 52 $t0 $ra! M(112); M($sp+8) M($sp+4) $t0! M(108) M(104) 2 48 $ra $ra! 40 100 M($sp+8)! M(84) 44 $ra $v0! 40 112 1x1 44 $v0 $ra! factorial factorial 1x2 caller caller RA FP 52 56 $sp $ra! = 76+12; M($sp+4) 40 $sp! M(80) 88 52 56 $sp $v0! = return M($sp+4) 88 + 12 address =! 100 M(92) reg. 52 56 $v0 $sp! M($sp+4) $v0 100 x + $t0 12 =! 112 M(104) 52 (pop! stack) 100 M($sp+4)! M(80) 48 $ra! 112 M($sp+8)! M(96) 48 $ra! M($sp+8) factorial caller! M(108) FP 56 60 $sp jr $ra =! implies 76+12; 100 that $sp PC! 88! 40 56 60 $sp jr $ra $ra! makes: 40 88 + 12 PC = 100! 40 56 60 $ra $sp jr $ra! (PC factorial 100 + + 4 12 of caller = fact 112 RA caller) 56 (pop $sp stack) = 76+12; $sp! 88 52! M($sp+4)! M(92) 52! M($sp+4)! M(104) (pop stack)! 112! factorial caller FP Memory 60 jr $ra Contents: implies that PC (Assume! 40 () 60 calls jr $ra function makes: PC which! 40 calls factorial.) 60 jr $ra (PC + 4 of fact caller) 60 jr $ra implies that Memory Before 1 st PC! 40 During 1 st 56 $sp! 88 During 2 nd + 12 = 100 During 3 rd 56 $sp! 100 + 12 = 112 10d Undo stack pushes, 11 Go back 60 jr 13a $ra Return makes: as PC before!! 40 Return 60 14a Calculate Return Return Address Fact Call Fact Call Fact Call Fact Call from 3 rd jr $ra (PC next + from 2 nd 4 value of fact to caller) return! Memory restore $ra, Contents:! (Assume to jal () + 4! calls function which calls factorial.) from 1 st 76 Current $sp Memory Contents: Before 1 st (Assume During () 1 st During calls function 2 nd During which calls 3 rd factorial.) Return Return Return Address 80 Saved Memory Fact Before Call 1 st Fact During Call 1 st Fact During Call 2 nd Fact from During Call prior call 3 rd from Return 3 rd Return from 2 nd Return from 1 st 76 Current $sp Address Fact Call Fact Call Fact Call (100) Fact Call from 3 rd from 2 nd from 1 st 80 Saved 84 Saved $ra of 76 Current $sp from fact (40) prior call 88 80 Saved Current $sp (100) Current $sp 3 rd fact 10b More of from prior call 84 Saved N never $ra stored of the same,! (100) 92 Saved fact (40) $ra = 40! 84 Saved $ra of 88 Current from prior $sp call Current $sp 3 rd fact fact (40) (112) N never stored 88 Current $sp Current $sp 3 rd fact 92 Saved 96 Saved 10e Pop Stack! $ra of from N never stored 1 make room fact (40) prior call 92 Saved 100 for $sp, Current, $sp (112) N! Current from prior call $sp 2 nd fact 96 Saved N (112) = 1 $ra of 104 Saved of fact (40) 96 Saved $ra of 100 Current function calling $sp Current $sp 2 nd fact 13b Pop Stack! fact (40) fact (124) N = 1 100 Current $sp Current $sp 2 nd fact 104 Saved 108 Saved $ra of of function N = 1 function calling calling 104 fact Saved of fact (124) function calling 108 112 2 save Current $ra, $sp Saved Current $ra fact (124) of 8a More of $sp 1 st fact function N = 2 calling the same, 108 (prep for new call)! Saved $ra of 116 Saved of fact $ra = 40! function calling 112 Current $sp Current $sp 1 st fact fact 120 Saved $ra of N = 2 112 Current $sp Current $sp 1 st fact 116 Saved of N = 2 3 update to 116 Saved of define start of call 120 Saved $ra of frame! 120 Saved $ra of 14b Pop Stack; restore 124 Current Callee saving! address of function that called factorial! 0 Main() calls function which calls factorial!