Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in assembly language using the MIPS instruction set to calculate

ID: 3720781 • 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

.data
in_string: .asciiz "Input a positive integer: "
out_string: .asciiz "The Fibonacci number is: "


.text
main:
# print out prompt
li $v0, 4 # system call code for printing string
la $a0, in_string
syscall

# read integer into $s0
li $v0, 5 # system call code for read integer
syscall
move $s0, $v0

sw $s0,($sp)
addi $sp,$sp,-4
jal Fib
addi $sp,$sp,4
lw $s1,($sp)

# print out prompt
li $v0, 4 # system call for printing string
la $a0, in_string
syscall

# print out result (stored in $s1)
li $v0, 1 # system call code for printing integer = 1
move $a0, $s1
syscall

# exit program
li $v0, 10 # system call for exit
syscall

# Fibonacci subroutine



Fib:
# procedure prologue:
sw $ra,($sp)
addi $sp,$sp,-4
sw $fp,($sp)
addi $sp,$sp,-4 # and decrement stack pointer
add $fp,$sp,12

lw $t0,($fp)
li $t1, 2
bgt $t0,$t1,do_recurse
li $t0, 1
b epilogue

do_recurse: addi $t0,$t0,-1
sw $t0,($sp)
addi $sp,$sp,-4
jal Fib # call Fibonacci with argument n-1

lw $t0,($fp)
addi $t0,$t0,-2
sw $t0,($sp)
addi $sp,$sp,-4
jal Fib
addi $sp,$sp,4 # increment stack pointer
lw $t0,($sp)
addi $sp,$sp,4
lw $t1,($sp)
add $t0,$t0,$t1

epilogue:
addi $sp,$sp,4
lw $fp,($sp)
addi $sp,$sp,4
lw $ra,($sp)
addi $sp,$sp,4

sw $t0,($sp)
addi $sp,$sp,-4
jr $ra

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote