【问题标题】:MIPS saving data in arraysMIPS 将数据保存在数组中
【发布时间】:2025-12-20 22:50:11
【问题描述】:

我在运行程序时遇到错误:存储地址未对齐字边界,我该怎么办?

代码如下:

        .data
welcome: .asciiz "Welcome to Memorization Game. \n\nYour need to enter the numbers printed in the exact sequence. Press S to start.\n"
start:   .asciiz "\nHere we go....\n"
enter:   .asciiz  "\n Please enter the number:\n"
array:   .space 400
    .text

main:       la $t0, array       # load address of array
        la $a0, welcome
        li $v0, 4       #
        syscall         # print welcome message
        li $v0, 12      #
        syscall         # scan to continue 
        bne $v0, 115, Exit  # if $v0 != "s", jump to Exit
        la $a0, start       #
        li $v0, 4       #
        syscall         # Print start message
        li $s0, 0       # N0. of random generated numbers = 0
        add $t3, $t0, $0    # load address of array into $t3

Random:     slti $t2, $s0, 100  #
        beqz $t2, Exit      #
        addi $a0, $zero, 10 #   
        addi $a1, $zero, 99 #
        li $v0, 42      #
        syscall         # generate a random number
        sw $v0, ($t3)       # put number generated into array[n]
        addi $t3, $t3, 4    # next address
        addi $s0, $s0, 1    # counter++
        j Random

Exit:       li $v0, 10      #
        syscall         # terminate

【问题讨论】:

  • 更多详细信息:.align 如何在经典 MIPS 汇编器中工作(MARS/SPIM 遵循,与 clang 和针对 MIPS 的 GNU 汇编器不同):MIPS Assembly Alignment Align n

标签: arrays assembly mips memory-alignment memory-address


【解决方案1】:

尝试在array: .space 400 之前插入以下内容之一:

.p2align 2

 

.align 2

 

.align 4

另一个可能的选择是将array: .space 400 移动到数据部分的开头。

【讨论】:

  • 谢谢,这解决了问题,但我不认为随机生成的数字被存储。有什么建议吗?
  • 是的,使用调试器。此外,由于这个问题已经解决,其余的问题值得单独提问。但在发布新问题之前进行调试。