PIC16C5X / PIC16CXXX Math Utility Routines
Author: Amar Palacherla Microchip Technology Inc.
TABLE 1: PERFORMANCE SPECS
Spec Speed Efﬁcient Code Efﬁcient Program Memory 35 16 Instruction Cycles 37 71
PLEASE NOTE: This application note uses the old Microchip Math Routine format. It is intended for reference purposes only and is being provided for those of you still implementingBinary Coded Decimal(BCD) routines. For any new designs, please refer to application notes contained in Microchip’s Embedded Control Handbook Volume II - Math Library.
FIGURE 1: Flowchart for Unsigned 8x8 Multiply
This application note provides some utility math routines for Microchip’s PIC16C5X and PIC16CXXX series of 8-bit microcontrollers. The following mathoutlines are provided: • • • • • • • • • • 8x8 unsigned multiply 16x16 double precision multiply Fixed Point Division (Table 3) 16x16 double precision addition 16x16 double precision subtraction BCD (Binary Coded Decimal) to binary conversion routines Binary to BCD conversion routines BCD addition BCD subtraction Square root
Count = 8
H_Byte = L_Byte = 0
W ← Multiplicand Clear Carry BitRotate Right Multiplier Thru Carry
Carry = 1? H_Byte = H_Byte + W
These are written in native assembly language and the listing ﬁles are provided. They are also available on a disk (MS-DOS®). All the routines provided can be called as subroutines. Most of the routines have two different versions: one optimized for speed and the other optimized for code size. The calling sequence of each routineis explained at the beginning of each listing ﬁle.
Rotate Right H_Byte
Rotate Right L_Byte
SINGLE PRECISION UNSIGNED MULTIPLICATION (8x8)
This routine computes the product of two 8-bit unsigned numbers and produces a 16-bit result. Two routines are provided: one routine is optimized for speed (by writing a straight line code) and the other routine has been written to reduce the code size(a looped code). The listing of these routines are given in Appendices A and B. The performance specs for the routines are shown in Table 1. MS-DOS is a registered trademark of Microsoft Corporation.
© 1997 Microchip Technology Inc.
Count = Count - 1
Carry = 0?
DOUBLE PRECISION MULTIPLY
This routine computes the product of two 16-bit numbers andproduces a 32-bit result. Both signed and unsigned arithmetic are supported. Two routines are provided: one routine is optimized for speed (by writing a straight line code) the other routine has been written to reduce code size (a looped code). The listing of these routines are given in Appendices C and D. The performance specs for routines are shown in Table 2. of the dividend, the divisor issubtracted and the corresponding quotient bit as well as the next add or subtract operation is determined by the carry bit . Unfortunately, no simple method exists for performing two’s complement binary division, thereby requiring negate operations during a preprocessing phase. It is important to note that with the dividend initially loaded into the accumulator, an overﬂow of the ﬁnal quotient willresult if the high half of the dividend is greater than or equal to the divisor , indicating that the n-bit range of the quotient will be exceeded. Because of the inherent byte structure of the PICmicro™ family of microcontrollers, a more creative and efﬁcient implementation of the above algorithms is possible. In what follows, partial remainder is initialized at zero and is separated from thedividend, thereby avoiding any alignment logic overhead and yielding a quotient with the same number of bits as the dividend and a remainder with the same number as the divisor. Furthermore, routines are named in format FXDxxyyz, where xx is the number of bits in the dividend, yy is the number of bits in the divisor, and z indicates a signed or unsigned routine. Macros are used for core sections...