2014-07-07 40 views
0

我是新的MIPS,有我的代码:MIPS MULT两个数字,并导致它负数

.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 

这里有一个问题,当代码进入这个“multu $ T0,$ S0”表彰结果将是错误的。 1103515245 * 2返回一个负数-2087936806 任何人都知道如何解决它? 谢谢

+3

普通的整数溢出? – chrylis

回答

1

正如chrylis所说,它看起来像整数溢出。如果你不清楚,你应该阅读two's complement整数表示。

基本上,最高位的值被定义为负值。假设你有32位整数。那么0x800000将具有-2**32的值,并且其他位具有它们的正常值,因此您最终得到类似-2**32 + [the sum of the other bits]的表达式,特别是0xFFFFFFFF具有值-10xFFFFFE-2,依此类推。