MIPS将两个数相乘,结果为负数
我是MIPS新手,这里有我的代码:MIPS将两个数相乘,结果为负数,mips,Mips,我是MIPS新手,这里有我的代码: .text main: add $s0, $zero, 1 # set $s0 to 0 for the initial value for the following loop Loop: jal srand # Call function srand addi $s0, $s0, 1 # $s0 = i++ slti $s1, $s0, 6
.text
main:
add $s0, $zero, 1 # set $s0 to 0 for the initial value for the following loop
Loop: jal srand # Call function srand
addi $s0, $s0, 1 # $s0 = i++
slti $s1, $s0, 6 # $s1 < 6
bne $s1, $zero, Loop # go to loop in i < 6
beq $s1, 6, end_loop # go out the loop when i == 6
end_loop:
li $v0, 10
syscall
srand: # This function will set the numbers for future calculation
lw $t0, a # Load a value to $t0 1103515245
lw $t1, c # Load c value to $t1 12345
lw $t2, m # Load m value to $t2 2147483648
multu $t0,$s0 # result for multiplication (Xn*a) and store the result to $t3
add $t4, $t3, $t1 # result for add multiplication (Xn*a+c) and store the result to $t4
move $a0, $t4 # Debug function
li $v0, 1 # Debug function
syscall # Debug function
la $a0, msg
li $v0, 4
syscall
jr $ra
.text
主要内容:
添加$s0、$0、1#将以下循环的初始值设置为$s0到0
循环:jal srand#调用函数srand
addi$s0,$s0,1#$s0=i++
slti$s1、$s0、6#$s1<6
bne$s1,$0,循环#进入i<6的循环
beq$s1,6,结束循环#当i==6时退出循环
结束循环:
李$v0,10
系统调用
srand:#此函数将为将来的计算设置数字
lw$t0,a#将值加载到$t0 1103515245
lw$t1,c#将c值加载到$t1 12345
lw$t2,m#将m值加载到$t2 2147483648
multu$t0,$s0#乘法结果(Xn*a)并将结果存储到$t3
为加法乘法(Xn*a+c)添加$t4、$t3、$t1#结果,并将结果存储到$t4
移动$a0、$t4#调试功能
li$v0,1#调试功能
系统调用#调试函数
洛杉矶$a0,味精
李$v0,4
系统调用
jr$ra
有一个问题,当代码转到这个“multu$t0,$s0”时,结果将是错误的。
1103515245*2返回了一个负数-2087936806
有人知道怎么修吗??
谢谢正如chrylis所说,它看起来像整数溢出。如果您不清楚,您应该仔细阅读整数表示法
基本上,最高阶位的值定义为负值。假设您有32位整数。然后,
0x800000
的值为-2**32
,其他位的值为正常值,因此最终得到的表达式类似于-2**32+[其他位的总和]
,尤其是0xFFFFFFFF
的值为-1
,0xFFFFFE
是-2
,等等。普通整数溢出?