基本上,我通过区分乘法和加法将不等式分为不同的部分,以检查其溢出。我的代码如下所示:
add $s2, $s1, $s1 # $s1 = y , $s2 = 2y
j loop # test whether $s1 & $s0 is overflow or not
mul $s3, $s2, $s2 # (2y)^2
jal loop2
mul $s4, $s3, $s2 # (2y)^3
jal loop2
add $s5, $s0, $s1 # x + y
jal loop # test whether $s5 is overflow or not
mul $s6, $s5, $s5 # (x+y)^2
jal loop2
mul $s7, $s6, $s5 # (x+y)^3
jal loop2
add $s7, $s7, $s4 # (x+y)^3 + (2y)^3
jal loop # test whether $s7 is overflow or not
move $a0, $s7
loop:
slti $t0, $s1, 0 # examine the sign of x
slti $t1, $s2, 0 # examine the sign of y
xor $t0, $t0, $t1 # examine whether signs differ
bne $s2, $zero, no_overflow # $s0 & $s1 signs not equal, so no
overflow
Continue:
slti $t0, $s5, 0
slti $t1, $s7, 0 # signs = sign of sum match
xor $t0, $t1, $t0 # $t0 negative if sum sign different
bne $t0, $zero, overflow # All 3 signs r not same, so goto overflow
loop2:
mult $s0, $s2, $s3 # multiply numbers stored in the .
registers
mfhi $t0 # load upper 32 bits from register
mflo $t1 # load lower 32 bits from register
beq $t0, $0, no_overflow # if $t0 = 0, then load no overflow
beq $t1, $0, no_overflow # if $t1 = 0, then load no overflow
no_overflow:
j Continue # return original address
overflow:
la $a0, error_msg
li $v0, 4
syscall
j exit
如上面的代码所示,我想知道我的逻辑是否有效以及如何改进它。 另外,还有一个问题是我运行代码后,在检查了之前的溢出之后,我不知何故无法继续进行后续的计算,这很可能是由于对Jump&link的不当使用造成的。因此,我也想知道我在哪里犯了自己的错误。