0
votes

I am trying to get MIPS to generate 32bit random integers. I'm trying to use the following code but it gives an error when I run the program.

addi $v0, $zero, 42
li $a1, 4294967295 #32 bit number
syscall

I'm using MARS simulator. The program assembles without any errors. Problem occurs when the syscall above is executed. I'd be really grateful for any help.

1
Since it uses Java's random number support, it probably only supports signed numbers. I would recommend trying a number less than 2^31.Gabe
@MarcB it says that the upper bound of range can not be negative.harbinger
@Gabe yes, the program accepts 2^31, but I want the program to give me a random value between 0 and 2^32. So i somehow need to tell the program that the range I'm providing is unsigned. How can I do that?harbinger

1 Answers

2
votes

I think the problem lies in the maximum number you provided (0xffffffff). Try using the (signed) maximum positive value (0x7fffffff)

addi $v0, $zero, 42
li $a1, 0x7fffffff  #32 bit number (maximum 32bit signed positive number)
syscall

As per OPs comment, to get a 32 bit number, you might do it in two steps: first "toss a coin" to get the 32th bit, and the randomly get the other 31 bits:

    addi $v0, $zero, 42
    move $a2, $zero
    li $a1, 2
    syscall
    bnez $a0, skip
    li $a2, 0x80000000
skip:
    li $a1, 0x7fffffff #31 bit number (maximum 32bit signed positive number)
    syscall
    or $a0, $a0, $a2   # Here $a0 has a 32 bit number