Question

In: Computer Science

Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method....

Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method. The definition of a Fibonacci number is F(n) = F(n-1) + F(n-2).

The implementation must follow the following guidelines:

  • Prompt the user for a number n
  • Allocate heap memory to hold the exact number of elements in the Fibonacci sequence for n
  • Implement recursive Fibonacci method as a subprogram
  • Print the Fibonacci sequence array

Solutions

Expert Solution

Code:

.text
main:
# Prompt user to input
la $a0,askUser
li $v0,4
syscall

li $v0,5    #Read the number
syscall
move $t7,$v0    # move number to t7

# Call function to get fibonnacci sum
move $a0,$t7
move $v0,$t7
jal fibbonacciSum     #call fibbonacciSum
move $t6,$v0    #store result in t6

la $a0,res   #Print output prompt
li $v0,4
syscall

move $a0,$t7    #Print sum
li $v0,1
syscall

la $a0,res2 #Print output prompt
li $v0,4
syscall

move $a0,$t6    #Print the answer
li $v0,1
syscall

la $a0,endl
li $v0,4
syscall

# Exit
li $v0,10
syscall

fibbonacciSum:

beqz $a0,zero   # return 0 if n=0
beq $a0,1,one   # return 1 if n=1


sub $sp,$sp,4   #storing return address on stack
sw $ra,0($sp)

sub $a0,$a0,1
jal fibbonacciSum     #fibbonacciSum(n-1)
add $a0,$a0,1

lw $ra,0($sp)   #restoring return address from stack
add $sp,$sp,4


sub $sp,$sp,4   #Push return value to stack
sw $v0,0($sp)

sub $sp,$sp,4   #storing return address on stack
sw $ra,0($sp)

sub $a0,$a0,2
jal fibbonacciSum     #fibbonacciSum(n-2)
add $a0,$a0,2

lw $ra,0($sp)   #restoring return address from stack
add $sp,$sp,4

lw $s0,0($sp)   #Pop return value from stack
add $sp,$sp,4

add $v0,$v0,$s0 # fibbonacciSum(n-1)+fibbonacciSum(n-2)
jr $ra

zero:
li $v0,0
jr $ra
one:
li $v0,1
jr $ra

.data
askUser: .asciiz "Enter a number: "
res: .asciiz "Fibonacci Sum for "
res2: .asciiz " is "
endl: .asciiz "\n"


Related Solutions

Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any...
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any sorting algorithm. Print both the sorted array and unsorted array. N should be greater than or equal to 10.
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Create a MIPS assembly program to calculate x^y using a recursive subprogram. You may assume that...
Create a MIPS assembly program to calculate x^y using a recursive subprogram. You may assume that y is not negative.
Write an assembly code in MIPS program that can read 3 numbers from the user and...
Write an assembly code in MIPS program that can read 3 numbers from the user and print the following: a. The summation b. The average c. The minimum d. The maximum e. Print the values between the minimum and maximum f. Write comments to explain each line in your code -look at Fibonacci code- (no comments mean zero for the assignment ) use MARS MIPS .
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers....
Write a program in MIPS Assembly. This program will ask the user to enter 20 numbers. The program will store these numbers in an array in memory (sequential memory locations). It will then print out the list of numbers in three different formats: The numbers will be printed each on a separate line. The numbers will be printed on a single line, with spaces between the numbers. The program will ask the user to enter a number n. The program...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
1. Write MIPS assembly code to sum “n” positive integers. Forexample, n=5, read 5 numbers...
1. Write MIPS assembly code to sum “n” positive integers. For example, n=5, read 5 numbers in memory (“.data” section) and add them together. 2. Write MIPS assembly code to calculate N-factorial. Read n from the memory. (Hint: use “.data” to define the content in a memory location)
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the...
Write a MIPS Assembly program function to calculate the factorial of an input number. Analyze the program for the value 10 and compute the Execution time in a MIPS processor at 2.4GHz. The CPI is 1.
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT