[prev] 18 [next]

2-d Arrays in MIPS (cont)

Computing sum of all elements for strategy (a)  int matrix[6][5]

   li  $s0, 0             # sum = 0
   li  $s1, 6             # s1 = #rows
   li  $s2, 0             # row = 0
   li  $s3, 5             # s3 = #cols
   li  $s4, 0             # col = 0 // redundant
   li  $s5, 4             # intsize = sizeof(int)
   mul $s6, $s3, $s5      # rowsize = #cols*intsize
loop1:
   bge  $s2, $s1, end1    # if (row >= 6) break
   li   $s4, 0            # col = 0
loop2:
   bge  $s4, $s3, end2    # if (col >= 5) break
   mul  $t0, $s2, $s6     # t0 = row*rowsize
   mul  $t1, $s4, $s5     # t1 = col*intsize
   add  $t0, $t0, $t1     # offset = t0+t1
   lw   $t0, matrix($t0)  # t0 = *(matrix+offset)
   add  $s0, $s0, $t0     # sum += t0
   addi $s4, $s4, 1       # col++
   j    loop2
end2:
   addi $s2, $s2, 1       # row++
   j    loop1
end1: