mips - How does this assembly language detect signed overflow in addition? -
here assembly language code test 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 exact meaning of human language?
- overflow occurs when adding 2 numbers of same sign (both positive, or both negative). hence, result of
operand1 xor operand2
should have sign-bit cleared (since0 xor 0
,1 xor 1
both equal 0). that's first part checking. - the second part checking whether sign of result differs sign of operands (only operand1 used here since know after first part both operands have same sign). if sign of result same sign of operands no overflow occurred.
Comments
Post a Comment