Use the following template Submission Instruction: You must run the assembly pro
ID: 3720446 • Letter: U
Question
Use the following template
Submission Instruction: You must run the assembly program using the SPIM simulator. Please zip the following files: The assembly program(s) (using file extension asm) and log files showing the state of SPIM at program completion and the console. Readme.txt (you should explain how to run your program.) Also, you need to provide the output of the console window and the saved log after the program finishes executing). Project Specifications: 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. Execute the program for n 10 and n -20. Save a complete log file for each execution.Explanation / Answer
The coding is done based on your requirement and it is given below clearly:
code:
# The corresponding C code might look like this:
#
# int fib(int N) {
# if (N == 0) return 0;
# if (N == 1) return 1;
# return fib(N-1) + fib(N-2);
# }
# input: N in $s0
# output: fib(N) in $s0
.text
.globl __start
__start:
or $a0, $zero, $s0 # Call fib(N)
jal fib
or $zero, $zero, $zero # nop in delay slot
or $s0, $zero, $v0 # Save the returned value of fib(N)
# Exit
addiu $v0, $zero, 10 # Prepare to exit (system call 10)
syscall # Exit
# Fib takes a single argument N in $a0, and returns fib(N) in $v0
# Uses registers as follows:
# s0 - saved N (preserved across calls)
# s1 - fib(N-1)
# s2 - fib(N-2)
fib:
# Save return address
addi $sp, $sp, -4
sw $ra, 0($sp)
# fib(0) case (return 0)
or $v0, $zero, $zero
beq $a0, $zero, end
or $zero, $zero, $zero # nop in delay slot
# fib(1) case (return 1)
ori $v0, $zero, 1
beq $a0, $v0, end
or $zero, $zero, $zero # nop in delay slot
# Recursion
# Save s0
addi $sp, $sp, -8
sw $s0, 0($sp)
sw $s1, 4($sp)
# Recover N from a0
or $s0, $zero, $a0
# Get fib(N-1)
addi $a0, $s0, -1 # N-1
jal fib
or $zero, $zero, $zero # nop in delay slot
or $s1, $v0, $zero # Save fib(N-1)
# Get fib(N-2)
addi $a0, $s0, -2 # N-2
jal fib
or $zero, $zero, $zero # nop in delay slot
or $s2, $v0, $zero # Save fib(N-2)
# Return fib(N-1) + fib(N-2)
add $v0, $s1, $s2
# Restore s0, s1, s2
lw $s0, 0($sp)
lw $s1, 4($sp)
addi $sp, $sp, 8
end:
lw $ra, 0($sp)
addi $sp, $sp, 4 # Note: Fills load delay
jr $ra
or $zero, $zero, $zero # nop in delay slot
Hope this Helps, if you have any doubs please comment i will get back to you and please thumbs up, thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.