1

I'm working on "Craps" (a dice game) for an assignment and i'm puzzled about one thing in my code. I'm trying to figure out why both the win prompt and lose prompt trigger when the beq if statement triggers. In craps if the rolling player rolls a 7 or an 11 they win, and somehow the win condition is also triggering the losing condition prompt found at the bottom of the code.

Thanks for your help

.text
main:

li $v0, 4                               # Load system call code 4 into register $v0; System call code 4 represents print_string.
la $a0, heading                         # Load address of string to print.
syscall                                 # Perform system call.
        
# Prompt for the integer to enter
li $v0, 4
la $a0, prompt
syscall

# Read the integer and save it in $s0
li $v0, 5
syscall
move $s0, $v0

# Output the text
li $v0, 4
la $a0, output
syscall

# Output the value
li $v0, 1
move $a0, $s0
syscall





li $t0, 3 # t0 is a constant 10
li $t5, 0 # t1 is our counter (i)
loop:
beq $t5, $s0, end # if t1 == 10 we are done





##################body of loop
    # Output the text
li $v0, 4
la $a0, rng
syscall


.text
    li $a1, 6  #Here you set $a1 to the max bound.
    li $v0, 42  #generates the random number.
    syscall

    add $a0, $a0, 1  #Here you add the lowest bound
    li $v0, 1   #1 print integer

    syscall
   
    move $t1, $a0
   
       # Output the text
li $v0, 4
la $a0, registerMoved
syscall

# Output the value
li $v0, 1
move $a0, $t1
syscall

    # Output the text
li $v0, 4
la $a0, rng
syscall

#generation of second random number
    li $a1, 6  #Here you set $a1 to the max bound.
    li $v0, 42  #generates the random number.
    syscall

    add $a0, $a0, 1  #Here you add the lowest bound
    move $t2, $a0

    syscall
   
# Output the value
li $v0, 1
move $a0, $t2
syscall
   
       # Output the text
li $v0, 4
la $a0, registerMoved
syscall

# Output the value
li $v0, 1
move $a0, $t2
syscall

add $t3, $t1, $t2

       # Output the text
li $v0, 4
la $a0, combined
syscall

li $v0, 1
move $a0, $t3
syscall

li $t1, 7

 beq $t1, $a0, WIN  # if(input==7 output win)
li $t2, 11






###### end of body of loop



addi $t5, $t5, 1 # add 1 to t1
j loop # jump back to the top
end:


 # Exit the program
li $v0, 10
syscall

.text

WIN: 

   # Output the text
li $v0, 4
la $a0, winString
syscall


END_WIN:

.text
LOSE: 
   # Output the text
li $v0, 4
la $a0, loseString
syscall


END_LOSE:
    
.data
heading: .asciiz "Welcome to the game of craps\n"
registerMoved: .asciiz "\nThe register was copied to another address: "
prompt: .asciiz "Enter a number of rolls: "
die_roll: .asciiz "The dice roll is: "
win: .asciiz "You have won"
loss: .asciiz "You have lost"
output: .asciiz "\n You entered for the number of rolls :"
rng: .asciiz "\nThe number generated was: "
combined: .asciiz "\nThe combined value is: "
winString: .asciiz "The rolling player wins "
loseString: .asciiz "The rolling player loses "```

1 Answers1

1

Labels don't alter execution flow. Inserting a label called END_WIN: doesn't in any way cause the function or block to "end"; it just associates the current address with a symbol called END_WIN that is never used. After the syscall following WIN, execution falls through into LOSE, which I suspect is not what you want.

If you want execution to go somewhere else, you need to explicitly write a jump instruction.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • if the methods are contained in the label, why are both methods being triggered? I thought the : after a string makes it a method that can be called and singularly called as opposed to two methods – nbulgarides Nov 11 '21 at 18:47
  • No, a label is not a method. Assembly is not a high-level language and does not have HLL concepts like "methods". An assembly program is just a sequence of instructions, that get executed in sequence until one of them is a branch instruction. And a label is literally just a label - a human-readable name for a particular location in the code, which you could jump to if you want. – Nate Eldredge Nov 11 '21 at 18:49
  • Ty for your help. How can I make a conditional that only displays Text A and a separate condition that displays Text B ( Player won if 7 or 11 or player lost otherwise, the actual rules are slightly more complex but i'm trying to grasp the conditional syntax – nbulgarides Nov 11 '21 at 18:51
  • @nbulgarides: We already have some other duplicates that explain it in different words, like [MIPS conditional printing all cases](https://stackoverflow.com/q/52597380) / [If else in MIPS](https://stackoverflow.com/q/12439356) / [MIPS if-else with printing trouble](https://stackoverflow.com/q/46410654). Taking the time to understand one or more of those examples should make it clearer. – Peter Cordes Nov 11 '21 at 18:53
  • @nbulgarides: At each point in the code, if you *don't* want to go on to the next instruction, you have to think about where you want to go instead, and jump there. In this case, it sounds like after outputting the win message, you want to exit the program. You already have code to do this at the `end` label, so you can simply write `j end` after the `syscall`. – Nate Eldredge Nov 11 '21 at 18:53
  • This was helpful but I actually need to run multiple times to see how many times the player wins out of N games. The j end is stopping the multiple outputs but is also hindering the next iterations of the game. How can I have the code jump back to the RNG lines of generating numbers? Do I just use an instruction with a specific line number to return to that line? – nbulgarides Nov 11 '21 at 19:01
  • Actually I figured it out, im using J plus a label earlier in the code to jump back up to that line – nbulgarides Nov 11 '21 at 19:04
  • I like to use this analogy: Assembly is like that episode of Tom and Jerry where the baby escapes from his crib and wanders through a construction site. Jumps and branches are how Tom and Jerry follow the baby around and keep him safe. – puppydrum64 Nov 22 '22 at 18:17