#---------------------------------
# Lab 5: Pixel Conversion
#
# Name: <Joe Puccio>
# Onyen: <jpuccio>
#
# --------------------------------
# Below is the expected output.
# 
# Converting pixels to grayscale:
# 0
# 1
# 2
# 34
# 5
# 67
# 89
# Finished.
# -- program is finished running --
#---------------------------------

.data 0x0
	startString:	.asciiz	"Converting pixels to grayscale:\n"
	finishString:	.asciiz	"Finished."
	newline:	.asciiz	"\n"
	pixels:		.word 	0x00010000,	0x010101,	0x6,		0x3333, 
				0x030c,		0x700853,	0x294999,	-1

.text 0x3000

main:
	ori $v0, $0, 4				#System call code 4 for printing a string
	ori $a0, $0, 0x0   	 		#address of startString is in $a0
	syscall					#print the string

	addi $t1, $zero, 0			#t1 holds our loop ticker
	lw $t2, pixels($t1)
pixelLoop:					#load new pixel into $t2
	
	srl $t3, $t2, 16			#get the red in one go
	sll $t4, $t2, 16			#remove some
	srl $t4, $t4, 24			#get the green
	sll $t5, $t2, 24			#remove some
	srl $t5, $t5, 24			#get the red
	
	add $t6, $t5, $t4			
	add $t6, $t6, $t3			#put the sum in 6
	
	addi $t7, $zero, 3 			#store a three in t7 for division
	div $t6, $t7				#make the division, it's stored in LO
	
	mflo $s1				#store result in s1
	
	ori $v0, $0, 1				#need to tell it to print an int
	ori $a0, $s1, 0x0
	syscall
	
	ori $v0, $0, 4				#print the new line
	ori $a0, $0, 43
	syscall
	
	
	addi $t1, $t1, 4 
	lw $t2, pixels($t1)			#change offset by 4 to load new word
	bne $t2, 0xffffffff, pixelLoop		#loop so long as the input is not negative (namely, negative 1)
	


exit:

	ori $v0, $0, 4				#System call code 4 for printing a string
	ori $a0, $0, 33  			#address of finishString is in $a0; we computed this
						#  simply by counting the number of chars in startString,
						#  including the \n and the terminating \0

	syscall					#print the string

	ori $v0, $0, 10				#System call code 10 for exit
	syscall					#exit the program
