MIPS 溢出异常
MIPS overflow exception
我已经阅读了几篇关于 MIPS 有符号和无符号加法溢出异常的文章,但我仍然不清楚。
1) 溢出异常是什么意思,什么时候发生?
2) 无符号加法什么时候忽略溢出?
假设我有两个数字,让我们从 4 位有符号开始,然后转到 4 位无符号
Signed negative addition
1010 -6
+ 1101 -3
------
1 0111 +7 with a carry(overflow) of 1, but result should have been -9.
Signed positive addition
0110 +6
+ 0011 +3
--------
1001 -7 with no carry(probably an overflow of 1), but actual result should have been +9.
Next is an unsigned addition
1111 +15
+ 1010 +10
---------
1 1001 +9 with an overflow/carry of 1, but actual result is +25.
请告诉我在哪种情况下我们会忽略溢出异常,在哪种情况下我们会引发溢出异常以及为什么。我读到对于无符号,我们总是忽略溢出,但是如果结果不正确(9 而不是 25),为什么我们忽略溢出?
MIPS 有符号和无符号加法指令的区别在于,在 2 的补码溢出的情况下,有符号加法会产生整数溢出异常,而无符号异常不会产生异常。
此处说明说明:MIPS - Integer - Arithmetic。
所以在你的例子中,两个"signed additions"(例如ADD指令)会产生异常,因为和2的补码溢出发生,而"unsigned addition"(例如ADDU指令)不会,即使通过结果这里也是错误的。程序员因此可以控制何时应生成 2 的补码溢出和异常。但是,然后指令集不允许 32 位无符号加法从第 32 位溢出,所以如果你真的需要最后一位,请进行 64 位算术运算。
另请参阅这些帖子:
- Why do MIPS operations on unsigned numbers give signed results?
- In MIPS, what's the difference between signed addition, unsigned addition, signed subtraction and unsigned subtraction?.
我已经阅读了几篇关于 MIPS 有符号和无符号加法溢出异常的文章,但我仍然不清楚。
1) 溢出异常是什么意思,什么时候发生? 2) 无符号加法什么时候忽略溢出?
假设我有两个数字,让我们从 4 位有符号开始,然后转到 4 位无符号
Signed negative addition
1010 -6
+ 1101 -3
------
1 0111 +7 with a carry(overflow) of 1, but result should have been -9.
Signed positive addition
0110 +6
+ 0011 +3
--------
1001 -7 with no carry(probably an overflow of 1), but actual result should have been +9.
Next is an unsigned addition
1111 +15
+ 1010 +10
---------
1 1001 +9 with an overflow/carry of 1, but actual result is +25.
请告诉我在哪种情况下我们会忽略溢出异常,在哪种情况下我们会引发溢出异常以及为什么。我读到对于无符号,我们总是忽略溢出,但是如果结果不正确(9 而不是 25),为什么我们忽略溢出?
MIPS 有符号和无符号加法指令的区别在于,在 2 的补码溢出的情况下,有符号加法会产生整数溢出异常,而无符号异常不会产生异常。
此处说明说明:MIPS - Integer - Arithmetic。
所以在你的例子中,两个"signed additions"(例如ADD指令)会产生异常,因为和2的补码溢出发生,而"unsigned addition"(例如ADDU指令)不会,即使通过结果这里也是错误的。程序员因此可以控制何时应生成 2 的补码溢出和异常。但是,然后指令集不允许 32 位无符号加法从第 32 位溢出,所以如果你真的需要最后一位,请进行 64 位算术运算。
另请参阅这些帖子:
- Why do MIPS operations on unsigned numbers give signed results?
- In MIPS, what's the difference between signed addition, unsigned addition, signed subtraction and unsigned subtraction?.