Search code examples
cif-statementassemblymips

Multiple Conditions for an if-statement in Assembly MIPS


I'm working on translating a C program to assembly MIPS. I have multiple conditions in a single if-statement. It's if (n == 0 || (n > 0 && n % 10 != 0)). When this condition is true, it reassign the variable ans to 0.

This is what I have done so far:

    beqz n label1

What should I do when I have multiple conditions?


Solution

  • An if statement directs execution to one of two locations: The start of the THEN block, or the start of the ELSE block (or immediately after the THEN block if it doesn't have an ELSE block.)

    So we can break down the conditions as follows:

    • n == 0: Go to the THEN block
    • n <= 0: Go to the ELSE block or after IF
    • n % 10 == 0: Go to the ELSE block or after IF
    • Go to the THEN block