# ------------------------------------------------------------------
# divInt.s
#	Perform integer division.
# A signed long integer dividend, and a signed integer divisor, are
# parameters. The return value is a signed integer quotient.
# Note:	Caller is responsible for sign extension !! If bit 31 of
# the least significant dividend word is True, the function will
# interpret this word as representing an unsigned value greater
# than 2^30. Only a signed negative value in the most significant
# dividend word, will cause the function to recognize the dividend
# as negative. (Effectively, the lower word is an unsigned int.)
# ..................................................................
# calling sequence:
#	push integer quotient
#	push least significant word of dividend
#	push most significant word of dividend
#	call	div
#	add 12 to stack pointer
# ..................................................................
# author: David Bozarth - 30 April 2004
# modified 30 April 2004
# ------------------------------------------------------------------
STDIN = 0
STDOUT = 1
TRUE = -1
FALSE = 0
WORD = -4
#
# Stack frame
stkTop = blnDivisorNeg			# top-of-stack marker
blnDivisorNeg = blnDividendNeg + WORD	
blnDividendNeg = basePtr + WORD		# if TRUE, dividend is negative 
basePtr = 0
rtnAddr = basePtr - WORD
intHiWord = rtnAddr - WORD
intLoWord = intHiWord - WORD
intDivisor = intLoWord - WORD

	.data
strMsg:
	.string ""
	
	.text
	.align	4
	.globl	divInt

divInt:
	pushl	%ebp			# save caller's base pointer
	movl	%esp, %ebp		# establish our base pointer
	addl	$stkTop, %esp		# allocate local variables
	pushl	%ebx			# save registers
	pushl	%ecx
	pushl	%edx
	
	xorl	%edx, %edx		# initialize local variables
	movl	$FALSE, blnDividendNeg(%ebp)
	movl	$FALSE, blnDivisorNeg(%ebp)

	movl	$0x80000000, %ecx
	andl	intHiWord(%ebp), %ecx
	cmpl	$0, %ecx
	je	doneHi
	
	#cmpl	$0, intHiWord(%ebp)	# test for negative numbers
	#jae	doneHi

	movl	$TRUE, blnDividendNeg(%ebp)
	#negl	intHiWord(%ebp)

# note:	 this is "rigged" to get the project working, needs fixing
doneHi:
	# ****************
	movl	$0, intHiWord(%ebp)
	# ****************
	movl	$0x80000000, %ecx
	andl	intDivisor(%ebp), %ecx
	cmpl	$0, %ecx
	je	doneLo
	
	#cmpl	$0, intDivisor(%ebp)	# test for negative numbers
	#jae	doneLo

	movl	$TRUE, blnDivisorNeg(%ebp)
	negl	intDivisor(%ebp)
doneLo:
	movl	intHiWord(%ebp), %edx
	movl	intLoWord(%ebp), %eax
	divl	intDivisor(%ebp)	# quotient -> eax, remainder -> edx
	movl	blnDividendNeg(%ebp), %ebx
	xorl	blnDivisorNeg(%ebp), %ebx
	cmpl	$0, %ebx
	je	allDone

	negl	%eax			# return value	
allDone:
	popl	%edx			# restore registers
	popl	%ecx
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret
