MIPS function continued

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
The University of Adelaide, School of Computer Science
MIPS Function Continued
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
.text fact: addiu $sp, $sp, -32 sw $ra, 20($sp) sw $fp, 16($sp) addiu $fp, $sp, 28 sw $a0, 0($fp) bgtz $a0, L2 li $v0, 1 b suffix L2: addi $a0, $a0,
Procedure call frame: Hold values passed to a procedure as arguments
Solution to Problem Recitation #1 (Week 2) ECEN350 Prof. Choi.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedures 2a MIPS code examples Making Use of a Stack.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
CPEG323 Homework Review I Long Chen October, 17 th, 2005.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
CSCI206 - Computer Organization & Programming
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Procedures 101: There and Back Again
MIPS Procedures.
Lecture 4: MIPS Instruction Set
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
MIPS Procedures.
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
MIPS Instructions.
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
MIPS Functions.
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
MIPS function continued
Computer Architecture Procedure Calls
Systems Architecture I
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
MIPS Functions.
MIPS function continued
Presentation transcript:

MIPS function continued

Recursive functions So far, we have seen how to write A simple function A simple function that have to use the stack to save values A function that calls another function We are going to see how to write a recursive function

Recursive functions Recursive relations naturally exist, such as calculating the factorial of a number: n! = n * (n-1) * (n-2) … *2 * 1 = n* (n-1)!

Recursive functions How do we write the recursive relation down in programs? In C, it will look like: int fact(int n) { int res; if (n <= 1) res = 1; else res = n * fact(n-1); return res; }

Implementing a Recursive Function It is a recursive function – a function that calls itself. It will keep on calling itself, with different parameters, until a terminating condition is met. It’s like writing a loop. You can also write a loop to do something over and over again, until some exiting condition is met.

The Recursive Function Let’s first pretend that we have a function that can calculate the factorial of numbers, called fact_nm1, and we just need to implement the recursive step. So our job is easy: subtract n by 1, call fact_nm1, then multiply n by $v0 (which should have (n-1)!). fact: addi $a0, $a0, -1 jal fact_nm1 mul $v0, $v0, $a0 jr $ra

The Recursive Function Not going to work because we changed $ra and $a0 with calling the function, but we still need them after the call fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) addi $a0, $a0, -1 jal fact_nm1 lw $ra, 4($sp) lw $a0, 0($sp) addi $sp, $sp, 8 mul $v0, $v0, $a0 jr $ra

The Recursive Function Hold on, we also can solve the problem in a special case, when n<2 fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 2 beq $t0, $0, fact_L1 ori $v0, $0, 1 lw $ra, 4($sp) lw $a0, 0($sp) addi $sp, $sp, 8 jr $ra fact_L1:addi $a0, $a0, -1 jal fact_nm1 mul $v0, $v0, $a0

The Recursive Function Changing fact_nm1 to fact will make it work! fact: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) slti $t0, $a0, 2 beq $t0, $0, fact_L1 ori $v0, $0, 1 lw $ra, 4($sp) lw $a0, 0($sp) addi $sp, $sp, 8 jr $ra fact_L1:addi $a0, $a0, -1 jal fact mul $v0, $v0, $a0

What happens if we call fact(3)? First call, compare 3 with 1, call fact again – fact(2). Second call, compare 2 with 1, call fact again – fact(1). Third call, compare 1 with 1, return 1. Return to the state when fact(2) was called. Multiply 2 with 1, return 2. Return to the state when fact(3) was called. Multiply 3 with 3, return 6.

The Stack During Recursion 4/15/2019 The Stack During Recursion 4/15/2019 week04-3.ppt CDA3100 week04-3.ppt

Two other MIPS pointers $fp: When you call a C function, the function may declare an array of size 100 like int A[100]. It is on the stack. You would want to access it, but the stack pointer may keep changing, so you need a fixed reference. $fp is the “frame pointer,” which should always point to the first word that is used by this function. $gp: the “global pointer.” A reference to access the static data.