If you can get away with integer division (i.e. 1/2 = 0
) consider the following pseudo code:
function multiply(a, b):
sum := 0
for i = 0 to b:
sum := sum + a
return sum
function divide(a,b):
count := 0
while a ≥ b:
a := a - b
count := count + 1
return count
There are a few errors with what you've written:
Firstly the line
slt $t3, $t2, $t1 #checks if $t1 > $t2
is incorrect. The correct way to negate > is with ≤. This means the line should be changed to:
sle $t3, $t2, $t1 #checks if $t1 > $t2
Next you have the condition of your loop at the top but the actual branch that uses it at the bottom. You should be aware that a while loop always checks the condition at the top.
Consider:
div_loop:
sle $t3, $t2, $t1 #checks if $t1 > $t2
beq $t3, $zero, end_divis #if first number < second number, break
sub $t1, $t1, $t2 #subtract $t2 from $t1
addi $t0, 1 #add one to counter
j div_loop #loop