0
votes

I am new for MIPS, there is my code:

.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

There is a problem, when the code goes to this "multu $t0,$s0" commend and result will be wrong. 1103515245 * 2 returned a negative number -2087936806 anyone know how to fix it ?? thanks

1
Run-of-the-mill integer overflow? - chrylis -cautiouslyoptimistic-

1 Answers

1
votes

As chrylis said, it looks like integer overflow. If that's unclear to you, you should read up on two's complement integer representations.

Basically, the value of the highest-order bit is defined to be negative. Suppose you have 32-bit integers. Then 0x800000 will have the value of -2**32, and the other bits have their normal value, so you end up with an expression that looks like -2**32 + [the sum of the other bits], in particular 0xFFFFFFFF has the value of -1, 0xFFFFFE is -2, and so on.