MIPS assembly printing element of an array

0
 .globl main
    .data
    array:
        .asciiz "a" 
        .align 5
        .asciiz "b"
        .align 5
        .asciiz "c"
    .text
    main:
    # Loads address of array into $a0
    la $t0, array
    # Loads a[0]
    lw $a0, 0($t0)
    # Sets to print string
    li $v0, 4
    #prints the string
    syscall

The above MIPS assembly code is giving me the error: Runtime exception at 0x00400010: address out of range 0x00000061 when I try to load into $a0. I've tried also using 32 next to ($t0) as well but nothing seems to be letting me load from my array.

Thoughts?

assembly
mips
asked on Stack Overflow Feb 2, 2016 by EgoKilla

1 Answer

1

It seems very unlikely that the exception occurs at lw. It most likely occurs when you perform the syscall.

Let's look at what your data section looks like:

10010000: 61 00 00 00 00 00 ....
10010010: 00 00 00 ...
10010020: 62 00 00 ...
...

(the numbers above are all hex).

What you do with la $t0, array is set $t0 to the address of the first byte in the array, i.e. $t0 = 0x10010000.
lw $a0, 0($t0) then loads the first word from that address, i.e. 0x00000061. You then pass 0x00000061 as the address of a string to print using system call 4, which results in an exception.

It's not really clear to me what you want to do. If you wanted to print "a" you should've used la $a0, array. If you wanted array to contain string addresses rather than the string contents you would have to change the way you declare your data to reflect that.

answered on Stack Overflow Feb 2, 2016 by Michael

User contributions licensed under CC BY-SA 3.0