0
votes

I need to know how can I test the equality of two registers in MIPS without using branches? I need to make a new instruction that does the same as the beq without using branches... The Labeling part can be done easily by using the Jump instruction, but how can we check for equality ? Thanks in advance

3

3 Answers

4
votes

If you've got two numbers X and Y, and X - Y equals 0, then X and Y are equal.

So, for example, if the two values you want to compare are in $t0 and $t1:

# $t2 will be 0 if $t0 and $t1 are equal, and non-zero otherwise
subu $t2, $t0, $t1

If you want to normalize the result to either 0 (non-equal) or 1 (equal), you could expand the above with:

# Set $t2 to 1 if it's non-zero
sltu $t2, $zero, $t2
# Flip the lsb so that 0 becomes 1, and 1 becomes 0
xori $t2, $t2, 1
0
votes

If you also want to avoid using slt/slti/sltu/sltui you will have to resort to bit-by-bit manipulation of instructions, using J-format most probably.

Otherwise computing the difference then doing sltu $(result), $0, $(difference) is the best alternative.

0
votes

You can check that the first register isn't smaller than the second, and the second isn't smaller than the first, so it means they are equals...

  slt $t0 $t1 $t2    #t0 is 0 if t1==t2 or t1>t2
  slt $t0 $t2 $t1    #t0 is 0 if t1==t2 or t1<t2

so if $t0 has value 0, then they are equal else they are unequal