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

5.9- Assignment The assignment for this lab asks us to compose assembly code tha

ID: 3739056 • Letter: 5

Question

5.9- Assignment The assignment for this lab asks us to compose assembly code that calculates the Fibonacci sequence of length n+1 given n. For example, the Fibonacci sequence of n 3 is: 0 11 2. the Fibonacci sequence ofn 5 is 01123 5, and likewise for n 6 is 0 1 1 23 8. In general, the calculation of this sequence from position 1 to position n + 1 is calculated by n + 1 = n + (n- 1 )-the next digit is the sum of the preceding two digits. This calculation could be performed iteratively, where we keep track of the previous two digits in order to determine the next; however, it can also be implemented recursively, where we use global and local variables to maintain the previous digits and calculate the Fibonacci sequence. It is important that you write a recursive implementation of this assignment. Not only is this how the specification code is written, but creating an iterative solution will not allow you to sufficiently practice with the stack 5 This is the specification of what the assembly functions need to perform. Do not copy or type this code. Use it as a reference when writing the assembly. This is a recursive implementation; do not write an iterative solution. /* begin specification code/ /* Convert the procedure exactly as given using the local variables local_var and temp var on the stack./ void Fib (void) int local var int temp_var; local var = global va r; if(local var.. 0) return ; else if (local var m= 1) return; else f global-var-local-var - 1; Fib ); temp_var - global_var: global_var-local_var - 2: Fib) temp-var-temp-var + global-var; global_var temp_var: returni / end specification code /

Explanation / Answer

I am assuming that you need just the fibonacci part and does not require printing of the values.

/* begin assembly stub */

.globl Fib
.type Fib,@function

Fib:
   /* prolog */
   addi $sp, $sp -12
   sw $ra, 8($sp)
   sw $s0, 4($sp)
   sw $s1, 0($sp)

   /*put code here*/
   move $s0, $a0
   li $v0, 1 # terminal conditon (return value)
   ble $s0, 0x2, return # terminal condition checking
   addi $a0, $s0, -1 # arguements for the recursive call for fib(n-1)
   jal Fib
   move $s1, $v0 # storing fib(n-1) to s1
   addi $a0, $s0, -2 # arguements for the recursive call for fib(n-2)
   jal Fib
   add $v0, $s1, $v0 # add fib(n-1) to it
  
   return:
       /* epilog */
       lw $ra, 8($sp)
       lw $s0, 4($sp)
       lw $s1, 0($sp)
       addi $sp, $sp, 12
       jr $ra

       ret

/* end assembly stub */

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