以下代码是一个玩家2x2点和盒子游戏。它首先打印板并等待用户输入。之后,它只是不打印板,但成功地提出了问题。有什么见解吗?
.data
board: .ascii "\n\n . . . . 0 . 1 ."
.ascii "\n 2 3 4"
.ascii "\n . . . . 5 . 6 ."
.ascii "\n 7 8 9"
.asciiz "\n . . . . a . b .\n"
intro: .asciiz "\n One-Player 2x2 Dots-and-Boxes Game.\n"
prompt: .asciiz "\n Please Enter Next Move. (0..b): "
contPrompt: .asciiz "\n Would you like to Continue? (y/n): "
newGame: .asciiz "\n New Game? (y/n): "
wrongMove: .asciiz "\n---------Wrong Move-----------\n"
duplMove: .asciiz "\n--------Duplicate Move--------\n"
accept: .asciiz "y"
end: .asciiz "b"
buffer: .space 2
addr: .byte 6, 8, 33, 35, 37, 62, 64, 89, 91, 93, 118, 120
mark: .byte '-', '-', '|', '|', '|', '-', '-', '|', '|', '|', '-', '-'
.text
main:
la $a0, intro
li $v0, 4
syscall
j game
game:
# Printing the Board
la $a0, board
li $v0, 4
syscall
# Printing Prompt for Value
la $a0, prompt
li $v0, 4
syscall
# Reading Input Values
li $v0, 8
la $a0, buffer
li $a1, 2
syscall
# Checking for Validity
lb $t0, end
lb $t1, buffer
bgt $t1, $t0, error
lb $t0, buffer
j placePiece
placePiece:
# Placing Values
lb $t1, addr($t0)
lb $t2, mark($t0)
sb $t2, board($t1)
j printBoard
printBoard:
la $a0, board
li $v0, 4
syscall
j con
con:
la $a0, contPrompt
li $v0, 4
syscall
li $v0, 8
la $a0, buffer
li $a1, 2
syscall
lb $t0, accept
lb $t1, buffer
beq $t0, $t1, game
j exit
error:
la $a0, wrongMove
li $v0, 4
syscall
j game
exit:
la $a0, newGame
li $v0, 4
syscall
la $a0, buffer
li $a1, 2
li $v0, 8
syscall
lb $t0, accept
lb $t1, buffer
beq $t0, $t1, main
li $v0, 10
syscall
答案 0 :(得分:1)
我认为你的问题是你在写' \ 0'在董事会的开头。
您似乎读取了ASCII代码,然后使用该ASCII代码作为索引读取查找表。你应该减去ASCII码' 0'对于数字和减去(' a' -10)字母' a'和' b'。请注意,ASCII码	 9'之间存在差距。和ASCII码' a'。
您也可以将最后两个电路板移动更改为':'
和';'
,因为它们位于ASCII表格中的'9'
旁边。
因此,假设您使用'a'
更改':'
而'b'
更改';'
,则需要在跳转到'0'
之前减去placePiece
,例如:
lb $t0, buffer
subiu $t0, $t0, '0' # convert to array index
j placePiece