在MIPS中打印字符串数组

时间:2015-02-07 18:24:41

标签: arrays string mips

我想在MIPS中打印出一个数组。这就是我所做的

.globl main
.data
    hello: .asciiz "Hello, the string is:\n"
    names:
        .align 3
        .asciiz "MIPS"
        .align 3
        .asciiz "IS"
        .align 3
        .asciiz "CRAZY"
        .align 3

.text
main:
    la $a0, hello
    li $v0, 4
    syscall

    #print the first member of the names array
    la $t0, names
    lw $a0, 0($t0)
    li $v0, 4
    syscall

    #exit
    li $v0, 10
    syscall

但是当我组装和运行时,MARS报告有一个超出范围的地址。我怀疑我做错了是从阵列中取出第一个元素。有人可以帮我解释一下代码中的错误吗?

3 个答案:

答案 0 :(得分:2)

您需要加载需要打印的字符串的地址,而不是字符串本身

la $t0, names
lw $a0, 0($t0)

而不是写下面的

la  $a0,names

这将打印" MIPS IS CRAZY"

答案 1 :(得分:0)

问题是syscall 4要求$a0是要显示的字符串的地址,但是你要向$a0加载垃圾(0或第一个字符的内容)要显示的字符串)。

不知道为什么要对齐字符串。

我修改代码看起来像这样:

.globl main
.data
    hello: .asciiz "Hello, the string is:\n"
    names1:
        .asciiz "MIPS"
    names2:
        .asciiz "IS"
    names3:
        .asciiz "CRAZY"

.text
main:
    la $a0, hello
    li $v0, 4
    syscall

    #print the first member of the names array
    la $a0, names1
    syscall

    #print the second member of the names array
    la $a0, names2
    syscall

    #exit
    li $v0, 10
    syscall

答案 2 :(得分:-1)

li $t0,0

addi $t0,$t0,16 #->porque al usar aling va de 8 en 8 (0,8,16...)

la $a0,names($t0)
相关问题