wow yoo
wow yoo

Reputation: 895

How does this assembly language detect signed overflow in addition?

Here is the assembly language code to test for signed overflow in addition:

addu $t0, $t1, $t2 
xor  $t3, $t1, $t2
slt  $t3, $t3, $zero
bne  $t3, $zero, No_overflow
xor  $t3, $t0, $t1
slt  $t3, $t3, $zero
bne  $t3, $zero, Overflow

what is the exact meaning of human language?

Upvotes: 0

Views: 1016

Answers (1)

Michael
Michael

Reputation: 58427

  • Overflow only occurs when adding two numbers of the same sign (both positive, or both negative). Hence, the result of operand1 XOR operand2 should have the sign-bit cleared (since 0 XOR 0 and 1 XOR 1 both equal 0). That's what the first part is checking.
  • The second part is checking whether the sign of the result differs from the sign of the operands (only operand1 is used here since we know after the first part that both operands have the same sign). If the sign of the result is the same as the sign of the operands then no overflow occurred.

Upvotes: 3

Related Questions