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

in (n<1) return 1; else returne n*fact(n-1) } I have an assembly language versio

ID: 2990268 • Letter: I

Question

in (n<1)

     return 1;

           else returne n*fact(n-1)

}

I have an assembly language version that works for the second part, but I am not sure what is meant by demonstraiting how the stack changes.

fact:

addi $sp, $sp, -8                # adjust stack for 2 items

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

sw $a0, 0($sp)                   # save argument

slti $t0, $a0, 1                     # test for n < 1

beq $t0, $zero, L1

addi $v0, $zero, 1             # if so, result is 1

addi $sp, $sp, 8                 # pop 2 items from stack

jr $ra                                      # and return

L1:          addi $a0, $a0, -1                # else decrement n

jal fact                                   # recursive call

lw $a0, 0($sp)                    # restore original n

lw $ra, 4($sp)                     # and return address

addi $sp, $sp, 8                 # pop 2 items from stack

mul $v0, $a0, $v0              # multiply to get result

jr $ra                                      # and return

Explanation / Answer

This is correct code with stack implementation . In this to store all intermediate values we use $sp which uses stack pointer . The intermediate results are pushed and poped from the stack as required.