Question: Im trying to make a random integer generator from - 3 1 to 3 2 from mips but I encounter an error Runtime exception at
Im trying to make a random integer generator from to from mips but I encounter an error "Runtime exception at x: Upper bound of range cannot be negative syscall Here is my code in MIPS Assembly
data
BingoArray: word
ArraySize: word # Total elements in BingoArray
text
globl main
main:
# Load the array size
lw $a ArraySize
# FisherYates Shuffle Algorithm
addi $a $a # Adjust size for indexing
move $t $a # $t will be used for i in the loop, starting from n
shuffleloop:
bltz $t printrandom # If $t we've shuffled the array; go to print
# Generate random number, j i using syscall
li $v # Syscall code for random int in range
li $a # Lower bound of the range
move $a $t # Upper bound is i
syscall # Perform syscall, result is in $arandom index j
move $a $a # Move random index to $a for SWAP procedure
# Call SWAP with indices i $t and j $a
move $a $t
jal SWAP
# Decrement i for next iteration
addi $t $t
j shuffleloop
SWAP:
# Input: $a i $a j
# Swap BingoArrayi and BingoArrayj
# Calculate addresses
sll $t $a # $t i
sll $t $a # $t j
add $t $t $gp # Address of BingoArrayi
add $t $t $gp # Address of BingoArrayj
# Swap values
lw $t$t # Temporarily store BingoArrayi
lw $t$t # Temporarily store BingoArrayj
sw $t$t # BingoArrayi BingoArrayj
sw $t$t # BingoArrayj BingoArrayi
jr $ra # Return to caller
printrandom:
# Printing the first element of the shuffled array
lw $a BingoArray # Load the first element of the array
li $v # syscall for print integer
syscall
# End of program
li $v # exit syscall
syscall
The Swap procedure work like this
Store any variables you will use on your procedure on the stack
Inputs: i j
Exchange BingoArrayi and BingoArrayj
Restore the used variables from the stack
And the psuedocode for shuffle work like this
To shuffle an array a of n elements indices n:
for i from n down to do
j random integer such that j i
exchange aj and ai
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
