【问题标题】:How do I store more than one user entered integer without using an array in MIPS?如何在不使用 MIPS 中的数组的情况下存储多个用户输入的整数?
【发布时间】:2019-11-21 19:33:48
【问题描述】:

我的问题是;如何在不创建数组的情况下将多个用户输入的输入存储到变量中?当我运行我在此处发布的代码时,我收到错误“存储地址未在字边界上对齐”。变量一是store1,变量二是store2。我想将两个单独输入的整数分别存储到 store1 和 store2 中。

.data

store1: .byte 4                     #Stores data entered by user
store2: .byte 4                     # "                        "
msg: .asciiz "Enter your first decimal number: "
msg2: .asciiz "Enter your second decimal number: "
.text

main:

la $a0, msg #Displays msg
li $v0, 4
syscall

li $v0, 5   #Prompts user to enter an integer
syscall

la $t0, store1  #Loads user input into store1
sw $v0, store1  #Stores user input into store1

la $a0, msg2    #Displays msg2
li $v0, 4
syscall

li $v0, 5   #Prompts user to enter another integer
syscall

la $t1, store2    #My error occurs here 
sw $v0, store2    #If I delete these 3 lines the code compiles with no errors
syscall

li $v0, 10  #Cleanly exits the program 
syscall 

【问题讨论】:

  • 你只分配了一个字节。如果您打算存储字节,请使用sb 而不是sw。相反,如果您想要单词,请使用 .int 或 mars 中的任何等效项。
  • 明白。使用sb 更正了该问题。以及将.byte 4 更改为.int 0 感谢您的帮助。

标签: assembly memory mips mars-simulator


【解决方案1】:

.byte 4 是一个字节,值为 4,而不是四个字节(一个字)。所以它们不能都是字对齐的。您可以通过使用调试器查看内存内容来看到这一点。

如果 MARS 允许 GAS 伪指令,您可能正在寻找 .skip 4

或者像 Jester 说的那样使用 .int 0.word 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多