# 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 (if, for, while)
# + teach load and store pseudo instructions
# + teach how to use library functions (e.g. printf)

	.data				# Use HLL program as a comment
x:	.word	12			# int x = 12; 
y:	.word	7			# int y = 12; 
res:	.word	0			# int res = 0;
pf1:	.asciiz "Result = "
pf2:	.asciiz "Remainder = "
	.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 \n");
	li	$v0, 4			# System call to print_str
	syscall
	move	$a0, $s3		
	li	$v0, 1			# System call to print_int
	syscall
	la	$a0, pf2		# printf("Remainder = %d \n");
	li	$v0, 4			# System call to print_str
	syscall
	move	$a0, $s1
	li	$v0, 1			# System call to print_int
	syscall

