Thursday, May 26, 2016

8051 Program - unsigned 32bit division

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

UDIV32:push 08 ;Save Register Bank 1
push 09
push 0AH
push 0BH
push 0CH
push 0DH
push 0EH
push 0FH
push dpl
push dph
push B
setb RS0 ;Select Register Bank 1
mov r7, #0 ;clear partial remainder
mov r6, #0
mov r5, #0
mov r4, #0
mov B, #32 ;set loop count
div_lp32:clr RS0 ;Select Register Bank 0
clr C ;clear carry flag
mov a, r0 ;shift the highest bit of the
rlc a ;dividend into...
mov r0, a
mov a, r1
rlc a
mov r1, a
mov a, r2
rlc a
mov r2, a
mov a, r3
rlc a
mov r3, a
setb RS0 ;Select Register Bank 1
mov a, r4 ;... the lowest bit of the
rlc a ;partial remainder
mov r4, a
mov a, r5
rlc a
mov r5, a
mov a, r6
rlc a
mov r6, a
mov a, r7
rlc a
mov r7, a
mov a, r4 ;trial subtract divisor from
clr C ;partial remainder
subb a, 04
mov dpl, a
mov a, r5
subb a, 05
mov dph, a
mov a, r6
subb a, #0
mov 06, a
mov a, r7
subb a, #0
mov 07, a
cpl C ;complement external borrow
jnc div_321 ;update partial remainder if
;borrow
mov r7, 07 ;update partial remainder
mov r6, 06
mov r5, dph
mov r4, dpl
div_321:mov a, r0 ;shift result bit into partial
rlc a ;quotient
mov r0, a
mov a, r1
rlc a
mov r1, a
mov a, r2
rlc a
mov r2, a
mov a, r3
rlc a
mov r3, a
djnz B, div_lp32
mov 07, r7 ;put remainder, saved before the
mov 06, r6 ;last subtraction, in bank 0
mov 05, r5
mov 04, r4
mov 03, r3 ;put quotient in bank 0
mov 02, r2
mov 01, r1
mov 00, r0
clr RS0
pop B
pop dph
pop dpl
pop 0FH ;Retrieve Register Bank 1
pop 0EH
pop 0DH
pop 0CH
pop 0BH
pop 0AH
pop 09
pop 08
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 - unsigned 16bit division   |   8051 Program - signed 32bit division   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment