Home > Mobile >  Multiple Conditions for an if-statement in Assembly MIPS
Multiple Conditions for an if-statement in Assembly MIPS

Time:05-03

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?

CodePudding user response:

Using pseudo-code, a solution could look like:

Compare n to zero.
If the comparison result is “equal”, go to TestIsTrue.
If the comparison result is not “greater than”, go to TestIsFalse.
Calculate the remainder of n divided by 10.
Compare the remainder to zero.
If the comparison result is “not equal”, go to TestIsTrue.
Go to TestIsFalse.

TestIsTrue:
Store 0 in ans.
Go to AllDone.

TestIsFalse:
(Put any desired code here.  Can be empty.)

AllDone:

CodePudding user response:

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
  • Related