ECE 374: Computer Organization
Set Less Than
The instruction slt
will set $R_d$ to 1 if true, else it will set it to 0.
To do a greater than, instead of a separate instruction, you can just swap the operators and use slt
.
To then put this into practice, you can use this to branch like with if statements. To do so, you
first compute the slt
, then branch if it is either equal or not equal to 1.
Example
C Code:
if (x < y) {
// Block A
} else {
// Block B
}
MIPS ASM:
slt $t0, $s1, $s2 # Compare x and y
bne $t0, $0, ELSE # Go to Block A
# BlockB Instructions
j END
ELSE:
# BlockA Instructions
END:
# ...
Set Less Than Immedate Unsigned
sltiu rd, rs, imm
This executes as if (rs < imm) then rd = 1 else rd = 0
All Comparison Instructions
slt
- Set Less Thanslti
- Set Less Than Immediatesltu
- Set Less Than Unsignedsltiu
- Set Less Than Immedate Unsigned