【问题标题】:Get user input for integers and store in array获取用户输入的整数并存储在数组中
【发布时间】:2014-02-01 04:57:30
【问题描述】:

我试图让用户输入他们想要输入的整数数量,然后循环遍历该数量。此外,我想将之后的每个值都输入到一个数组中,但是我遇到了一个问题,检查循环是否继续直到用户输入的哨兵值。 li 不接受两个寄存器作为参数,还有其他方法吗?

.data

    arr1:
         .word 0:50 #allocate space for 50 integers (4-bits)
    msg:
         .asciiz "Give me the quantity of numbers:\n"

.text

 main:
         #print message
         li $v0, 4
         la $a0, msg    
         syscall

         #read int
         li $v0, 5
         syscall

         #store initial loop value
         move $t0, $v0

         li $8, 0
         li $13, $t0 #sentinel value for loop
         for:
              bge $8, $13, end_for #end the loop when you reach then number entered earlier

              #keep reading numbers
              li $v0, 5
              syscall

              move $t1, $v0
              la $9, arr1 #base address for array
              mul $10, $8, 4 #offset of 4 bytes
              add $11, $10, $9 #address for new element
              li $12, $t1
              sw $12, ($11) #save element at address
              add $8, $8, 1 #increment loop
              b for
          end_for:

【问题讨论】:

    标签: assembly architecture mips


    【解决方案1】:

    li 代表“加载立即数”,因此它总是采用一个寄存器和一个立即数。 要将一个寄存器值复制到另一个寄存器,请使用move destination source

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2018-01-19
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      相关资源
      最近更新 更多