More MIPS: Recursion. Computer Science 104 Lecture 9



Similar documents
Computer Systems Architecture

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Intel 8086 architecture

Instruction Set Architecture

MIPS Assembly Code Layout

Reduced Instruction Set Computer (RISC)

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Assembly Language: Function Calls" Jennifer Rexford!

Translating C code to MIPS

CSE 141 Introduction to Computer Architecture Summer Session I, Lecture 1 Introduction. Pramod V. Argade June 27, 2005

Typy danych. Data types: Literals:

Review: MIPS Addressing Modes/Instruction Formats

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Machine-Code Generation for Functions

Introduction to MIPS Assembly Programming

Assembly Language Programming

Instruction Set Design

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Stack Allocation. Run-Time Data Structures. Static Structures

Instruction Set Architecture

5 MIPS Assembly Language

X86-64 Architecture Guide

EC 362 Problem Set #2

Compilers I - Chapter 4: Generating Better Code

Computer Architectures

In the Beginning The first ISA appears on the IBM System 360 In the good old days

umps software development

Instruction Set Architecture (ISA) Design. Classification Categories

MIPS Assembler and Simulator

Computer Organization and Architecture

LSN 2 Computer Processors

Recursive Fibonacci and the Stack Frame. the Fibonacci function. The Fibonacci function defines the Fibonacci sequence, using a recursive definition :

A deeper look at Inline functions

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Figure 1: Graphical example of a mergesort 1.

MICROPROCESSOR AND MICROCOMPUTER BASICS

Instruction Set Architecture (ISA)

The Operating System and the Kernel

Code Generation & Parameter Passing

Processor Architectures

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

Chapter 5 Instructor's Manual

Computer Architecture Lecture 3: ISA Tradeoffs. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 1/18/2013

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Lab Work 2. MIPS assembly and introduction to PCSpim

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

Chapter 7D The Java Virtual Machine

Lecture 22: C Programming 4 Embedded Systems

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

A Tiny Guide to Programming in 32-bit x86 Assembly Language

10CS35: Data Structures Using C

COS 318: Operating Systems

İSTANBUL AYDIN UNIVERSITY

CS2210: Compiler Construction. Runtime Environment

Introduction to Data Structures

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Computer Organization and Components

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Extending the swsusp Hibernation Framework to ARM. Russell Dill

C Compiler Targeting the Java Virtual Machine

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Computer Architectures

CPU Organization and Assembly Language

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

C Programming Review & Productivity Tools

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

An Introduction to the ARM 7 Architecture

CS201: Architecture and Assembly Language

ECE 122. Engineering Problem Solving with Java

CSC 2405: Computer Systems II

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Design of Pipelined MIPS Processor. Sept. 24 & 26, 1997

Chapter 2. Binary Values and Number Systems

Lecture 17: Virtual Memory II. Goals of virtual memory

HC12 Assembly Language Programming

Introduction to MIPS Programming with Mars

Analysis of a Search Algorithm

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Common Data Structures

arrays C Programming Language - Arrays

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

x64 Cheat Sheet Fall 2015

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Transcription:

More MIPS: Recursion Computer Science 104 Lecture 9

Admin Homework Homework 1: graded. 50% As, 27% Bs Homework 2: Due Wed Midterm 1 This Wed 1 page of notes 2

Last time What did we do last time? 3

Last time What did we do last time? More MIPS! Functions: jal jr Calling conventions Stack Frames, saving registers Worked bubble sort example 4

Review: MIPS Registers 0 zero constant 0 1 at reserved for assembler 2 v0 expression evaluation & 3 v1 function results 4 a0 arguments 5 a1 6 a2 7 a3 8 t0 temporary: caller saves... 15 t7 16 s0 callee saves... 23 s7 24 t8 temporary (cont d) 25 t9 26 k0 reserved for OS kernel 27 k1 28 gp Pointer to global area 29 sp Stack pointer 30 fp frame pointer 31 ra Return Address (HW) 5

Review: Calling a function Calling Procedure Step-1: Setup the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack (in reverse order arg5 is at the top of the stack). Step-2: Save caller-saved registers Save registers $t0-$t9 if they contain live values at the call site. Step-3: Execute a jal instruction. 6

