# SPIM code to initialize the elements of a 1-d array
#  with the value stored in location val

.data
ar1:        .word   0:20		# array of 20 integers (4 bytes each)
val:        .float  1.e-3		# value to store in array elements
base:       .word	0x10010000	# will hold address of ar1[0]
index:      .word   0			# array index; initialized here to 0
nmax:       .word   19			# no. of array elements - 1; max. value
                      			#   of array index
size:       .word   4			# size of an array element, in bytes
addr:       .word   0			# absolute address of ar1[index]
offset:     .word   0			# offset from first array element
n:          .word


.text
__start:
		la		$t0,ar1				# get absolute address of ar1[0]
		                            # addresses of other array elements
		                            #  will be computed as base + offset
		lw		$t1,offset
		lw		$t2,index
		lw		$t3,size
		lw		$t4,base
		lw		$t6,val
		lw		$t7,nmax
loop:   mul     $t1,$t2,$t3			# loop begins here
        add     $t5,$t1,$t4			# compute absolute address of ar1[index]
		sw		$t6,0($t5)			# store val in ar1[index]
		addi	$t2,$t2,1			# increment array index
		sub		$t8,$t7,$t2			# subtract array index from (nelems - 1)
		bgez	$t8, loop			# branch back to loop if diff >= 0
		ori     $v0,$0,10
		syscall	

