# INEL 4206 - Microprocessors
# University of Puerto Rico Mayaguez
#
# Program that performs division by repeated subtraction
# The operands reside in global variables x and y
# The result of x/y ends up in global variable res
# The remainder of the division ends up in x
#
# Instructional Objectives
# + teach how to program control structures (while)
# + teach load and store pseudo instructions
# + teach how to use I/O system calls
	.data				# Use HLL program as a comment
x:	.word	12			# int x = 12; 
y:	.word	7			# int y = 7; 
res:	.word	0			# int res = 0;
pf1:	.asciiz "Result = "		# Format string
	.globl	main
	.text
main:					# Allocate registers for globals
	ulw	$s1, x			#     x in $s1
	ulw	$s2, y			#     y in $s2
	ulw	$s3, res		#     res in $s3
while:	bgt	$s2, $s1, endwhile	# while (x <= y) {
	sub	$s1, $s1, $s2		#   x = x - y; 
	addi	$s3, $s3, 1		#   res ++; 
	j	while			# }
endwhile:
	usw	$s1, x			# Update variables in memory
	usw	$s2, y
	usw	$s3, res
	la	$a0, pf1		# printf("Result = %d");
	li	$v0, 4			# System call to print_str
	syscall
	move	$a0, $s3		
	li	$v0, 1			# System call to print_int
	syscall
	jr	$ra
