im having trouble understanding how to do the rs1 >= rs2 and rs1 <= rs2 by using only slt, slti and xori.I know how to do rs1 < rs2 but i cant really find a way to do the <= without using bne, anyone has an idea?
2 Answers
What are you looking for as an answer? Trying to get a boolean value into a register, or to branch to target -- those would have different answers.
Since the limitations include xori
, we'll assume the former. The xori
instruction can be used to negate a boolean value. If you don't understand how that might be done, look up negate aka NOT on MIPS or RISC V, e.g. How do I write NOT Operation for the Risc-V (Assembly Language)?
So, to do rs1 <= rs2
, we would do ! (rs2 < rs1)
which is logically the same as rs2 >= rs1
which is logically the same as rs1 <= rs2
.
The difference is that ! (rs2 < rs1)
is directly implementable given the MIPS instruction set and your limitations.
Were you looking to branch on <=
instead of generating a boolean value, we would compute rs2 < rs1
and then branch on false (using beq .. $0
) instead of branch on true using (bne .. $0
).
All 6 relational operations can be had this way. The tools in your tool kit are: swap operand sides, negate the boolean, and/or branch on false vs. branch on true.

- 23,049
- 2
- 29
- 53
-
Was trying to get a boolean into a register, Thank you, you helped me a lot. – Ntavass Mar 22 '21 at 15:40
ask the compiler.
unsigned int fun ( unsigned int a, unsigned int b )
{
unsigned int ret;
ret=0;
if(a<b) ret=1;
return(ret);
}
00000000 <fun>:
0: 00b53533 sltu x10,x10,x11
4: 8082 c.jr x1

- 69,149
- 8
- 89
- 168
-
-
I was doing other things with the source as well, this one turned out to be the most interesting to me and just left it like that. – old_timer Mar 23 '21 at 11:43