.data
ar2:        .word   0:12    # array of 12 integers (4 bytes each)
val:        .float  1.e-3   # value to store in array elements
nrows:      .word   4       # no. of rows
ncols:      .word   3       # no. of columns
size:		.word	4		# size of an array element, in bytes

# Register usage
# For clarity, each register holds only one variable
#  Nobody would program in this way, because some registers can be re-used
#
# $t0       base address (address of ar2[0][0])
# $t1       size = size of an array element (in bytes)
# $t2       nrows = number of rows
# $t3       ncols = number of columns
# $t4       nrmax = nrows - 1 = max. value of row index
# $t5       ncmax = ncols - 1 = max. value of column index
# $t6       rindex = row index
# $t7       cindex = column index
# $t8       addr = absolute address of ar2[rindex][cindex]
# $t9       roffset = rindex * ncols * size
# $s0       coffset = cindex * size
# $s1       offset = roffset + coffset
# $s2       nc = column counter to be decremented
# $s3       nr = row counter to be decremented
# $s4       value to be stored in each array element


.text
__start: 
		la $t0, ar2 
		lw $t1, size
		lw $t2, nrows
		lw $t3, ncols
		addi $t4, $t2, -1 # nrmax
		addi $t5, $t3, -1 # ncmax
		ori $t6, $0, 0    # initialize row index to 0
		lwc1 $f0, val
		mfc1 $s4, $0 
rloop:	mul $t9, $t6, $t3 # multiply rindex by ncols
		mul $t9, $t9, $t1 # multiply by size of one array element to get roffset
		ori $t7, $0, 0    # initialize column index to 0
cloop:	mul $s0, $t7, $t1 # multiply cindex by size to get coffset
		add $s1, $s0, $t9 # offset of ar2[rindex][cindex] = roffset + coffset 
		add $t8, $s1, $t0 # address of ar2[rindex][cindex] = offset + base 
		sw $s4, 0($t8)    # store val in ar2[rindex][cindex]
		addi $t7, $t7, 1  # increment the column index
		sub $s2, $t5, $t7 # nc = ncmax - cindex
		bgez $s2, cloop   # branch back to cloop if nc >= 0
		addi $t6, $t6, 1  # increment the row index
		sub $s3, $t4, $t6 # nr = nrmax - rindex
		bgez $s3, rloop   # branch back to rloop if nr >= 0 
		ori $v0, $0, 10   # reach here if row loop is done
		syscall           # end of program!

