home *** CD-ROM | disk | FTP | other *** search
-
- released to Compuserve (and public domain) 1-19-93
- by Scott Mitchell Jennings (author)
-
-
- This is a simple(?) sixteen bit divide routine for the 8048 and 8051
- class of microcontrollers. Note that Multiplication and Division are
- not true compliments of one another the way Addition and Subtraction
- are. For example, division by zero is invalid; but there is no
- corresponding condition in Multiplication. Hence, a sixteen bit
- multiply routine can make extensive use of an eight bit multiply
- instruction, whereas a sixteen bit divide routine cannot make use of
- an eight bit divide instruction.
-
-
- For speed, all intermediate data are kept in the "R" registers, and
- no use of addressable RAM or STACK space is needed. All the
- registers (of one bank) are used.
-
- - Scott Mitchell Jennings /kd6gou
- PO Box 107
- Penn Valley, CA 95946
- 916-477-6449
-
-
-
- ;
- ; On Entry:
- ; R0:R1 contains the divisor (low byte in R0)
- ; R2:R3 contains the dividend (low byte in R2)
- ;
- ;
- ; On Exit:
- ; R4:R5 contains the quotient (low byte in R4)
- ; R6:R7 contains the remainder (low byte in R6)
- ;
-
-
-
-
- MOV A, R1 ;*************
- CPL A ;** **
- MOV DPH, A ;** negate **
- MOV A, R0 ;** the ** you'll need to change
- CPL A ;** divisor ** only this code here
- MOV DPL, A ;** ** to adapt to the 8048
- ; ;** in ** class of uPs
- INC DPTR ;** R0:R1 **
- MOV R0, DPL ;** **
- MOV R1, DPH ;*************
-
-
- CLR A
- MOV R4, A
- MOV R5, A ;R4:R5=quotient
- MOV R6, A
- MOV R7, A ;R6:R7=remainder
-
- INC R4 ;quotent=1 (done when shifted into CY)
-
- DIVIDE_LOOP: XCH A, R2 ;****************************
- RLC A ;** **
- XCH A, R2 ;** **
- XCH A, R3 ;** **
- RLC A ;** shift one bit **
- XCH A, R3 ;** **
- ; ;** from dividend (R2:R3) **
- XCH A, R6 ;** **
- RLC A ;** into remainder (R6:R7) **
- XCH A, R6 ;** **
- XCH A, R7 ;** **
- RLC A ;** **
- XCH A, R7 ;****************************
-
- MOV A, R6
- ADD A, R0
-
- MOV A, R7
- ADDC A, R1
- JNC DIV_ZERO ;branch if borrowed
-
-
- MOV A, R6
- ADD A, R0
- MOV R6, A
-
- MOV A, R7
- ADDC A, R1
- MOV R7, A
-
- DIV_ZERO: XCH A, R4
- RLC A
- XCH A, R4
- XCH A, R5
- RLC A
- XCH A, R5
- JNC DIVIDE_LOOP
-
-
- ;done.
-
-