Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/06/04 14:03
Read: times


 
#69942 - RE: 16-bit division
Responding to: ???'s previous message
Nico:
Here is the 16/16 divide that I have used on numerous projects. Give it a try. It is most likely very similar to library code at this site.


;====================================================================
; subroutine UDIV16
; 16-Bit / 16-Bit to 16-Bit Quotient & Remainder Unsigned Divide
;
; input:    r1, r0 = Dividend X
;           r3, r2 = Divisor Y
;
; output:   r1, r0 = quotient Q of division Q = X / Y
;           r3, r2 = remainder 
;
; alters:   acc, B, dpl, dph, r4, r5, r6, r7, flags
;====================================================================

UDIV16:
        MOV     R7, #0              ; clear partial remainder
        MOV     R6, #0
        MOV     B, #16              ; set loop count
;
DIV_LOOP:      
        CLR     C                   ; clear carry flag
        MOV     A, R0               ; shift the highest bit of
        RLC     A                   ; the dividend into...
        MOV     R0, A
        MOV     A, R1
        RLC     A
        MOV     R1, A
        MOV     A, R6               ; ... the lowest bit of the
        RLC     A                   ; partial remainder
        MOV     R6, A
        MOV     A, R7
        RLC     A
        MOV     R7, A
        MOV     A, R6               ; trial subtract divisor
        CLR     C                   ; from partial remainder
        SUBB    A, R2
        MOV     DPL, A
        MOV     A, R7
        SUBB    A, R3
        MOV     DPH, A
        CPL     C                   ; complement external borrow
        JNC     DIV_1               ; update partial remainder if
                                    ; borrow
        MOV     R7, DPH             ; update partial remainder
        MOV     R6, DPL
DIV_1:
        MOV     A, R4               ; shift result bit into partial
        RLC     A                   ; quotient
        MOV     R4, A
        MOV     A, R5
        RLC     A
        MOV     R5, A
        DJNZ    B, DIV_LOOP
        MOV     A, R5               ; put quotient in r0, and r1
        MOV     R1, A
        MOV     A, R4
        MOV     R0, A
        MOV     A, R7               ; get remainder, saved before the
        MOV     R3, A               ; last subtraction
        MOV     A, R6
        MOV     R2, A
        RET



Michael Karas


List of 10 messages in thread
TopicAuthorDate
16-bit division            01/01/70 00:00      
   RE: 16-bit division            01/01/70 00:00      
      RE: 16-bit division            01/01/70 00:00      
         RE: 16-bit division            01/01/70 00:00      
            RE: 16-bit division            01/01/70 00:00      
               RE: 16-bit division            01/01/70 00:00      
   RE: 16-bit division            01/01/70 00:00      
   RE: 16-bit division            01/01/70 00:00      
      RE: 16-bit division            01/01/70 00:00      
         RE: 16-bit division            01/01/70 00:00      

Back to Subject List