On Fri, 17 Oct 1997 08:52:19 -0300 Ricardo Seixas
> Here is a 16 bit BIN->BCD.
> I don't know who's the original author but it works fine.
The routine is in a Microchip app note, though it's probably much older
than that. The principle that it uses is to repeatedly multiply the BCD
number by 2 (first by 'adjusting' it, then shifting left 1 bit). After
the shift, the low bit is taken from the binary number. The first bit
shifted into the BCD number is the MSB of the binary number, so it is
multiplied by 2 n times. This isn't a bad way to convert to decimal,
especially for large numbers.
I've rewritten the app note code a little to make it smaller. This code
also converts 24-bit numbers, producing an 8 digit result. (2^^24 =
16,777,216). The code below has been tested. It requires use of the
indirect addressing system (FSR), a 1 byte loop counter (ii), 3 bytes for
the original number (bin..bin+2, LSB first), and 4 bytes for the BCD
result (bcd..bcd+3, LSB first).
; Converts a 24-bit binary number to 8 digits of BCD.
; Derived from AN526.
; Input: Number at bin...bin+2 LSB first
; Output: Packed BCD at bcd...bcd+3 LSB first.
; RAM : ii, FSR, 4 bytes at bcd.
; Input number at bin is destroyed.
movlw d'24' ;Do 24 bits
clrf bcd+0 ;Clear the result
rlf bin+0,f ;Rotate one bit out of left end of
; If you want to return with the original value in bin (rather than
; garbage), copy the C bit into the low bit of bin here.
; (Not tested)
;; bcf bin+0,0 ;Assume C=0,
;; skpnc ;if it is, OK
;; bsf bin+0,0 ;otherwise put 1 in.
rlf bcd+0,f ;and into the low byte of BCD.