; void         _DivUnsArrByUnsRound(a, c, n)
 ;
 ; ARGUMENT
 ;      unsigned        a[]     n-word number to divide
 ;      unsigned        c       number of divide by
 ;      int             n       number of digits
 ;
 ; DESCRIPTION
 ;      Divides n-digit number c by b, storing result in c.
 ;  The quotient is rounded to the nearest integer.
 ;
 ; SIDE EFFECTS
 ;      None.
 ;
 ; RETURNS
 ;      None.
 ;
 ; AUTHOR
 ;  Jared Levy  7/9/89
 ;   Copyright (C) 1989-90 Greenleaf Software Inc.  All Rights Reserved.
 ;
 ; MODIFICATIONS
 ;
 ;
 ;
        include model.h
        include prologue.h
        include gm.equ

        pseg    dmby1r
        cproc   _DivUnsArrByUnsRound,,_dmby1r

; load parameters
if      _LDATA
        push es
        les     di, parm1_      ; get pointer to source & destination
        mov     cx, parm3_      ; get number to divide by
        mov     bx, parm4_      ; get number of digits
else
        mov     di, parm1_      ; get pointer to source & destination
        mov     ax, ds
        mov     es, ax
        mov     cx, parm2_      ; get number to divide by
        mov     bx, parm3_      ; get number of digits
endif

        call    near ptr dmby1ra
if      _LDATA
        pop     es
endif
        cproce

dmby1ra:
        dec     bx              ; digits go from 0 to n-1
        sal     bx, 1           ; want bytes rather than words

;/* skip initial zeroes */
;       for(;(q>0)&&(c[q-1]==0);q--);
;
;       if (q == 0)
;               return(0);
;       q--;                    /* q will be used for subscript */
zeroloop:
        mov     ax, word ptr es:[di+bx] ; get digit
        cmp     ax, 0                   ; is digit zero?
        jne     nonzero                 ; if non-zero, begin divide
        dec     bx                      ; next word
        dec     bx
        cmp     bx, 0                   ; last digit?
        jge     zeroloop                ; if not, process next digit
;       xor     ax, ax                  ; otherwise, remainder 0
        jmp     short exit1             ;  && division through (0/c=0)

;       bl = (unsigned long) b;
;       while (q>=0)  {
;               k = (unsigned long *) &r[q];
;               cl = *k / bl;
;               c[q] = (unsigned) cl;
;               r[q] = (unsigned) (*k - cl * bl);
;               q--;
;               }
nonzero:
        xor     dx,dx                   ; high word zero in first divide
divloop:
        div     cx                      ; divide digit
        mov     word ptr es:[di+bx], ax ; store quotient digit
        cmp     bx, 0                   ; last digit?
        je      exit2                   ; if so, division complete
        dec     bx                      ; next digit
        dec     bx
        mov     ax, word ptr es:[di+bx] ; get digit
        jmp     short divloop           ; process digit

; round up if remainder > (divisor-1)/2
exit2:
        dec     cx                      ; set cx = (cx-1)/2
        sar     cx, 1
        cmp     dx, cx                  ; test for rounding up
        ja      inclp                   ; if remainder is large, round up
        ret
inclp:
        inc     word ptr es:[di]
        jnz     exit1
        inc     di
        inc     di
        jmp     short inclp

exit1:
        ret

        endps
        end