Question: This is the code we have to modify, The question is after the code. I need the SOLUTION as a MIPS code # Comments. Anything
This is the code we have to modify, The question is after the code. I need the SOLUTION as a MIPS code
# Comments. Anything after # is comments. # Lab 3 # MARS Example .data # data segment nl: .asciiz " " # ASCII for a new line .align 2 # aligned at word boundary name: .asciiz "CSC34300: Lab 3-1: YOUR NAME (YOUR STUDENT ID) " .align 2 msg1: .asciiz "Fibonacci Series Element " #note the space .align 2 msg2: .asciiz " is " #note the spaces .text # Code segment .globl main # declare main to be global main: la $a0,name # load the address of "name" into $a0 li $v0,4 # system call, type 4, print an string($a0) syscall # call the "OS" li $t2,0 # $t2=0; initial value of F(n-2) li $t1,1 # $t1=1; initial value of F(n-1) li $t3,10 # $t3 is the counter to be decremented li $t4,2 # $t4=2; index, n. loop: add $t0,$t1,$t2 # F(n) = F(n-1) + F(n-2) la $a0,msg1 # $a0 = address of message 1 li $v0,4 # system call, type 4, print an string($a0) syscall move $a0,$t4 # $t4 contains the current value of n li $v0,1 # system call, type 1, print an integer $a0 syscall la $a0,msg2 # $a0=address of "msg2" li $v0,4 # system call, type 4, print an string syscall move $a0,$t0 # $a0= $t0, which is F(n) li $v0,1 # system call, type 1, print an integer $a0 syscall la $a0,nl # $a0= address of "nl" li $v0,4 # system call, type 4, print an string syscall addi $t4,$t4,1 # $t4 = $t4 + 1; increment n move $t2,$t1 # $t2 = $t1; old F(n-2) because old F(n-1) move $t1,$t0 # $t1 = $t0; old F(n-2) because old F(n) addi $t3,$t3,-1 # $t3 = $t3 - 1; decrement the counter bne $0,$t3,loop # continue if $t3 is not 0 Exit: li $v0,10 # System call, type 10, standard exit syscall # ...and call the OS
Quetion:
Fibonacci Number and Recursive Function Objectives You will practice function calls in MIPS assembly code. Your code will repeatedly read an integer n from the console, compute the Fibonacci number F (n) and print F (n) to the console (followed by a new line). If n is too large (i.e. causes overflow) which is detected in the function that computes the Fibonacci number, your code prints The number is too large. The program terminates if n = 0.
Description Use system call 5 to read an integer from the console. The returned value is in $v0. You code will compute Fibonacci number recursively. The function interface looks like int64 Fibonacci2 (int n) Different from previous labs, Fibonacci2 calculates the Fibonacci sequence in a recursive way. The function takes one integer as input and returns F(n) and F(n 1), two 32-bit integer numbers (thus int64). The argument is in $a0. F(n) is placed in $v0, and F(n 1) is in $v1. Since you have both F(n) and F(n 1) with a function call, you can easily compute F(n + 1). The pseudo code looks like // $a0 is n Allocate space on stack and save registers If ($a0 == 0) $v0 = 0 and $v1 = 0 Else if ($a0 == 1) $v0 = 1 and $v1 = 0 Else if ($a0 > 100) $v0 = 0xFFFFFFFF Else Decrement $a0 Call Fibonacci2 to get F(n-1) and F(n-2) If ($v0 is not 0xFFFFFFFF) Compute F(n) If (no overflow) Set correct values in $v0 and $v1 Else $v0 = 0xFFFFFFFF Restore saved registers and stack Return The main function of your code will call Fibonacci2 with one argument. Your code needs to preserve all the registers changed, except for $v0 and $v1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
