How can I get my MIPS code to output a specific case value based on a randomized number?

33 Views Asked by At

I am writing a game in MIPS code using MARS for one of my classes, and I am trying to use a random number generator that, depending on what random number it outputs, a specific string is printed. My problem is that while the random number is chosen, regardless of the value the same case is printed. This is my current code:

.data
arrayAddress: .word 268501028
array1: .asciiz "first case"
array2: .asciiz "second case"
array3: .asciiz "third case"
array4: .asciiz "fourth case"
array5: .asciiz "fifth case"
ln: .asciiz "\n"

.text
randNum:
    li $a1, 5  # max bound
    li $v0, 42  # rand num based on bounds
    syscall
    add $a0, $a0, 1  # low bound
    li $v0, 1  # print rand val based on bounds
    syscall
    li $v0, 4  # print new line
    la $a0, ln
    syscall
    j caseVal

caseVal:
    beq $v0, 1, caseVal1
    beq $v0, 2, caseVal2
    beq $v0, 3, caseVal3
    beq $v0, 4, caseVal4
    beq $v0, 5, caseVal5
    j main

caseVal1:
    la $t0, array1
       li $v0, 4
    la $a0, array1
    syscall
    j main

caseVal2:
    la $t0, array2
        li $v0, 4
    la $a0, array2
    syscall
    j main

caseVal3:
    la $t0, array3
        li $v0, 4
    la $a0, array3
    syscall
    j main

caseVal4:
    la $t0, array4
        li $v0, 4
    la $a0, array4
    syscall
    j main

caseVal5:
    la $t0, array5
        li $v0, 4
    la $a0, array5
    syscall
    j main

main:
    li $v0, 4
    la $a0, ln
    syscall
    #j randNum
    
    la $t0, ($a0)
    li $v0, 4
    syscall

An example is, if I get the value 2, it should also print out "second case" but it prints out "fourth case" which is also the same with every sinle case, it always prints "fourth case." Does anyone understand what the issue is here or point me to a resource that explains the problem? This code is a tester I made, in my actual game the output will be an array that is chosen to be printed. If I can get any help on this that would be fantastic!

I expect the output to print out the random value generated and the respective case string (Which will later be an array). I have tried to change the structure of the case calls just to see what would happen, I tried to load the address of the respective case string to a variable and see if I can use that value in my main function.

1

There are 1 best solutions below

1
On

Here is the solution to my own question incase anyone ends up needing help with a similar issue:

.data
arrayAddress: .word 268501028
array1: .asciiz " first case"
array2: .asciiz " second case"
array3: .asciiz " third case"
array4: .asciiz " fourth case"
array5: .asciiz " fifth case"
ln: .asciiz "\n"

.text
randNum:
    li $a1, 5  # max bound
    li $v0, 42  # rand num based on bounds
    syscall
    add $a0, $a0, 1  # low bound
    li $v0, 1  # print rand val based on bounds
    syscall
    j caseVal

caseVal:
    beq $a0, 1, caseVal1
    beq $a0, 2, caseVal2
    beq $a0, 3, caseVal3
    beq $a0, 4, caseVal4
    beq $a0, 5, caseVal5
    j main

caseVal1:
    la $t0, array1
    li $v0, 4
    la $a0, array1
    syscall
    j main

caseVal2:
    la $t0, array2
        li $v0, 4
    la $a0, array2
    syscall
    j main

caseVal3:
    la $t0, array3
        li $v0, 4
    la $a0, array3
    syscall
    j main

caseVal4:
    la $t0, array4
        li $v0, 4
    la $a0, array4
    syscall
    j main

caseVal5:
    la $t0, array5
        li $v0, 4
    la $a0, array5
    syscall
    j main

main:
    li $v0, 4
    la $a0, ln
    syscall
    #j randNum
    
    la $t0, ($a0)
    li $v0, 4
    syscall