【发布时间】:2021-11-11 18:37:46
【问题描述】:
我正在为一项任务开发“Craps”(一种骰子游戏),但我对代码中的一件事感到困惑。我试图弄清楚为什么当 beq if 语句触发时,win 提示和 lost 提示都会触发。在掷骰子的情况下,如果滚动玩家掷出 7 或 11,他们就赢了,并且不知何故,获胜条件也会触发代码底部的失败条件提示。
感谢您的帮助
.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 "```
【问题讨论】: