Article 2801 of vmsnet.internals:
Path: jac.zko.dec.com!nntpd.lkg.dec.com!pa.dec.com!decwrl!tribune.usask.ca!rover.ucs.ualberta.ca!hookup!news.mathworks.com!mvb.saic.com!macro32
From: "Brian Schenkenberger, VAXman-" <brians@advsyscon.com>
Newsgroups: vmsnet.internals
Subject: RE: 64 bit integers
Message-ID: <0099241A.FB316B80.1@AdvSysCon.COM>
Date: Thu, 22 Jun 1995 11:22:53 EST
Organization: Macro32<==>Vmsnet.Internals Gateway
X-Gateway-Source-Info: Mailing List
Lines: 88

In message <9506210628.AA24699@pvtbbvax2000.local>, sveton@ba.pvt.sk writes:

>How can I divide two 64 bit integers without floating-point operations ?
>
>Brano Sveton
>e-mail: sveton@ba.pvt.sk

Here's a little program which applies the old "grade-school" long division
method in binary.


	.TITLE	DIVDEMO
	.IDENT	"bjs"

	.PSECT	DATA,WRT,NOEXE,LONG
DIVIDEND	.LONG	^X33333334,^XFF333333
DIVISOR:	.LONG	2,0
QUOTIENT:	.QUAD	0


	.PSECT	CODE,NOWRT,EXE,LONG
	.ENTRY	DIVDEMO,0

;++
; 	-00CCCCCC'CCCCCCCC / 00000000'00000002 = -00666666'66666666
;--

	PUSHAQ	QUOTIENT
	PUSHAQ	DIVISOR
	PUSHAQ	DIVIDEND
	CALLS	#3,DIV64
	RET
	
	.ENTRY	DIV64,^m<R2,R3,R4,R5,R6,R7,R8,R9>

DIVD = 4
DIVR = 8
QUOT = 12

	CLRL	R1		; result (+) or (-); initially (+)
	MOVQ	@DIVD(AP),R6;R7	; move dividend to registers R6:R7
	BGEQ	10$		; dividend negative? branch if (+)

	MCOML	R6,R6		; compliment R6\
	MCOML	R7,R7		; compliment R7 \ quadword integer 
	INCL	R6		; add 1         / two's compliment
	ADWC	#0,R7		; add (C)      /
	XORL2	#1,R1		; mark result (-)

10$:	MOVQ	@DIVR(AP),R8;R9	; move divisor to registers R8:R9
	BGEQ	20$		; divisor negative? branch if (+)
	MCOML	R8,R8		; compliment R8\
	MCOML	R9,R9		; compliment R9 \ quadword integer 
	INCL	R8		; add 1         / two's compliment
	ADWC	#0,R9		; add (C)      /
	XORL2	#1,R1		; mark result conditionally (-)

20$:	CLRQ	@QUOT(AP)	; clear the quotient quadword

	MOVL	#63,R0		; 64 bits for multiplier

30$:	ASHQ	R0,R8,R2	; multiply R7;R8 by R0 -> R2;R3
	BVS	40$		; too big, try again

	;++			
	; R4:R5 = R6:R7 - R2:R3
	;--
	SUBL3	R2,R6,R4	; R4 = R6 - R2
	MOVL	R7,R5		; copy R7 to R5 (no SBWC3 instruction)
	SBWC	R3,R5		; R5 = R7 - R3 (-C)

	BCS	40$		; R2:R3 was larger than value in R6;R7	
	MOVQ	R4,R6		; move subtraction remainder to R6;R7
	BBSS	R0,@QUOT(AP),40$; set multiplier bit in quotient quadword
40$:	SOBGEQ	R0,30$		; decrement multiplier and try again

	BLBC	R1,50$		; should the result be negative?
	MOVQ	@QUOT(AP),R6;R7	; move the quotient to R6:R7
	MCOML	R6,R6		; compliment R6\
	MCOML	R7,R7		; compliment R7 \ quadword integer
	INCL	R6		; add 1         / two's compliment
	ADWC	#0,R7		; add (C)      /
	MOVQ	R6,@QUOT(AP)
50$:	RET

	.END	DIVDEMO
-------
VAXman-


