0

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?

Ntavass
  • 33
  • 1
  • 6
  • 1
    Duplicate of [How to do less than or equal in Assembly Language(MIPS)?](https://stackoverflow.com/q/22736836) - my answer there even mentions that RISC-V does it the same way as MIPS, and shows compiler output. – Peter Cordes Mar 22 '21 at 15:16

2 Answers2

1

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.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
0

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
old_timer
  • 69,149
  • 8
  • 89
  • 168