how can i make this code more efficient (e.g less code inside the loop)?

loop: beq $a0, $a1, exit
         slt $t0, $a0, $a1
         bne $t0, $zero, label
         sub $a0, $a0, $a1
         j loop
label: sub $a1, $a1, $a0
         j loop 
exit: add $v0, $zero, $a0


6 Years
Discussion Span
Last Post by wildgoose

I'm looking at your code, but I don't see your algorithm.

If you're trying to find the Greatest Common Denominator, then I'm not seeing the algorithm. Several methods for a GCD. One is inner, outer loop. Subtract tmp divisor each time, looking for a divisor == 0 vs divisor < remainder result. If not zero, increment the divisor, reset the dividend and start again. Fastest method though would be to start from divisor half the dividend, and decrement the divisor each time.

You aren't using recursion. I'm not aware of any Euclidean GCD algorithm that uses a Nx2 algorithm in its basis.

Can you post your logic in pseudo code to make it clearer!

I've added comments to your code but it isn't helping me understand the algorithm.

What happens if a0 is (0)? I'm assuming a1 is (1) as its being used as a shifter, but shouldn't that be a divisor instead of a multiplier?

;  a0, a1 preset.

; Assuming a1 always == 1, as a down counter?

;	If 1==1  exit

loop: beq $a0, $a1, exit

	slt $t0, $a0, $a1				; t0 = a0 << a1         ??? Why making # bigger to zero instead of smaller?
	bne $t0, $zero, label			; jmp t0==0 label

	sub $a0, $a0, $a1				; a0 -= a1				??? Only subtracting (1)
	j loop

;	Outer Loop

label: sub $a1, $a1, $a0			; a1 -= a0
	j loop
exit: add $v0, $zero, $a0			; return a0

Instead of starting with assembly. Start with pseudo code or a higher programming language to get the logic flow in place

Edited by wildgoose: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.