1

MIPS provides branching instructions like branch on equal, branch on not equal to register,branch on less than or equal to zero, branch on greater than or equal to zero and so on... all the branching instructions use only two operands and one conditions . What happens if we suddenly encounter multiple conditions in if statement.

So the question is how can one write a MIPS code for :

if( (a<b) & ( b>c ) || (c==d)) {

}
else 
{

 }

Please help with this kind of multiple conditions in if statement.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Nilesh Agrawal
  • 3,002
  • 10
  • 26
  • 54
  • Think about how you could restructure the above C code so that there's only one condition term per `if`... – Oliver Charlesworth Mar 13 '13 at 01:11
  • This would cause limitation to design a compiler using MIPS. Right now i am trying to build a compiler for 'if' and 'while' statement. – Nilesh Agrawal Mar 13 '13 at 01:20
  • Once you have restructured the C code, it should hopefully become obvious how the equivalent may be achieved in assembler. – Oliver Charlesworth Mar 13 '13 at 01:21
  • we can restructure only in and conditons – Nilesh Agrawal Mar 13 '13 at 01:37
  • This isn't unique to MIPS or `if` conditions. Read up a bit on expression evaluation in compiler design and it should hopefully get clearer. – Michael Mar 13 '13 at 06:59
  • Related: [How can I implement if(condition1 && condition2) in MIPS?](https://stackoverflow.com/q/55408890) / [Nested For Loop Multiple Conditions Mips](https://stackoverflow.com/q/35629152) / [Double condition in a for loop in MIPS assembly](https://stackoverflow.com/q/13892462) – Peter Cordes Oct 26 '21 at 03:56
  • Also duplicate of [How to write multiple condition if else statement mips](https://stackoverflow.com/q/74496686) which shows the logical transformation into C with multiple single-condition `if` statements. – Peter Cordes Feb 09 '23 at 01:24

2 Answers2

3

You can rewrite:

if( (a<b) && ( b>c ) || (c==d)) {

}

Like this:

bool altb = a < b;
bool bgtc = b > c;
bool ceqd = c == d;
bool and1 = altb && bgtc;
bool condition = and1 || ceqd;
if (condition) {
} else {
}

This is how most compilers will evaluate a complex condition in an if statement. Doing it this way is also much faster than chaining a lot of conditional branches together.

markgz
  • 6,054
  • 1
  • 19
  • 41
  • 1
    Compilers may also actually branch, actually doing short-circuit evaluation like the C semantics specify. Either one is fine if there are no side-effects or possible faults from evaluating the later expressions even if `a – Peter Cordes May 03 '22 at 05:23
2

Assuming that $t0 has a, $t1 has b, $t2 has c and $t3 has d:

     outter_if_lbl: 
     bge $t0,$t1,exit_outter   #if a>=b break
     ble $t1,$t2,exit_outter   #if b=< c break
     bne $t2,$t3,exit_outter   #if c != d break
     #add functionality here   

     exit_outter:
     jr $ra #or whatever does your job

I use pseudo-instructions so if you want you can convert them. Google how. The idea behind that ,is the fact that you have to use the opposite case of the if statement for the code to work similarly (and that is a general C to Mips Conversion rule).