.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       prow = pointer to 1st element in row
# $t7       pelem = pointer to current array element
#                 = absolute address of ar2[rindex][cindex]
# $t8       bytes = no. of bytes in 1 row = ncols * size
# $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       # get pointer to start of array
		or $t6, $t0, $0   # initialize pointer to 1st row
		lw $t1, size
		lw $t2, nrows
		lw $t3, ncols
		mul $t8, $t3, $t1 # no. of bytes in 1 row = ncols * size
		addi $t4, $t2, -1 # nrmax
		addi $t5, $t3, -1 # ncmax
		or $s3, $t4, $0   # initialize row counter to nrmax
		lwc1 $f0, val
		mfc1 $s4, $0 
rloop:	or $t7, $t6, $0   # initialize pointer to 1st element of 1st row
        or $s2, $t5, $0   # initialize nc to ncmax
cloop:	sw $s4, 0($t7)    # store val in ar2[rindex][cindex]
		add $t7, $t7, $t1 # increment the column pointer by the size of 1 element
		addi $s2, $s2, -1 # decrement nc by 1
		bgez $s2, cloop   # branch back to cloop if nc >= 0
		add $t6, $t6, $t8 # increment the row pointer
		addi $s3, $s3, -1 # decrement nr by 1
		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!

