#---------------------------------
# Lab 7: Pixel Conversion
#
# Name: <Joe Puccio>
# Onyen: <jpuccio>
#


.data 0x0
	pe:		.byte 0x00000000	
	pnumber:	.word 0x00000000
	rowCount:	.word 0x00000000
	columnCount:	.word 0x00000000
	maxValue:	.word 0x00000000
	totalScans:	.word 0x00000000
	r:		.word 0x00000000
	g:		.word 0x00000000
	b:		.word 0x00000000
	
	p2NewLine:	.asciiz	"P2\n"
	newLine:	.asciiz "\n"
	
	i:		.word 0x00000000
	

.text 0x3000

main:
	#read the p
	ori $v0, $0, 12				#System call code 12 for reading in character
	syscall					#ask for input then store in v0
	sw $v0, pe				#store the p in pe
	
	#read the pNumber
	ori $v0, $0, 5 				#System call code 5 for reading in integer
	#syscall					#ask for input then store in v0
	sw $v0, pnumber				#store the p in pe
	
	#read in the row count			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	sw $v0, rowCount			#store the rowCount in the data
	or $t0, $0, $v0				#store the rowCount in t0 for later use
	
	#read in the column count			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	sw $v0, columnCount			#store the columnCount in the data
	or $t1, $0, $v0				#store the columnCount in t0 for later use
	
	#read in the maxValue 			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	sw $v0, maxValue			#store the maxValue in the data
	or $s4, $0, $v0				#store maxValue in s4
	
	#calculate and store scans needed
	mul $t2, $t1, $t0			#calculate scans needed. Store in t2
	sw $t2, totalScans			#storeResultinScans
	
	#print P2
	la $a0, p2NewLine			#put address of "P2" in register $a0 for printing
	ori $v0, $0, 4				#sys call 4 is print string in a0
	syscall
	
	#print numrows
	or $a0, $0, $t0				#put integer value of rows in t0 
	ori $v0, $0, 1				#sys code 1 prints int in a0
	syscall
	
	#print newLine
	la $a0, newLine				#loads New Line
	ori $v0, $0, 4
	syscall					#prints the new line
	
	#print numcolumns
	or $a0, $0, $t1				#put integer value of columns in t0 
	ori $v0, $0, 1				#sys code 1 prints int in a0
	syscall
	
	#print newLine
	la $a0, newLine				#loads New Line
	ori $v0, $0, 4
	syscall
	
	#print 255
	ori $a0, $0, 255			#loads 255
	ori $v0, $0, 1
	syscall	
	
	#print newLine
	la $a0, newLine				#loads New Line
	ori $v0, $0, 4
	syscall
	
	
	#set s1 to zero?

pixelLoop:					#load new pixel into $t2
	
	#note: t0 will be the i incrementer. 
	#t2 will be the max value (row*columns)
	#use t3 and greater for calculations
	#maxvalue is conveniently stored in s4
	
	#read in red, then green, then blue
	
	#read in the red 			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	or $t3, $0, $v0				#set t3 to be equal to red
	
	#read in the green 			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	or $t4, $0, $v0				#set t4 to be equal to green
	
	#read in the blue 			
	ori $v0, $0, 5				#System call code 5 for reading in integer					
	syscall				#ask for the input and store in v0
	or $t5, $0, $v0				#set t5 to be equal to blue
	
	
	
	#calculate grey value
	
	add $t6, $t3, $t4 			#start the sum
	add $t6, $t6, $t5 			#finish the sum
	
	#addi $t5, $0, 255			#store 255 in t5
	mul $t7,$t6,255				#do multiplication in numerator
	
	mul $t6, $s4, 3				#do multiplication in denominator 
	
	div $t8, $t7, $t6			#divide to get greyscale
	
	
	#print grey value
	
	#print the calculated value
	or $a0, $0, $t8				#loads the greyscale
	ori $v0, $0, 1
	syscall	
	
	addi $s6, $s1, 1
	beq $t2, $s6, exit		#don't print a new line at the very end
	
	#print newLine
	la $a0, newLine				#loads New Line
	ori $v0, $0, 4
	syscall
	
	
	
	addi $s1, $s1, 1 
			
	bne $t2, $s1, pixelLoop		#loop so long as the input is not negative (namely, negative 1)
	


exit:

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