Hi Ed,
This is a solution. The routine uses some zero-page ram for Quotient, Divisor and Result (2x 4byte) and some bytes more (3) from stack. It is all noted.
; variables declaration (IntAcc1 & IntAcc2)
Quotient:
Remainder:
IntAcc1: ; 4-bytes integer accum. #1
TEMP1 ds 1
TEMP2 ds 1
Dividend:
TEMP3 ds 1
TEMP4 ds 1
Divisor:
IntAcc2: ; 4-bytes integer accum. #2
TEMP5 ds 1
TEMP6 ds 1
TEMP7 ds 1
TEMP8 ds 1
;**************************************************
; 32 x 16 UNSIGNED DIVIDE
;
; This routine takes the 32-bit dividend stored in
; IntAcc1:IntAcc1+3 and divides it by the 16-bit
; divisor stored in IntAcc2:IntAcc2+1.
; The quotient replaces the dividend and the
; remainder replaces the divisor.
; Time length: 1720us @ 2MHz BusClock with call
;**************************************************
UDVD32 pshh ; save H-reg value
psha ; save accumulator
pshx ; save H-reg value
ais #-3 ; reserve three bytes of
; temp storage
lda #32
sta 3,SP ; loop cntr for nr. shifts
lda Divisor ; get divisor Msb
sta 1,SP ; put divisor Msb in
; working storage
lda Divisor+1 ; get divisor lsb
sta 2,SP ; put divisor lsb in
; working storage
; Shift all four bytes of dividend 16 bits to the
; right and clear both bytes of the temporary
; remainder location
mov Dividend+1,Dividend+3 ; shift divid. lsb
mov Dividend,Dividend+2 ; shift 2nd byte
; of dividend
mov Dividend-1,Dividend+1 ; shift 3rd byte
; of dividend
mov Dividend-2,Dividend ; shift divid. Msb
clr Remainder ; zero remain. Msb
clr Remainder+1 ; zero remain. lsb
; Shift each byte of dividend and remainder one bit
; to the left
ShftLP lda Remainder ;get remainder Msb
rola ;shift remainder Msb into
;carry
rol Dividend+3 ;shift dividend lsb
rol Dividend+2 ;shift 2nd byte of dividend
rol Dividend+1 ;shift 3rd byte of dividend
rol Dividend ;shift dividend Msb
rol Remainder+1 ;shift remainder lsb
rol Remainder ;shift remainder Msb
; Subtract both bytes of the divisor from remainder
lda Remainder+1 ;get remainder lsb
sub 2,SP ;subtract divisor lsb from
;remainder lsb
sta Remainder+1 ;store new remainder lsb
lda Remainder ;get remainder Msb
sbc 1,SP ;subtract divisor Msb from
;remainder msb
sta Remainder ;store new remainder Msb
lda Dividend+3 ;get low byte of
;dividend/quotient
sbc #0 ;dividend low bit holds
;subtract carry
sta Dividend+3 ;store low byte of
;dividend/quotient
; Check dividend/quotient lsb. If clear, set lsb of
; quotient to indicate successful subraction, else
; add both bytes of divisor back to remainder
brclr 0,Dividend+3,SetLsb ;check for a carry
;from subtraction and add
;divisor to remainder if set
lda Remainder+1 ;get remainder lsb
add 2,SP ;add divisor lsb to
;remainder lsb
sta Remainder+1 ;store remainder lsb
lda Remainder ;get remainder Msb
adc 1,SP ;add divisor Msb to
;remainder Msb
sta Remainder ;store remainder msb
lda Dividend+3 ;get low byte of dividend
adc #0 ;add carry to low bit of
;dividend
sta Dividend+3 ;store low byte of dividend
bra Decrmt ;do next shift and subtract
SetLsb bset 0,Dividend+3 ;set lsb of quotient to
;indicate successive
; subtraction
Decrmt dbnz 3,SP,ShftLP ;decrement loop counter and
;do next shift
; Move 32-bit dividend into IntAcc1:IntAcc1+3 and put
; 16-bit remainder in IntAcc2:IntAcc2+1
lda Remainder ;get remainder msb
sta 1,SP ;temporarily store
;remainder msb
lda Remainder+1 ;get remainder lsb
sta 2,SP ;temporarily store rem.r lsb
mov Dividend,Quotient
mov Dividend+1,Quotient+1 ;shift all four
;bytes of quotient
mov Dividend+2,Quotient+2 ;16 bits to the
;left
mov Dividend+3,Quotient+3
lda 1,SP ;get final remainder Msb
sta IntAcc2 ;store final remainder Msb
lda 2,SP ;get final remainder lsb
sta IntAcc2+1 ;store final remainder lsb
; Deallocate local storage, restore register values,
; and return from subroutine
ais #3 ;deallocate temporary storage
pulx ;restore X-reg value
pula ;restore accumulator value
pulh ;restore X-reg value
rts
Sorry the editor on the forum made a mess with the original formatting, but it is a minor problem: copy it on a good word editor with fixed character size (courier new). The routine is fully tested and I use it in my programs in assembler. Anyway the original text is attached in asm format.
Good luck,
Salvatore