In assembly language parlance
, BCD refers to a way of intermediate
ly storing numbers - between input (where the numbers are in ASCII
form) and computation (where the numbers are in their native binary
form, being either big-endian
, depending on CPU architecture
ASCII characters representing numbers are numbered from 48 (0) to 57 (9), so the easiest way to get an ASCII string into BCD format is to simply subtract 48 from the ASCII value of each character and then just deal with it in binary form. (Some architectures, such as x86, have instructions that will allow you to carry out arithmetical operations (+-*/) directly on raw ASCII data without converting it to BCD, but if the program needs to do anything deeper, it is usually better to convert from ASCII to BCD or packed BCD.) BCD is often used in scenarios where rounding would result in an undesirable loss of precision, since a BCD number can be any length, even greater than that of the host CPU's registers.
BCD has two formats - unpacked, in which one decimal number is stored in each byte, and packed, in which two decimal numbers are stored in each byte. Since it only takes four bits (also called a nybble) to store a number between 0 and 9, and a byte contains twice as many bits, space can be saved by storing two decimal numbers in one byte. Many coprocessors prefer their numbers in packed BCD format.
x86 has the following instructions that facilitate ASCII, BCD, and packed BCD arithmetic:
- AAA - ASCII Adjust After Addition (Raw ASCII)
- AAS - ASCII Adjust After Subtraction (Raw ASCII)
- AAM - ASCII Adjust After Multiplication (Packed BCD)
- AAD - ASCII Adjust After Division (Packed BCD)
- DAA - Decimal Adjust After Addition (Packed BCD)
- DAS - Decimal Adjust After Subtraction (Packed BCD)
The first four instructions allow the programmer to add, subtract, multiply, and divide non-binary-format numbers. Since the results of adding/subtracting two ASCII numbers are inaccurate both in binary AND in ASCII, it is necessary to adjust them, and this is what the first four instructions do. (As noted, AAM and AAD do require that the data first be converted to packed BCD.)
DAA and DAS are similar to AAA and AAS, except that they operate on packed-BCD-format data.
(Information partially paraphrased from IBM PC Assembly Language and Programming by Peter Abel, Prentice-Hall 1995.)