2

I'm new to Assembly and I know this is a fair easy question. I supposed to do unsigned integer addition for $a0 and $a2 and store the result in $v0 by checking the carry flag.

The assignment says:

Use only addu, not add, for adding and use slt for determining if the addition of two unsigned integers produced a carry, without using conditional branch instructions.

Here is my code:

addu $v0, $a0, $a2
sltu $t0, $v0, $a0

For this code, if a carry is produced, $t0 equals 1. But the question said use only slt to check the carry flag and no conditional branch instruction can be used. I'm a little bit confused about how this is gonna to work. Any help would be appreciated.

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
Serena Qi
  • 121
  • 1
  • 3
  • 11
  • It's unclear what you want to do. What should happen if you got carry? – Jester Nov 03 '15 at 16:06
  • @Jester: the carry should be stored in `$t0`, I take it. – Michael Nov 03 '15 at 16:07
  • Yep, just store the carry in _$t0_ – Serena Qi Nov 03 '15 at 16:10
  • Didn't they actually mean "slt, any of its variants" and consider "sltu" to be a variant? Because testing unsigned wrapping with an actual `slt` makes little sense (it can be done, but in a pointlessly verbose way) – harold Nov 03 '15 at 16:10
  • Well, carry is already in $t0, and no branching has been used. So if `sltu` is allowed, I don't see what the question is? – Jester Nov 03 '15 at 16:17
  • The question says that **Use only addu, not add, for adding and use slt for determining if the addition of two unsigned integers produced a carry, without using conditional branch instructions** – Serena Qi Nov 03 '15 at 16:18
  • Is there anything else you may use? Such as `and`/`or`? – Jester Nov 03 '15 at 16:45
  • If you must use `slt`, you can do it with four `addu`'s and the `slt` (would destroy one of the operands, which can be recovered with a fifth `addu`) – harold Nov 03 '15 at 16:46
  • I think I can use and or xor. – Serena Qi Nov 03 '15 at 17:27
  • MIPS does not have a carry flag. Your code is almost correct, but you should be using `slt` instead of `sltu`, since you want to do a signed comparison rather than an unsigned comparison. – markgz Nov 03 '15 at 18:00
  • @markgz if I use _slt_, the value stored in $t0 is 0? – Serena Qi Nov 03 '15 at 18:03
  • 3
    Consider that `a – harold Nov 03 '15 at 20:32
  • See also [get unsigned long long addition carry](https://stackoverflow.com/q/56027411) for the `sum = a+b;` / `carry = sum – Peter Cordes Jun 29 '21 at 02:16

1 Answers1

0

OK, the professor said he got the question wrong, using addu and sltu are fine.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Serena Qi
  • 121
  • 1
  • 3
  • 11