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: 3764484 • 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

fib(0) = 0

fib(1) = 1

fib(n) = fib(n-1) + fib(n-2)

0, 1, 1, 2, 3, 5, 8, 13, 21,…

li $a0, 10           # call fib(10)

jal fib                #

move $s0, $v0 # $s0 = fib(10)

fib is a recursive procedure with one argument $a0

need to store argument $a0, temporary register $s0 for intermediate results, and return address $ra

fib:    subi $sp,$sp,12         # save registers on stack

        sw $a0, 0($sp)          # save $a0 = n

        sw $s0, 4($sp)          # save $s0

        sw $ra, 8($sp)          # save return address $ra

        bgt $a0,1, gen          # if n>1 then goto generic case

        move $v0,$a0            # output = input if n=0 or n=1

        j rreg                  # goto restore registers

gen:    subi $a0,$a0,1           # param = n-1

        jal fib                 # compute fib(n-1)

        move $s0,$v0            # save fib(n-1)

        sub $a0,$a0,1           # set param to n-2

        jal fib                 # and make recursive call

        add $v0, $v0, $s0       # $v0 = fib(n-2)+fib(n-1)

rreg:   lw $a0, 0($sp)         # restore registers from stack

        lw $s0, 4($sp)         #

        lw $ra, 8($sp)         #

        addi $sp, $sp, 12       # decrease the stack size

        jr $ra

this also works

MIPS code:

    .text

main:

    addi $a0, $zero, 5        # suppose we want to find F(5)

    jr   $ra

fib:                          # start of the Fibonacci function

    addi $sp, $sp, -12        # save registers

    sw   $ra, 0($sp)

    sw   $a0, 4($sp)

    sw   $s0, 8($sp)

    bgt $a0, $zero, testmore

    add $v0, $zero, $zero    # fib(0)=0

    j    rtn

testmore:

    addi $t0, $zero, 1

    bne $a0, $t0, recur

    add $v0, $t0m $zero      # fib(1)=1

    j    rtn

recur:

    addi $a0, $a0, -1         # find fib(n-1)

    jal fib

    addi $s0, $v0, $zero      # result of fib(n-1).

                              # Why not put it in $v0?

                              # And why $s0 instead of $t0?

    lw   $a0, 4($sp)          # restore $a0.

                              # Why? Don't we already know it is n-1?

    addi $a0, $a0, -2         # find fib(n-2)

    jal fib

    add $v0, $v0, $s0        # fib(n)=fib(n-1)+fib(n-2)

rtn:

    lw   $s0, 8($sp)          # restore (almost) all registers

    lw   $ra, 0($sp)

    add $sp, $sp, 12

    jr   $ra

Note the use of the rtn block. It is good to have such a block at the end of your function to perform common bookkeeping tasks before returning from the function. This also ensures one entry/exit point in your code, avoiding certain bugs.

We use $s0 and $a0

. The function that is to be called makes no guarantees that $v0, $a0, $t0, etc will be the same after the function call. $s0 is used because the callee promises not to touch the $s0-$s7 registers in accordance with the register conventions.

But because here the caller is the same as the callee, the $s0 register must be saved in accordance with callee rules, and restores it before it returns.

It is a good exercise to see what happens to the stack as the functions get called.

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