Home > Blockchain >  How to write multiple condition if else statement mips
How to write multiple condition if else statement mips

Time:11-19

would i write a logical AND statement the same way i would write a logical OR statement converting C to mips assembly?

else if (i == x && j == y)
printf("%c", 219);

this is what i put

bne $reg1, $t3, draw219 # i==x
bne $reg2, $t4, draw219 # j==y

CodePudding user response:

You accidentally inverted your logic. I'm assuming "draw219" is a piece of code equivalent to your C code printf("%c", 219);. The bne instructions should not branch there, because they only branch if the two registers are not equal. When the two registers are not equal, the overall if condition is false, so you should branch to whatever block of code you want to execute next, but don't go to "draw219".

CodePudding user response:

We would use deMorgan as needed to convert:

Compound condition in structured if-then-else statement:

...
if ( i == x && j == y ) {
    <then-part>
}
else {
    <else-part>
}
...

In if-goto-label form, the condition is negated while also the branching is directed toward the else part, so with both of these changes together, it still runs the same (it is effectively double negation, so same logic):

    ...
    if ( ! (i == x && j == y) ) goto else1Part;
    then1Part:
        <then-part>
        goto endIf1;
    else1Part:
        <else-part>
    endIf1:
        ...

Negation can be distributed over the conjunction by negating the operands of && and changing to ||.

Application of de Morgan to the negated condition:

    if ( ! (i == x) || ! (j == y) ) goto else1Part;

And then optimize the negation of relations:

    if ( i != x || j != y ) goto else1Part;
  • Related