Struggling with 2D arrays in assembly

3k views Asked by At

I am trying to implement the following in MIPS

X is a two-dimensional array (matrix) of double-precision floating-point numbers and Y is two-dimensional array of 32-bit integers. Trying to write code for the following

Z= (X[i][j] + Y[i][j])

Now I dont know how to declare these arrays in MIPS:

So far I have written some of the main program but dont know how to implement them in the .data section

.data

.text
.main:
mtc1 $0, $f0
 cvt.d.w $f0, $f0
  # $a1 is the address of the first element of x
  l.a $r1, $a1
  # $a2 is the address of the first element of y
  l.a $r2, $a2
  addi $r4, $0, 30
  add.d $f8, $f0, $f0
1

There are 1 answers

1
56phil On

2D arrays aren't that bad. The trick is to deal with one dimension at a time. If you have the size of each element, and the size of each dimension, the math is simple. Add the product of the row index(zero based), row size and element size (in bytes) to your base address. This will be the base address of the row you want. Add the product of the column index and element size to that and you have the address of the element.

As for the data segment, just reserve the total size of the array. For example if you need space for a 3x4, reserve 12 elements. If you're using integers, array: .word 0:12 will do what you need.