Called Routine Review: Callee setup Step-1: Establish stack frame. Subtract the frame size from the stack pointer. addiu $sp, $sp, - <frame-size> Typically, minimum frame size is 32 bytes (8 words). Step-2: Save callee saved registers in the frame. Register $fp is always saved. Register $ra is saved if routine makes a call. Registers $s0-$s7 are saved if they are used. Step-3: Establish Frame pointer Add the stack <frame size> - 4 to the address in $sp addiu $fp, $sp, <frame-size> - 4 7

On return from a call Review: Returning Step-1: Put returned values in registers $v0. (if a value is returned) Step-2: Restore callee-saved registers. Restore $fp and other saved registers. [$ra, $s0 - $s7] Step-3: Pop the stack Add the frame size to $sp. addiu $sp, $sp, <frame-size> Step-4: Return Jump to the address in $ra. jr $ra 8

Today More MIPS! Recursion Won t be required on the exam But you could use recursion if you want And good MIPSing practice anyways Extra time? I ll work example problems, answer review questions, etc 9

Recursion Why do we want recursion? Because recursion is a wonderful thing! Canonical recursion example? Factorial 10

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } int x = fact(3); 11

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } main x??? int main (void) { int x = fact(3); 12

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } main x??? factorial n 3 return C0 C0 int main (void) { int x = fact(3); 13

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } main x??? factorial n 3 return C0 C0 int main (void) { int x = fact(3); 14

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 15

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 16

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 factorial n 1 return C1 17

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 factorial n 1 return C1 18

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 factorial n 1 return C1 factorial n 0 return C1 19

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 factorial n 1 return C1 factorial n 0 return C1 20

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); fact returned 1 factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 factorial n 1 return C1 21

