Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would we use addiu instead of addi?

In MIPS assembly, what is the benefit of using addiu over addi? Isn't addiu unsigned (and will ruin our calculations?)

like image 647
qaispak Avatar asked Mar 29 '16 02:03

qaispak


People also ask

What is the difference between Addi and Addiu?

The only difference between addi and addiu is that addiu doesn't check for overflow.

What does addiu do?

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 type of instruction is addiu?

The addiu instruction includes a 16-bit immediate operand. When the ALU executes the instruction, the immediate operand is sign-extended to 32 bits. If two's complement overflow occurs during the addition, it is ignored.

Is Addi signed or unsigned?

The only difference between the signed instructions add, addi and sub, and the unsigned ones addu, addiu, and subu, is that the unsigned ones do not generate overflow exceptions.


Video Answer


1 Answers

and will ruin our calculations

No, MIPS uses two's complement, hence the same instruction for addition/subtraction can be used for both signed and unsigned operations. There's no difference in the result.

That's also true for bitwise instructions, non-widening multiplication and many other operations. See

  • Which arithmetic operations are the same on unsigned and two's complement signed numbers?
  • Difference between signed and unsigned on bitwise operations

The only difference between them is that addi generates a trap when overflow while addiu doesn't. So addi and its overflow family (add, sub...) is often useless. In fact it's so rarely used that addi was removed in MIPSr6 to release valuable opcode space to other instructions

Here the instruction name is extremely misleading, because it's not actually an "unsigned" addition. The immediate is still sign extended instead of zero extended. So addiu $1, $2, 0xFFFF will actually subtract 1 from $2 instead of adding 65535 to it.

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.

Computer Organization and Design: The Hardware/software Interface By David A. Patterson, John L. Hennessy

Read more Difference between add and addu

like image 63
phuclv Avatar answered Sep 24 '22 10:09

phuclv