> How do you write code to do factorials in MIPS?

How do you write code to do factorials in MIPS?

Posted at: 2014-12-18 
You are very close (and I appreciate you posting your attempt at a solve). You just loaded a lot of registers unnecessarily. Here is my solution

#t0 will hold the initial value

#t1 will hold the current product

#In a loop, we multiply $t1 by the current value in $t0

#As long as $t0 is greater than or equal to zero, we multiply again to find the factorial

#The key to a four line solution is using the mips instruction bgez

#which is branch if greater than or equal to zero which only needs one register.

#This is possible because 0 is ALWAYS in the $zero register.

#set our initial factorial to 1

li $t1 1

LOOP: mul $t1 $t1 $t0

addi $t0 $t0 -1

bgez $t0 LOOP

Now I used mul because you had it there, but I am fairly certain that doesn't exist and that you need to use mult and then mflo to access it, but my MIPS is a bit rusty.