# MIPS example: Manipulating arrays
# Author:  Ney Calazans
# Function: This code adds the value of a constant (const) to each element of an array
# 	The code works in a modified version of the MIPS-V0 architecture, where instructions
#	LUI and BLEZ are supported
#

	.data			# add what follows to the data segment of the program
array:	.word	0x12 0xff 0x3 0x14 0x878 0x31  0x62 0x10 0x5 0x16 0x20
				# Loads the list of ints into successive locations in memory
				# beginning with location A (i.e. we've initialized an array)
size:	.word	11		# size of the array
const:	.word	0x100		# constant to add to each element of the array
	
	.text			# Add what follows to the text segment of the program
	.globl	main		# Declare the label main to be a global one
main:
	la	$t0,array	# register $t0 contains the address of array
	la	$t1,size	# get address of size
	lw	$t1,0($t1)	# register $t1 contains the size of the array
	la	$t2,const	# get address of constant
	lw	$t2,0($t2)	# register $t2 contains the constant to add
	ori	$t4,$zero,4	# generate constant 4 in $t4
	ori	$t5,$zero,1	# generate constant 1 in $t5
loop:	blez	$t1,end		# if size is/becomes 0, end of processing
	lw	$t3,0($t0)	# get array element
	addu	$t3,$t3,$t2	# add constant
	sw	$t3,0($t0)	# update array
	addu	$t0,$t0,$t4	# update array pointer. Remember, 1word=4 memory addresses
	subu	$t1,$t1,$t5	# decrement array size counter
	blez	$zero,loop	# continue execution, producing inconditional jump from conditional one
	
	# now, return from main
end:	jr	$ra		# For MIPS-V0, this is an invalid instruction


