简体   繁体   English

MIPS程序集:如何知道用户输入的值是否正确存储到数组中

[英]MIPS Assembly: how to know if user inputted values are correctly stored into an array

.data
      array: .word 0:5
      prompt1: .asciiz "enter number: "
      newline: .asciiz "\n"
.text

      add $t2,$zero,$zero       # initializes counter to 0
      la $s0, array     # stores the beginning of array into $s0

secretcode:

      li $v0, 4                 # prints "enter number: "   
      la $a0, prompt1
      syscall

      li $v0, 5                 # reads in user input
      syscall
      sw $v0, ($s0)     # saves user input into address at $s0
      addi $s0, $s0, 4          # increments address at $s0 by 4 bytes

      addi $t2, $t2, 1          # increments counter by 1
      bne $t2, 5, secretcode    # stops loop when loop executes 5 times 

printsecretcode:

      lw $a0, ($s0)         # print first element
      li $v0, 1
      syscall

      li $v0, 10                # system code halt
      syscall

The program is supposed to store 5 user inputted numbers into an array. 该程序应该将5个用户输入的数字存储到一个数组中。 I tried to print the first value but it comes up as a large number which I assume is an address. 我尝试打印第一个值,但它显示为一个很大的数字,我认为这是一个地址。 How would I print the actual value of the number so that I know it saved correctly? 如何打印数字的实际值,以便我知道它已正确保存?

By the time you reach your printing code $s0 contains the address array + 4*5 , so what you end up printing is the 32-bit number formed by the first four characters of the "enter number: " string. 在您到达打印代码时, $s0包含地址array + 4*5 ,因此最终打印的是由"enter number: "字符串的前四个字符形成的32位数字。

To fix this, add an la $s0, array before you try to print the first element. 要解决此问题,请在尝试打印第一个元素之前添加la $s0, array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM