# 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

	li	$s1, 0xffff0000		# s1 - base addr of kbd regs
					# Enable interrupts on status reg
	lw	$t9, 0($s1)
	ori	$t9, $t9, 0x0002	# Enable interrupts on kbd device
	sw	$t9, 0($s1)

	mfc0	$t9, $12
	ori	$t9, $t9, 0xFF00
	mtc0	$t9, $12

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

