hphp95
hphp95

Reputation: 315

Printing string array in MIPS

I want to print out an array in MIPS. This is what I did

.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

But when I assemble and run, MARS report there is an address out of range. I doubt that what I did wrong is taking the first element out of the array. Could someone help me explain what is wrong in my code?

Upvotes: 2

Views: 9434

Answers (3)

irene
irene

Reputation: 1

li $t0,0

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

la $a0,names($t0)

Upvotes: -1

Devarsh Dani
Devarsh Dani

Reputation: 31

You need to load the address of the String that you need to print and not the string itself

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

instead of this write the below

la  $a0,names

This would print "MIPS IS CRAZY"

Upvotes: 2

gusbro
gusbro

Reputation: 22585

The problem is that syscall 4 requires $a0 to be the address of the string to be displayed, but you are loading garbage to $a0 (either 0 or the contents of the first characters of the string to be displayed).

Don't know why you are aligning the strings either.

I'd modify the code to look like this:

.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

Upvotes: 0

Related Questions