int fact (int n) { if (n <= 0) { Factorial in C main x??? } } return 1; C1 return n * fact (n 1); fact returned 1 factorial n 3 return C0 factorial C0 int main (void) { int x = fact(3); n 2 return C1 22

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } fact returned 2 main x??? factorial n 3 return C0 C0 int main (void) { int x = fact(3); 23

Factorial in C int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } main x 6 int main (void) { int x = fact(3); 24

int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } Observe: Parameter n in $a0 Need to put n-1 in $a0 to call Need n after call int main (void) { int x = fact(3); 25

int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } Observe: Parameter n in $a0 Need to put n-1 in $a0 to call Need n after call Conclusion: Need to move n to other reg Which one: $t0 or $s0? int main (void) { int x = fact(3); 26

int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } int main (void) { int x = fact(3); Observe: Parameter n in $a0 Need to put n-1 in $a0 to call Need n after call Conclusion: Need to move n to other reg Which one: $t0 or $s0? Would need save/restore => May as well use $s0 27

Convert C to Assembly int fact (int n) { if (n <= 0) { return 1; } return n * fact (n 1); } (We ll switch to emacs For this part) int main (void) { int x = fact(3); 28

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0003 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 1234 ABCD FF80 FFCC $ra 4000 Addr FF78 FF74 FF70 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 Value 29

Addr Value <<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 FF78 FF74 FF70 FF68 FF64 FF60 $a0 0003 $s0 1234 $v0 ABCD $sp FF80 $fp FFCC $ra 4000 FF58 FF54 FF50 FF48 FF44 FF40 30

Addr Value <<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 FF78 FF74 FF70 FF68 FF64 FF60 $a0 0003 $s0 1234 $v0 ABCD $sp FF70 $fp FFCC $ra 4000 FF58 FF54 FF50 FF48 FF44 FF40 31

Addr Value <<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 FF78 FF74 FF70 FF68 FF64 FF60 FFCC $a0 0003 $s0 1234 $v0 ABCD $sp FF70 $fp FFCC $ra 4000 FF58 FF54 FF50 FF48 FF44 FF40 32

<<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 $a0 0003 $s0 1234 $v0 ABCD $sp FF70 $fp FFCC $ra 4000 FF78 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 33

<<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 $a0 0003 $s0 1234 $v0 ABCD $sp FF70 $fp FFCC $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 34

<<Frame Setup>> addiu $sp, $sp, -16 sw $fp, 0($sp) sw $ra, 4($sp) sw $s0, 8($sp) addiu $fp, $sp, 12 $a0 0003 $s0 1234 $v0 ABCD $sp FF70 $fp $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 35

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0003 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 1234 ABCD FF70 $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 36

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0003 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 ABCD FF70 $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 37

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0003 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 ABCD FF70 $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 38

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0002 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 ABCD FF70 $ra 4000 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 39

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0002 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 ABCD FF70 $ra 1044 FF68 FF64 FF60 FF58 FF54 FF50 FF48 FF44 FF40 40

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0002 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 ABCD FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 FF54 FF50 FF48 FF44 FF40 41

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0002 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 ABCD FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 FF54 FF50 FF48 FF44 FF40 42

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0002 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 ABCD FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 FF54 FF50 FF48 FF44 FF40 43

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0001 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 ABCD FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 FF54 FF50 FF48 FF44 FF40 44

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0001 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 ABCD FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 FF54 FF50 FF48 FF44 FF40 45

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0001 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 ABCD FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 FF44 FF40 46

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0001 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 ABCD FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 FF44 FF40 47

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0001 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 ABCD FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 FF44 FF40 48

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 ABCD FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 FF44 FF40 49

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 ABCD FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 FF44 FF40 50

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 ABCD FF40 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 51

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0000 ABCD FF40 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 52

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0000 ABCD FF40 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 53

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0000 0001 FF40 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 54

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0000 0001 FF40 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 55

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 0000 $v0 0001 $sp FF40 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 56

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 0001 $v0 0001 $sp FF40 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 57

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 0001 $v0 0001 $sp FF40 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 58

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 0001 $v0 0001 $sp FF40 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 59

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) Factorial in Assembly addiu $sp, $sp, 16 Notice how $sp and $fp describe the callers frame now Reg Value $a0 0000 $s0 0001 $v0 0001 $sp $fp FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 60

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 0001 FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 61

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 0001 FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 62

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0001 0001 FF50 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 63

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 0001 FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 64

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 0001 FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 65

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0002 0002 FF60 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 66

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 0002 FF70 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 67

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 0002 FF70 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 68

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 0003 0006 FF70 $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 69

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 0003 $v0 0006 $sp FF70 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 70

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 1234 $v0 0006 $sp FF70 $fp $ra 1044 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 71

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 1234 $v0 0006 $sp FF70 $fp $ra 4000 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 72

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 1234 $v0 0006 $sp FF70 $fp FFCC $ra 4000 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 73

<<Frame Cleanup>> lw $s0, 8($sp) lw $ra, 4($sp) lw $fp, 0($sp) addiu $sp, $sp, 16 $a0 0000 $s0 1234 $v0 0006 $sp FF80 $fp FFCC $ra 4000 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 74

fact: <<frame setup>> move $s0, $a0 blez $s0, factendzero addi $a0, $a0, -1 jal fact # Addr 1040 mul $v0, $v0, $s0 factret: <<frame cleanup>> $a0 0000 jr $ra factendzero: li $v0, 1 b factret $s0 $v0 $sp $fp 1234 0006 FF80 FFCC $ra 4000 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 75

main: li $a0, 3 jal fact move $a0, $v0 Value returned in $v0 Stack /callee saves restored Factorial in Assembly Reg Value $a0 0000 $s0 1234 $v0 0006 $sp $fp FF80 FFCC $ra 4000 FF68 0003 FF64 1044 FF60 FF58 0002 FF54 1044 FF50 FF48 0001 FF44 1044 FF40 76

Recursion De-mystified? Recursion: Assembly: not required on midterm1 Generally good to know Hopefully de-mystified? P.S. Some languages only have recursion 77

Other ISAs We ve been studying MIPS x86: Intel, AMD very common, kind of ugly Variable length insns (1-22 bytes) Very complex insns Not a load-store ISA (can do mem + reg -> mem) PowerPC more like MIPS ( RISC ) Has some not-so RISC things: load-with-update ARM Good to know others exist, but our focus is MIPS 78

Remaining Time: Work Examples, Answer?s With any remaining time I ll Work examples (write C, asm, do binary math..) Answer questions Whatever. 79