【问题标题】:Mips, continuous user inputMips,连续的用户输入
【发布时间】:2020-05-31 11:49:30
【问题描述】:

我将如何要求用户持续输入?基本上我只想捕获大于 0 的数字,小于 0 的数字我想打印一条错误消息,然后是输入数字消息。

【问题讨论】:

  • 使用一个或多个条件分支编写一个循环。如果您还没有MIPS32™ 程序员架构第二卷:MIPS32™ 指令集,请下载它,并仔细阅读它以找到您可以使用的合适指令。

标签: loops while-loop io conditional-statements mips


【解决方案1】:

您将如何用高级语言伪代码解决这个问题?一种方法是 -

while true
  print("Enter a number: ")
    n = read_a_number()
    if n < 0
      print("Error. Negative number")
    else
      # process the non-negative number (>= 0)
    # detect when to break while loop

一旦清楚以上内容,就很容易将其转换为 MIPS。

.data
prompt: .asciiz "Enter a number: "
error:  .asciiz "Error. Negative number\n"

.text
while:
la   $a0, prompt                    # print prompt to enter a number
li   $v0, 4                         # $v0 = 4 to print string with address in $a0
syscall

li   $v0, 5                         # to read an int. Read in $v0
syscall

bltz $v0, negative                  # is the input negative?
j    non_negative                   # else, handle non_negative

negative:
la   $a0, error                     # print error
li   $v0, 4
syscall
j    next                           # and goto 'next'

non_negative:
# process the non-negative number (>= 0)
j    next                           # and goto 'next'

next:
# detect when to break while loop (or do that at the start of the loop)
j    while                          # continue loop

after_while:                        # after the loop
li   $v0, 10                        # $v0=10 to exit
syscall

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多