# 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)
	.data				# Use HLL program as a comment
x:	.word	12			# int x = 12; 
y:	.word	4			# int y = 4; 
res:	.word	0			# int res = 0; 
	.globl	main
	.text
main:	la	$s0, x			# Allocate registers for globals
	lw	$s1, 0($s0)		#   x in $s1
	lw	$s2, 4($s0)		#   y in $s2
	lw	$s3, 8($s0)		#   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:
	la	$s0, x			# Update variables in memory
	sw	$s1, 0($s0)		
	sw	$s2, 4($s0)		
	sw	$s3, 8($s0)
	jr	$ra

