Fibonacci with Loop int fib1(int i) { int f1 = 1; int f2 = 1; int f3;

Slides:



Advertisements
Similar presentations
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Advertisements

The University of Adelaide, School of Computer Science
CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,
Lecture 9: MIPS Instruction Set
CDA 3100 Recitation Week 15. What does the function f1 do:.data A:.word 10,21,45,8,100,15,29,12,3,19 B:.word 2,5,33,5,20,1,53,52,5,5 C:.word 6,8,5,4,5,22,53,12,33,89.text.globl.
MIPS Assembly Language CPSC 321 Computer Architecture Andreas Klappenecker.
5Z032 Processor Design SPIM, a MIPS simulator Henk Corporaal
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.
Assembly Code Example Selection Sort.
SPRING 2015 QtSpim Demo & Tutorial. 2 By DGP Outline How to write your own MIPS assembly language program How to use QtSpim simulator.
The University of Adelaide, School of Computer Science
SPIM and MIPS programming
Some Samples of MIPS Assembly Language Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University.
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
MIPS Assembly Language Programming
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Recitation Material of Engineering an Assembler June 11, 2013.
1 Computer Architecture MIPS Simulator and Assembly language.
Assembly Language Working with the CPU.
.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,
ECE 0142 Recitation #5.
Solution to Problem Recitation #1 (Week 2) ECEN350 Prof. Choi.
MIPS Assembly Language
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
Lab #1 PCSpim SPIM is a software simulator that loads and executes assembly language programs for the MIPS RISC computers. Download and install PCSpim.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
Spim part II Chun-Cheng Lin ( 林春成 ) & Jiunn-Jye Lee ( 李俊頡 ) CS, EE, NTU.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
In Class Execise. .data A:.word 0,1,2,3,4,5,6,7,8,9.text.globl main main: la $a0,A li $a1,6 li $a2,5 jal onUpStream done: li $v0, 10# exit syscall onUpStream:
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Subroutines, parameters and the stack Bryan Duggan.
Lecture 10: MIPS Simulator Today’s topic –SPIM Simulator Readings –Appendix B 1.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
MIPS Coding. Exercise – the bubble sort 7/9/2016week04-3.ppt2.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
MIPS Assembly Language Programming
Lecture 7: MARS, Computer Arithmetic
MIPS Procedures.
MIPS instructions.
MIPS coding.
CSCI206 - Computer Organization & Programming
SPIM Syscalls.
MIPS Procedures.
MIPS Functions.
MIPS coding.
CSCI206 - Computer Organization & Programming
MIPS Functions.
MIPS Functions.
Компьютерийн зохион байгуулалт, ассемблер CS201
MIPS Procedures.
Review.
MIPS function continued
Компьютерийн зохион байгуулалт, ассемблер CS201
MIPS Functions.
Review.
MIPS coding.
MIPS Functions.
MIPS function continued
MIPS Functions.
MIPS R3000 Subroutine Calls and Stack
Presentation transcript:

Fibonacci with Loop int fib1(int i) { int f1 = 1; int f2 = 1; int f3; while (i >= 3) { f3 = f1+f2; f1 = f2; f2 = f3; i--; } return f2;

Fibonacci with loop fib: li $t0, 1 li $v0, 1 li $t1, 3 L1: blt $a0, $t1, fibExit add $t2, $t0, $v0 move $t0, $v0 move $v0, $t2 addi $a0, $a0, -1 j L1 fibExit: jr $ra

Fibonacci with Recursion int fib(int i) { if (i < 3) return 1; int j = fib(i-1); int k = fib(i-2); return j+k; }

Fibonacci with recursion addi $sp, $sp, -16 sw $s0, 12($sp) sw $s1, 8($sp) sw $a0, 4($sp) sw $ra, 0($sp) li $s0, 0 li $s1, 1 li $t1, 3 L1: blt $a0, $t1, fibExit addi $a0, $a0, -1 jal fib first recursive call move $s0, $v0 jal fib second recursive call move $s1, $v0

Fibonacci with recursion fibExit: add $v0, $s0,$s1 lw $s0, 12($sp) lw $s1, 8($sp) lw $a0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 16 jr $ra

Main program .text .align 2 .globl main main: li $v0, 4 la $a0, PR syscall li $v0, 5 move $a0, $v0 jal fib li $v0, 1 la $a0, LF li $v0,10

Main program .data .align 4 PR: .asciiz "Enter a positive integer " LF: .asciiz "\n"

Execute spim gendreaus-computer:fall10 tom$ spim SPIM Version 6.3a of January 14, 2001 Copyright 1990-2000 by James R. Larus (larus@cs.wisc.edu). All Rights Reserved. See the file README for a full copyright notice. Loaded: ./trap.handler (spim) read "Fib1.s" (spim) run Enter a positive integer 8 21 (spim) read "Fib2.s" (spim) exit