给定阵列的MIPS打印值

时间:2019-02-24 20:31:10

标签: mips

我正在尝试在控制台上打印数组的值,这些值已经在.data中作为intA给出。也就是说,尝试在没有用户提示的情况下打印数组的值。

我的代码:

.data 

prompt: .asciiz "The values in the array are:\n"
finished: .asciiz "\nNo more values to present"
space: .asciiz " "
intA:   .word   11, 2, 3, 4, 5, 34, 0

.text 
.globl main

main:

    #Prints the prompt string
    li $v0, 4
    la $a0, prompt 
    syscall 

    # initialization of a0, a1, and t3 (i, counter)
    la $a0, intA # loading starting address (base) of array in register a0
    addi $a1, $zero, 6 # array size - 1
    addi $t3, $zero, 0 # i initialized to 0

    j loop

loop: 

    lw $t1, 0($a0) # loading integer (value of array) in the current address to register t1, I use lw because integer is a word (4 bytes)

    # printing current value of array
    li $v0, 4
    la $a2, ($t1) 
    syscall 

    # spacing between values
    li $v0, 4
    la $a2, space
    syscall 

    # checking that next address is not outside of the array
    addi $t3, $t3, 1
    slti $t2, $t3, 6
    bne $t2, 1, done   

    # accessing next integer and jumping back to print it
    addi $a0, $a0, 4
    j loop

done:

    # indicating program is done
    li $v0, 4
    la $a0, finished 
    syscall 

我得到的输出:output

任何想法为什么它不打印数组的值,以及这些正方形打印出来的是什么?

编辑:

我改变了

# printing current value of array
    li $v0, 4
    la $a2, ($t1) 
    syscall

# printing current value of array
    li $v0, 1
    lw $a2, ($t1) 
    syscall 

因为,据我了解,我打印了一个整数,因此应将$ v0馈入1,而应将lw而不是la(因为它是一个整数,即一个单词)

但是,现在在第31行出现运行时错误:lw $a2, ($t1)告诉我

  

获取地址未在字边界0x0000000b上对齐

1 个答案:

答案 0 :(得分:0)

解决方案:我需要执行lw $a0, ($t1)而不是$t1来打印add $a0, $t1, $zero的值,因为我试图使用该值而不访问地址。