Monday, May 30, 2016

8051 Program - divide

; Description:
; Unsigned Divide Of R2/3 By R4/5
;
; Entry Requirements:
; Divisor In R4/5
; Dividend In R2/3
;
; On Exit:
; Quotient In R2/3
; Remainder In R4/5
;
; Affected:
; PSW.CY, PSW.Z, PSW.P, Acc, B, R0, R1, R2, R3, R4, R5
;
; Stack:
; X Bytes, Not Including Space Used By Called Routines
;
; Comments:
; Swiped From A Now Defunct 'C' Compiler
;
DIVIDE:
clr a;
mov b,a ; initialize count
mov r0,a ; zero quotient
mov r1,a;
mov a,r2 ; check for zero dividend
orl a,r3;
jz loop8;
mov a,r4 ; check for zero divisor
orl a,r5;
jnz loop3;
ret;
;
loop1:mov a,r2;
clr c;
subb a,r4 ; is divisor greater than dividend yet
jc loop4 ; yes, go no further
jnz loop2;
mov a,r3;
subb a,r5;
jc loop4;
;
loop2:mov a,r5 ; shift divisor up one bit
clr c;
rlc a;
mov r5,a;
mov a,r4;
rlc a;
mov r4,a;
;
loop3:inc b ; increment count
mov a,r4 ; check for safe to shift some more
jnb acc.7,loop1 ; loop if top bit clear
;
loop4:mov a,r2;
clr c;
subb a,r4 ; is divisor greater than dividend
jc loop5 ;
jnz loop6;
mov a,r3;
subb a,r5;
jnc loop6;
;
loop5clr c;
sjmp loop7;
;
loop6:clr c ; subtract divisor from dividend
mov a,r3;
subb a,r5;
mov r3,a;
mov a,r2;
subb a,r4;
mov r2,a;
setb c ; now set bit for quotient
;
loop7:mov a,r1;
rlc a;
mov r1,a;
mov a,r0;
rlc a;
mov r0,a;
;
mov a,r4 ; shift divisor down
clr c;
rrc a;
mov r4,a;
mov a,r5;
rrc a;
mov r5,a;
djnz b,loop4 ; and continue with the rest
;
loop8:mov 5,r3;
mov 4,r2;
mov 2,r0;
mov 3,r1;
ret;


Source: Assorted Utilities, John C. Wren 11/23/96



Related topics:
8051 Program - count ascii decimal in string   |   8051 Program - unsigned divide   |   8051 Program - unsigned mod   |   8051 Program - signed divide   |   8051 Program - signed mod   |   8051 Program - sign fixup for divide   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment