I am confused about how this code multiplies n for each n-1 given the order of the sequences. Let me explain how I see what is happening and you can point out where I'm wrong.
If n is less than 0, the comparison is false and the program simply returns 1(if this is the first time it has been called). If not, L1 is called and it gets decremented. Here is where I get confused. It looks like fact is being called again immediately after the decrement and the multiplication function never takes place. In this case, if n = 5, then it would simply decrement until n = 0 and finally multiply 1 x 5 at the very end.
My best guess is that I'm missing something in terms of it pushing/popping the values on and off the stack, but I haven't a clue.
fact: addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save the return address
sw $a0, 0($sp) # save the argument n
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1 # if n >= 1, go to L1
addi $v0, $zero, 1 # return 1
addi $sp, $sp, 8 # pop 2 items off stack
jr $ra # return to instruction after jal
L1: addi $a0, $a0, -1 # n >= 1; argument gets (n – 1)
jal fact # call fact with (n – 1)
lw $a0, 0($sp) # return from jal: restore argument n
lw $ra, 4($sp) # restore the return address
addi $sp, $sp, 8 # adjust stack pointer to pop 2 items
mul $v0, $a0, $v0 # return n * fact (n – 1)
jr $ra # return to the caller
Edit:
Here is the function in C that is to be taking place here:
int fact (int n)
{
if (n < 1) return (1);
else return (n * fact(n – 1));
}
if (n <= 1)to avoid one (useless) recursion.