assemblymipsmars-simulator

Storing words into an array in Mars MIPS


I'm trying to teach myself Assembly using Mars for the MIPS architecture and am wondering how to store a series of words into an array.

I know that if I have 4 words, I'd allocate 16 bytes like so:

.data
X: .space 16

I then have some words:

.data
X:       .space 16
Ryan:    .asciiz "Ryan"
Tammi:   .asciiz "Tammi"
Mike:    .asciiz "Mike"
Jessica: .asciiz "Jessica"

Now, if I want to assign X[0] to Ryan, X[1] to Tammi, etc, do I first load the array address into $a0, then load the words into temp registers? I'm not quite sure how to assign the values to their respective indexes and then print the values to the console.


Solution

  • If you don't need to do it at runtime, you can of course just initialize your array with the pointers directly:

    .data
    X:       .word Ryan, Tammi, Mike, Jessica
    Ryan:    .asciiz "Ryan"
    Tammi:   .asciiz "Tammi"
    Mike:    .asciiz "Mike"
    Jessica: .asciiz "Jessica"
    

    Otherwise you'd have to manually do the assignment at runtime, such as:

    la $t0, X
    la $t1, Ryan
    sw $t1, ($t0)
    la $t1, Tammi
    sw $t1, 4($t0)
    la $t1, Mike
    sw $t1, 8($t0)
    la $t1, Jessica
    sw $t1, 12($t0)
    

    Subsequently you can print them using a loop.