Arrays

As you may or may not be aware, arrays are a collection of items that are contiguous in memory. (all next to each other, in a single block of memory).

Because arrays generally contain many items, we cannot store an entire array in a register. Instead what we do is get the memory address of the first value in memory, and then from there we can load individual items by offsetting from the base address and loading the value at that address.

Say we declare an array in MIPS

.data
MY_ARRAY:
		.word 11
		.word 20
		.word 15
		.word 7
		.word 3

this creates a block of memory that looks something like this:

11 20 15 7 3

Now, if we load the address of MY_ARRAY (la $t0, MY_ARRAY), we will get the address of the first item in the array.

Now that we have this address, we can use the formula base + offset to get the address of any other value in the array, and then we can load them.

Calculating offset

Offset depends on the size of the items. Words are 4 bytes each. So we need to offset by multiples of 4 to get to each next item. The formula can be thought of as offset = index * 4.

Lets look at an example:

la   $t0, MY_ARRAY  # t0 = address of first item in array (the value 11)

# say we want to load the 5th item of the array. So we need to offset by 16 (4 * 4)
# this can be done two ways.

# METHOD 1: using the offset field in lw
lw   $t1, 16($t0)   # t1 = MY_ARRAY[4] = 3

# method 2: calculating the offset address first, and then loading
# (useful when iterating through an array, because we may want to offset by a different amount each time)

addi $t1, $zero, 4  # t1 = the index we want to access (index 4)
sll  $t1, $t1, 2    # calculate offset (index * 4)
add  $t1, $t0, $t1  # t1 = base address + offset
lw   $t1, 0($t1)    # t1 = MY_ARRAY[4] = 3 

Strings

Strings are simply arrays of characters. And all the same rules and workflows apply. The difference is that, unlike integers (words), characters are only 1 byte each. so the formula for calculating offset is offset = index * 1 , in other words, you don’t need to multiply the index by anything. Also when loading characters, you use the lb instruction instead of lw, to only load a single byte of data (1 character).

Strings also generally have a “null terminator” which is a special character \\0 (usually just equal to the number 0x00). This character signifies the end of the string. Without it, you would have no way of knowing when you’ve reached the end.