Write a program in MIPS assembly language to find nth Fibonacci number. What is
ID: 3625169 • Letter: W
Question
Write a program in MIPS assembly language to find nth Fibonacci number. What is Fibonacci series? It can be defined by the following equations:Fn= Fn-1+ Fn-2 where Fn is the nth Fibonacci number.
F0 = 0
F1 = 1
The sequence thus is 0,1,1,2,3,5,8,13,21,34,55.......
Remember that the indexing starts at 0.
Example:
Which Fibonacci number do you want: 7
Output: Number is 13 and the series is 0, 1, 1, 2, 3, 5, 8, 13
Do you want another Fibonacci number? Enter 1 for YES and 0 for NO.
User should be asked for positive integers.
Explanation / Answer
Dear, .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 Hope this will help you..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.