0

So basically, I'm new to MIPS and I came across this question.

How would you translate "if x>=y" in MIPS and need to assume that x is in $t0 and y is in $t1

Here is what I tried

bge $t0, $t1, label

I'm not sure whether I'm doing it correctly or not.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ben
  • 1
  • 1
  • You're doing it right. Thankfully MIPS is much easier to work with in this regard than most other languages. The only thing you need to watch out for is if your variables are signed or unsigned. For variables that are intended to be always positive, use `bgeu`. With `bge`, any register containing a value greater than `0x7FFFFFFF` will be considered negative. – puppydrum64 Dec 02 '22 at 14:49

1 Answers1

2

If you have this code:

  if x>=y
    // do something
  else
    // do otherthing
  endif

you may write it as:

  bge $t0, $t1, taken  # branch to taken if x>=y
  # do otherthing
  b endif
taken:
  # do something
endif:

or you may rewrite it to keep the ordering of your high-level statement, jumping with the negated condition:

  blt $t0, $t1, else  # branch to else  if x<y
  # do something
  b endif
else:
  # do otherthing
endif:

If you don't have an "else" part then the second approach leads to less code. So

  if x>=y
    // do something
  endif

can be translated to this:

  blt $t0, $t1, endif  # branch to endif  if x<y
  # do something
endif:
gusbro
  • 22,357
  • 35
  • 46