BCD conversion routines.

Summary
BCD conversion routines.
Functions
binbcd8Convert an 8 bit binary value to BCD.
binbcd16Convert an 16 bit binary value to BCD

Functions

binbcd8

Convert an 8 bit binary value to BCD.

Description

This function converts an 8 bit binary value into a 16 bit BCD.  It works by transferring one bit a time from the source and adding it into a BCD value that is being doubled on each iteration.  As all the arithmetic is being done in BCD the result is a binary to decimal conversion.

All conversions take 311 clock cycles.

For example the conversion of a $96 would look like this :

BIN = $96 -> BIN' = $2C C = 1 | BCD $0000 x2 + C -> BCD' $0001
BIN = $2C -> BIN' = $58 C = 0 | BCD $0001 x2 + C -> BCD' $0002
BIN = $58 -> BIN' = $B0 C = 0 | BCD $0002 x2 + C -> BCD' $0004
BIN = $B0 -> BIN' = $60 C = 1 | BCD $0004 x2 + C -> BCD' $0009
BIN = $60 -> BIN' = $C0 C = 0 | BCD $0009 x2 + C -> BCD' $0018
BIN = $C0 -> BIN' = $80 C = 1 | BCD $0018 x2 + C -> BCD' $0037
BIN = $80 -> BIN' = $00 C = 1 | BCD $0037 x2 + C -> BCD' $0075
BIN = $00 -> BIN' = $00 C = 0 | BCD $0075 x2 + C -> BCD' $0150

This technique is very similar to Garth Wilsons, but does away with the look up table for powers of two and much simpler than the approach used by Lance Leventhal in his books (e.g. subtracting out 1000s, 100s, 10s and 1s).

Andrew Jacobs, 28-Feb-2004

Parameters

ANumber.

Return

_axContains the bcd encoded number.

binbcd16

Convert an 16 bit binary value to BCD

Description

This function converts a 16 bit binary value into a 24 bit BCD.  It works by transferring one bit a time from the source and adding it into a BCD value that is being doubled on each iteration.  As all the arithmetic is being done in BCD the result is a binary to decimal conversion.  All conversions take 915 clock cycles.

See binbcd8 for more details of its operation.

Andrew Jacobs, 28-Feb-2004

Parameters

AWord MSB
XWord LSB

Return

_axContains the bcd encoded number (3 bytes).
Close