Assembly 汇编语言猜谜游戏。我的代码不断循环,即使猜测应该是正确的?
我在这方面真的很新,所以我甚至不确定我的代码是否正确。事实上,这是我的第一次。 所以,我想我做的每件事都是正确的?我不知道为什么它不会退出循环,因为Assembly 汇编语言猜谜游戏。我的代码不断循环,即使猜测应该是正确的?,assembly,mips,Assembly,Mips,我在这方面真的很新,所以我甚至不确定我的代码是否正确。事实上,这是我的第一次。 所以,我想我做的每件事都是正确的?我不知道为什么它不会退出循环,因为 beq$t1,$t0,winExit应该让它退出我的循环。loseExit也存在同样的问题。也许这一切都错了。。。如果是我的错>> # The program will ask the player to try to guess the secret number # and receive a numerical input from the
beq$t1,$t0,winExit
应该让它退出我的循环。loseExit也存在同样的问题。也许这一切都错了。。。如果是我的错>>
# The program will ask the player to try to guess the secret number
# and receive a numerical input from the players.
.data
# constant strings
prompt1: .asciiz "Please guess the secret number (up to 10 tries): "
tooHigh: .asciiz "Incorrect: Number is too high\n"
tooLow: .asciiz "Incorrect: Number is too low\n"
userWin: .asciiz "Congratulation! You guessed the secret number! :D"
userLose: .asciiz "Sorry, You lose :/"
.text
main:
#start of the loop
loop:
#Set secret number
li $t0,4 #$t0 hold the secret number: 4
li $t1,9 #$t1 hold the maximum number of tries: 10
li $t2,0 #$t2 hold the number of tries. (counter)
#Prompt user for a number
li $v0,4 #code for print_string
la $a0,prompt1 #point to $a0 to prompt strings
syscall #print prompt
#Get integer from user
li $v0,5 #code for read_int
syscall #get integer from user --> returned in $v0
move $s0,$v0 #move the resulting int to $s0
#$s0 hold the guess number
#check if it is equal, if it is then exit loop
beq $t1, $t0, winExit #Brand if Equal. User guess the correct number.
addi $t2,$t2,1 # +1 to counter
beq $t2,$t1, loseExit #check if counter reach the maximum number
blt $s0,$t0, lessThan #Branch on Lower Than. check if guess number is less than secret number
bgt $s0,$t0, greaterThan #Branch on Greater Than. check if guess number is higher than secret number
j loop #goes back to loop
#prompt guess number is too high then loop
greaterThan:
li $v0,4 #code for print_string
la $a0, tooHigh #point $a0 to tooLow string
syscall #print prompt
j loop
#go back to loop
#prompt guess number is too low then loop
lessThan:
li $v0,4 #code for print_string
la $a0, tooLow #point $a0 to tooLow string
syscall #print prompt
j loop #go back to loop
#print userWin to congratulate user
winExit:
li $v0,4 #code for print_string
la $a0, userWin #point $a0 to userWin string
syscall #print prompt
j exitProgram
#print userLose to user
loseExit:
li $v0,4 #code for print_string
la $a0, userLose #point $a0 to userLose string
syscall #print prompt
j exitProgram
#Exit program
exitProgram:
li $v0,10 #code for exit
syscall #exit program
检查什么是相等的?不是$s0
所以不是猜测的数字,可能尝试的次数等于密码?除非这些变量在t
(临时)寄存器中,否则系统调用可能没有保存它们,或者可能已经保存了,这取决于它,而只是遵循调用约定
loop:
#Set secret number
li $t0,4 #$t0 hold the secret number: 4
li $t1,9 #$t1 hold the maximum number of tries: 10
li $t2,0 #$t2 hold the number of tries. (counter)
如果另一个问题被解决了,这仍然会给玩家无限次的重试,因为每次迭代尝试的次数都会被重置为零。哦,哇,我真傻。你不知道为什么我要检查它而不是$s0 lol>>我该怎么做才能让它记住计数器?将其移动到寄存器?@Qezzz:MARS系统调用不会对任何寄存器(返回值除外)进行缓冲。您可以假设其他一切都在系统调用本身中生存。(一些系统调用以
$v0
或$a0
的形式返回,通常您必须将参数放入一些寄存器中,这意味着您自己的代码必须使用它们。)
loop:
#Set secret number
li $t0,4 #$t0 hold the secret number: 4
li $t1,9 #$t1 hold the maximum number of tries: 10
li $t2,0 #$t2 hold the number of tries. (counter)