我遇到了这个问题,试图在MIPS Assembly中获取用户的char输入。 我似乎不明白为什么,当输入一个字符时,不需要按ENTER键,而当输入任何其他类型,如.word时,你总是要按ENTER键。你可以向我解释一下,并告诉我在代码中我做错了什么吗?谢谢。
以下是相关代码:
.data
message: .asciiz "Please, enter the number of steps: \n"
message2: .asciiz "The factorial is: \n"
cont: .asciiz "Do you want to continue? (Y or N) \n"
yes: .byte 'Y'
no: .byte 'N'
.text
main:
# $t1 contains the iterating number
addi $t1, $zero, 1
# $t3 contains the factorial
addi $t3, $zero, 1
# set $t2 which is the number we want a factorial for to 0
add $t2, $zero, $zero
# now $t5 contains 'Y' and $t6 contains 'N'
lb $t5, yes
lb $t6, no
while:
# show the message
li $v0, 4
la $a0, message
syscall
# read the number
li $v0, 5
syscall
# $t2 contains the condition for the loop, which is the number we want a factorial for
add $t2, $t2, $v0
for:
# while $t1 is less than $t2 do
bgt $t1, $t2, end
# $t3 = $t3 * $t1
mul $t3, $t3, $t1
# $t1++
addi $t1, $t1, 1
# jump at the start of the loop
j for
end:
# prints "Do you want to continue?"
li $v0, 4
la $a0, cont
syscall
# reads a character
li $v0, 12
syscall
# if the answer is yes, jump to the start of the while loop
beq $v0, $t5, while
# if no, show the result
beq $v0, $t6, show
# else ask another time
j end
show:
# prints out the message
li $v0, 4
la $a0, message2
syscall
# prints out the result
li $v0, 1
add $a0, $zero, $t3
syscall
# end of the main function
li $v0, 10
syscall