Thursday, May 26, 2016

8051 Program - unsigned 16bit division

; 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


Source: Maths Subroutines for the 8051 microcontroller, W.G.Marshall 2002



Related topics:
8051 Program - signed 8bit division   |   8051 Program - unsigned 8bit division   |   8051 Program - signed 16bit division   |   8051 Program - signed 32bit division   |   8051 Program - unsigned 32bit division   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment