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

Write below code in MPI MASM 8086. not in C language.. Step1: Write an assembly

ID: 3764448 • Letter: W

Question


Write below code in MPI MASM 8086. not in C language..

Step1: Write an assembly procedure (mpi masm 8086) which computes the following:

int myfib(n) /* n is a positive integer */

if (n<=3) return 1;

/* else */

return myfib(n-1)+myfib(n-2)+myfib(n-3);

NOTE THAT THE ABOVE FUNCTION IS RECURSIVE. YOUR PROCEDURE NEEDS TO BE RECURSIVE AND COMPUTE EXACTLY AS DESRIBED IN MYFIB(). NO CREDIT WILL BE GIVEN IF YOUR PROCEDURE IS NOT RECURSIVE. NO CREDIT WILL BE GIVEN IF YOUR PROGRAM IS CROSS-ASSEMBLED FROM ANOTHER LANGUAGE.

YOU DECIDE ON WHICH REGISTERS TO USE (FOR PARAMETER PASSING AND RETURNED VALUES, ETC).

Step 2: After completing Step 1, write an assembly program that computes and displays in a separate line the following:

i myfib(i)

for all integer i, from i=1 to 15.

The initial part of the output will look as the following:

1 1

2 1

3 1

4 3

5 5

6 9

7 17

Explanation / Answer

The Fibonacci sequence has the following recursive definition: let F(n) be the nth
element (where n # 0) in the sequence:
• If n < =3, then F(n) $ 1. (the base case)
• Otherwise, F(n) = F(n 1) + F(n 2)+F(n-3). (the recursive case)

# fib
# Registers used:
# $a0 - initially n.
# $s0 - parameter n.
# $s1 - fib (n - 1).
# $s2 - fib (n - 2).
# $s3 -fib (n-3).
.text
fib:
subu $sp, $sp, 32 # frame size = 32, just because...
sw $ra, 28($sp) # preserve the Return Address.
sw $fp, 24($sp) # preserve the Frame Pointer.
sw $s0, 20($sp) # preserve $s0.
sw $s1, 16($sp) # preserve $s1.
sw $s2, 12($sp) # preserve $s2.
sw $s3, 8($sp) # preserve $s3.
addu $fp, $sp, 32 # move Frame Pointer to base of frame.
move $s0, $a0 # get n from caller.
blt $s0, 3, fib_base_case # if n < 3, then do base case.
sub $a0, $s0, 1 # compute fib (n - 1)
jal fib #
move $s1, $v0 # s1 = fib (n - 1).
sub $a0, $s0, 2 # compute fib (n - 2)
jal fib
move $s2, $v0 # $s2 = fib (n - 2).
sub $a0, $s0, 3 # compute fib (n - 3)
jal fib
move $s3, $v0 # $s3 = fib (n - 3).

add $v0, $s1, $s2,$s3 # $v0 = fib (n - 1) + fib (n - 2)+fib(n-3).
b fib_return
fib_base_case: # in the base case, return 1.
li $v0, 1
fib_return:
lw $ra, 28($sp) # restore the Return Address.
lw $fp, 24($sp) # restore the Frame Pointer.
lw $s0, 20($sp) # restore $s0.
lw $s1, 16($sp) # restore $s1.
lw $s2, 12($sp) # restore $s2.
lw $s3,8($sp) #restore $s3
addu $sp, $sp, 32 # restore the Stack Pointer.
jr $ra # return.

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