4 bit binary coded decimal

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
Mats
Posts: 111
Joined: 24 Aug 2003

4 bit binary coded decimal

Post by Mats »

I think that almost every program starts with CLD! Why use binary coded decimal representation? Possibly for applications where numbers are typed in in decimal form with a key board , some arithmetics is carried out and the result then sent in decimal form to a display. In this case the operations to go to BCD representation might be a bit shorter then to a "packed binary" representation. Anybody knows a "reasonable" application?

Well I once had a little application for BCD! Someone sent the fax home:

send
+ more
_________
money

asking his parents to send some money. Replacing the letters with the right decimal digest the arithmetic fits! This can certainly be solved by hand with a little analysis but also with "brute force" using a 6502 checking all possibilities. I once wrote a small rather elegant program using arithmetics in BCD mode of 16 bits and 32 bit words to solve this problem.

The idea was (I think) to see 0sndmore as a 4 byte number, to increase this number with 1 each time and to check the arithmetics.

Maybe somebody would be amused (like me) to write such a program. At least once to operate in BCD mode!
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post by GARTHWILSON »

The CMOS 65c02 has the decimal flag already clear when it comes out of reset or responds to an interrupt, so the CLD is not necessary there.

As for what the decimal mode is good for, I'll quote a little out of WDC's excellent programming manual:

"In certain applications such as numeric I/O programming, where conversion between ASCII and binary representation of decimal strings is inconvenient, and business applications, in which conversion of binary fractions to decimal fractions results in approximation errors, it is convenient to represent numbers in decimal form and, if possible, perform arithmetic operations on them directly in this form."
Sprow
Posts: 12
Joined: 23 Oct 2003
Contact:

Re: 4 bit binary coded decimal

Post by Sprow »

> I think that almost every program starts with CLD! Why use binary
> coded decimal representation?

The two most recent applications of BCD I have had were
a) a clock chip whose registers were BCD anyway
b) decoding the modified Julian data passed in the Television Service Data Packet as part of UK teletext broadcasts as a means of keeping an accurate clock which automatically corrected for BST/GMT

Sprow.
bogax
Posts: 250
Joined: 18 Nov 2003

Post by bogax »

"Anybody knows a "reasonable" application?"

Double-dabble binary to BCD conversion.
kc5tja
Posts: 1706
Joined: 04 Jan 2003

Re: 4 bit binary coded decimal

Post by kc5tja »

Mats wrote:
Anybody knows a "reasonable" application?
For when 32-bits isn't enough to hold your integers or floating point values, you can get "big-num" support in some programming languages like Lisp or J. These big-nums are specially formed in memory to support arbitrary precision math. This means, in no uncertain terms, if you have 1.000....0001, with a hundred zeros in between the fractional 1 and the decimal point, you need that many bits of representation to do it in.

The problem is, of course, that doing this in binary is nonsensical. How does one accurately represent 0.3, or 0.1 for that matter, in binary? It can't be done. You can approximate, and many computer floating point implementations take care of this for you, but (1.1 + 0.1) - 1.2 will, in a fair number of naive floating point implementations, NOT give zero as the answer (it'll be zero to a large number of bits, but over the whole floating point value, it won't be precisely zero). For small numbers of bits, say 32 or 64, this isn't so much a big deal. But when you're dealing with astronomically large or small numbers, say in the field of astronomy, weather, and medicine especially, these accumulated round-off errors can mean life or death to people.

BCD eliminates any chance of this happening.

--
Samuel A. Falvo II
Post Reply