[英]Simple yes/no loop to repeat/end - MIPS assembly (MARS)
bI'm not so sure why it's not reading the input and deciding to repeat or end.. Here is my code, help would be appreciated! b)我不太确定为什么它不读取输入内容并决定重复还是结束。
.data
again:
.asciiz "Again (y or n)? "
answer:
.asciiz " "
.text
.globl main
main:
li $v0, 4
la $a0, again
syscall
la $s4, answer
jal get
beq $v0, 'y', main
beq $v0, 'Y', main
li $v0, 10
syscall
get:
li $v0, 12
li $a1, 2
syscall
jr $ra
Consider this: 考虑一下:
.data
again:
.asciiz "Again (y or n)? "
answer:
.space 256
.text
.globl main
main:
li $v0, 4
la $a0, again
syscall
la $a0, answer
li $a1, 3
li $v0, 8
syscall
lb $t4, 0($a0)
beq $t4, 'y', main
beq $t4, 'Y', main
li $v0, 10
syscall
Firstly, in your code, you seem to misunderstand the way the syscalls work and how they ought to interact with functions ( some docs here ). 首先,在您的代码中,您似乎误解了系统调用的工作方式以及它们应如何与函数进行交互( 此处提供一些文档 )。 Because your get routine basically just called a syscall, I took it out as adding a routine for something that basic is probably adding complexity rather than reducing it.
因为您的get例程基本上只称为syscall,所以我把它作为添加例程的原因排除在外,而此例程可能会增加而不是降低复杂性。
Next, the main problem of your code was a misunderstanding of the way input buffering works. 接下来,代码的主要问题是对输入缓冲工作方式的误解。 In your code, you allocated exactly 2 bytes of space for
answer
, and then use syscalls to get no more than 2 bytes at a time. 在您的代码中,您正好为
answer
分配了2个字节的空间,然后使用syscalls一次获取不超过2个字节。 This will not work for stdin
, because on most systems, stdin
is line buffered, meaning that the user must press ENTER
in order to flush the stream. 这对于
stdin
无效,因为在大多数系统上, stdin
是行缓冲的,这意味着用户必须按ENTER
才能刷新流。 This means that when the user types 'y'
, a syscall
actually returns "y\\n\\0"
. 这意味着,当用户键入
'y'
, syscall
实际上返回"y\\n\\0"
。
To fix this I expanded the syscall
to read 3 characters and answer to store up to 256. Expanding this to be safe for any size is an excersize left to the reader. 为了解决这个问题,我将
syscall
扩展为读取3个字符并回答以存储多达256个字符。将其扩展为对任何大小都安全的做法是使阅读器拥有了一定的特权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.