Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between add and addu

I am confused about the difference between add and addu.

The MIPS instruction reference says:

  • add (with overflow)
  • add unsigned (no overflow)

My understanding is to use add with signed operands and addu with unsigned operands.

But let's consider this example (with only 6bit):

overflow
|
V
1 | 1 1 1  <- carry
  | 1 1 1 1 0 1 +
  | 1 1 1 1 1 0 =
-----------------
  | 1 1 1 0 1 1

And this is my reasoning:

  • if I consider the first and second operand signed numbers (two's complement), then the result is correct (-3 + -2 = -5) and I don't want an overflow exception. So I would use addu to avoid this exception, but, although the result is the same, the name suggests to use addu is for unsigned numbers!
  • if I consider the first and second operand unsigned numbers, then I want an exception to be raised (because 61 + 62 is not equal to 59). So I would use add to raise the exception, and not addu, as the name would suggest to do.

Now my questions are:

  • assuming that operands are signed (negative in the example above) numbers, should I use addu (as my reasoning suggests) or I should use add (as the name suggests)?
  • assuming that operands are unsigned (positive) numbers, should I use add (as my reasoning suggests) or addu (as the name suggests)?
like image 434
collimarco Avatar asked May 19 '13 11:05

collimarco


People also ask

What is the difference between the Add and Addu instruction?

In MIPS, what is the difference between the add and addu instructions? add causes an exception (stops normal execution of the program and goes to the exception handler or aborts) if the result of the addition causes an overflow. addu does not cause an exception if the result of the addition causes an overflow.

What does Addu mean in MIPS?

The addu instruction performs the Binary Addition Algorithm on the contents of two 32-bit registers and places the result in the destination register.

What is Addiu?

Despite its name, add immediate unsigned ( addiu ) is used to add constants to signed integers when we don't care about overflow. MIPS has no subtract immediate instruction, and negative numbers need sign extension, so the MIPS architects decided to sign-extend the immediate field.

What does Subu do in MIPS?

1 Answer. Show activity on this post. Like all machine instructions, subu will give you a binary result -- 32 bits that are stored in the destination register. These bits are just bits, they are neither positive or negative in and of themselves, it depends on how you interpret them.


2 Answers

The instruction names are misleading. Use addu for both signed and unsigned operands, if you do not want a trap on overflow.

Use add if you need a trap on overflow for some reason. Most languages do not want a trap on signed overflow, so add is rarely useful.

like image 163
markgz Avatar answered Sep 23 '22 04:09

markgz


If you are using signed numbers, you should use add if you want a trap to be generated when the result overflows.

If you are using unsigned numbers, you should always use addu and check the overflow of the addition by comparing the result with either numbers (if the result is less than the operands then the addition did overflow).

Here goes a snippet to show how you would check for overflow in unsigned addition:

    li $a1, 0xFFFF0FFF
    li $a2, 0x00010000

    addu $a3, $a1, $a2  # This unsigned addition overflows (set $a3 to $a1+$a2)
    bgt $a1, $a3, overflowed
    bgt $a1, $a2, overflowed
    # If you get here, unsigned addition did not overflow
  # your code goes here...
overflowed:
    # If you get here, unsigned addition overflowed
  # your code goes here...
like image 41
gusbro Avatar answered Sep 21 '22 04:09

gusbro