Write a program in assembly language using the MIPS instruction set to calculate
ID: 3721854 • Letter: W
Question
Write a program in assembly language using the MIPS instruction set to calculate the nth Fibonacci number. This must be done in an iterative loop. Your program will read from input the value of n. Be sure to validate user input and report errors when necessary. n must be a natural number that can not be too large that the value of f(n) cannot be expressed with a 32-bit unsigned integer and can be output to the console in SPIM. While iterating through this loop, store the value of f(n) in an array. Ths array should be large enough to contain N values (where N is the largest permissible value of n.) Your program should then output the nth Fibonacci number then output the portion of the sequence stored in the array.
Explanation / Answer
# Fibonacci Series
# numbers are defined as follows: FS(0) = 1, FS(1) = 1,
# and FS(N) = FS(N -1) + FS(N -2), for N >= 2.
.text
.
globl main # global main
main:
li $num1, 4 # Print string
la $add0, msg0 # Address of Title string
syscall # Display the title
#
getN:
li $num1, 4 # Print string
la $add0, msg1
syscall # Print the prompt.
#
li $num1, 5 # Enter a number
syscall # Return the value in $num1
blez $num1, done # End?
li $v1, 1
bgt $num1, $v1, doit # If N > 1?
#
li $num1, 4 # FS(1) will disrupt
la $add0, msg2 # Loop Logic
syscall # Display Answer
j getN
#
doit: li $t1, 1 # Initialization
li $t2, 1 # code for loop.
#
loop: addi $v1, $v1, 1
move $t0, $t1 # New FS(N -2)
move $t1, $t2 # New FS(N -1)
addu $t2, $t0, $t1 # New FS(N), where
# $v1 contains N
blt $v1, $num1, loop # Completed or Not
#
move $a1, $num1 # Keep the value of N
li $num1, 4
la $add0, msg3 # Print subpart of Fibonacci Series
syscall
#
li $num1, 1
move $add0, $a1 # Get the N value back
syscall # Display N
#
li $num1, 4
la $add0, msg4 # Print rest of the values of Series
syscall
#
li $num1, 1 # FS(N) is found in $t2
move $add0, $t2 # Display the result
syscall
#
j getN # Get more value
# Message at Exit
#
done: li $num1, 4
la $add0, finis
syscall
#
li $num1, 10 # End
syscall
#
.data
msg0: .asciiz "Program to find Fibonacci Series"
msg1: .asciiz " Input an integer Value :"
msg2: .asciiz " F(1) is equal to 1."
msg3: .asciiz " The Fibonacci number F("
msg4: .asciiz ") is equal to"
finis: .asciiz" Done. "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.