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.
. (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)
( 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...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle....
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle. (NAME = John Doe) 2)Write a Program that intializes X to 10, Y to 20 and Z to -50, adds X and Y and Z and prints the following the value of each variable, for example value of x is 10 as well as the result of the addition.
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT