我正在学习计算机算术。我使用的书(帕特森和轩尼诗)列出了以下问题。
编写 mips 代码对 64 位数据进行双精度整数减法。假设第一个操作数在寄存器 $t4(hi) 和 $t5(lo) 中,第二个在 $t6(hi) 和 $t7(lo) 中。
我对答案的解决方案是
sub $t3, $t5, $t7 # Subtract lo parts of operands. t3 = t5 - t7
sltu $t2, $t5, $t7 # If the lo part of the 1st operand is less than the 2nd,
# it means a borrow must be made from the hi part
add $t6, $t6, $t2 # Simulate the borrow of the msb-of-low from lsb-of-high
sub $t2, $t4, $t6 # Subtract the hi's. t2 = t4 - t6
但是作者针对此问题给出的解决方案如下
对于有符号双精度整数,
subu $t3, $t5, $t7
sltu $t2, $t5, $t7
add $t6, $t6, $t2
sub $t2, $t4, $t6
对于无符号双精度整数,
subu $t3, $t5, $t7
sltu $t2, $t5, $t7
addu $t6, $t6, $t2
subu $t2, $t4, $t6
我对sub/add
and操作差异的理解subu/addu
是溢出异常是在sub/add
而不是在subu/addu
. sub/add
和 和subu/addu
减/加操作数的位以及对有符号或无符号操作数的解释对结果没有影响,这与 in和slt
指令sltu
不同。
问题 1
我从作者给出的解决方案中推断出正在处理溢出检测,而我在我的解决方案中没有想到相同的解决方案。我对吗?还有什么我想念的吗?
问题 2假设我的上述推论是正确的,为什么在使用and
减去无符号双精度的情况下,作者提供的解决方案会关闭溢出检测?addu
subu