2d arrays in MIPS

MoBsTa

Limp Gawd
Joined
Jan 22, 2005
Messages
241
I have a computer assignment that requires me to use 2d arrays and check if the elements are "singleton's" in a double nested for loop. I wrote the loops and singleton function but I am having trouble figuring out what the starting address for a 2d array is.

Here is the formula that I found to find a certain element. Let's call the matrix A:

A[i,j] = A[0,0] + (((i*N) + j)*sizeof(element))

the sizeof(element) I believe is 4, since we're using hexadecimal to represent each value. The input to this function is $a0 = A[9,9].

The problem I am having is I do not know how to represent the beginning of the matrix A[0,0]. Right now I have it as $a0.
 
i havent done mips in a while but dont you just use a label for your 2d array which would just be a .space type
so a 3 by 3 array will be represented by a 9 length .space array

to get the starting address just do an la command on the label
 
Does your program require dynamic allocation of the array? If so, you should look at syscall 9.
Code:
li $v0, 9
move $a0, $t0 # where $t0 contains the size, in bytes, of the memory to allocate
syscall       # allocate the memory and return the base address of it in $v0

If you're instead using the .space directive to create a static array, simply use this command to retrieve and store its base address into $t0:
Code:
la $t0, name_of_array
 
Last edited:
Back
Top