【问题标题】:MIPS Mult two numbers and result it negative numberMIPS 将两个数字相乘并得到负数
【发布时间】:2014-07-07 03:43:27
【问题描述】:

我是 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        # $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 谁知道怎么修它 ?? 谢谢

【问题讨论】:

  • 普通整数溢出?

标签: mips


【解决方案1】:

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

基本上,最高位的值被定义为负数。假设您有 32 位整数。那么0x800000 将具有-2**32 的值,而其他位具有它们的正常值,因此您最终会得到一个看起来像-2**32 + [the sum of the other bits] 的表达式,特别是0xFFFFFFFF 的值是-1,@ 987654327@ 是-2,以此类推。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2010-12-24
    相关资源
    最近更新 更多