
Intel BCD opcode

The Intel BCD opcodes are a set of six x86 instructions that operate with binary-coded decimal numbers. The radix used for the representation of numbers in the x86 processors is 2. This is called a binary numeral system. However, the x86 processors do have limited support for the decimal numeral system.
In addition, the x87 part supports a unique 18-digit (ten-byte) BCD format that can be loaded into and stored from the floating point registers, from where ordinary FP computations can be performed.
The integer BCD instructions are no longer supported in long mode.
Usage.
Number representation.
BCD numbers can be represented in two ways in integer registers: packed decimal and unpacked decimal.
The values are generally assumed to be stored in the lower 8-bits of a register, e.g. AL.
Adding.
Only the decimal numbers 0 to 99 can be added directly.
First the numbers are added as usual using add (or adc if you need the carry flag). The processor will set the adjust flag if the sum of both lower nibbles is 16 or higher, and the carry flag if the sum of both bytes is 256 or higher.
Then the result is adjusted, depending on the number representation.
Subtraction.
Only the decimal numbers 0 to 99 can be subtracted directly.
First the numbers are subtracted as usual using sub (or sbb if you need the carry flag). The processor will set the adjust flag if a borrow occurred in the least significant nibble, and the carry flag if a borrow occurred in the most significant nibble.
Multiplication.
Only unpacked representation is supported. Only two single digit numbers can be multiplied.
First the digits are multiplied as usual using mul.
Then the result is adjusted using aam (ASCII adjust for multiplication): The processor divides the result by ten, storing the quotient (just the integral part) in the most significant byte of the result and the remainder in the least significant byte of the result.
Division.
Only unpacked representation is supported.
Operands must fall in the range 0 to 99.
First the operands are converted to normal binary representation using aad (ASCII adjust before division): The processor converts numbers by multiplying the most significant byte by 10 and adding the least significant byte. The quotient and remainder of the division are obtained as usual using div, and will be present in normal binary representation.
In x87.
The x87 coprocessor has BCD support in the form of a pair of load (FBLD) and store-and-pop (FBSTP) instructions. The former loads a 80-bit BCD integer into the FPU, while the latter writes a FPU value as a 80-bit integer value into the memory. Inside of the FPU, the values are stored as normal x87 extended-precision floats. Unlike the integer-facing versions, the two instructions remain available in long mode.
The 80-bit format is divided into the following:
There is a special "indefinite" value encoded as FFFFC000000000000000h.
Application.
Binary-coded decimal (BCD) numbers are used for storing decimal numbers, especially in financial software.
The opcodes mentioned above give the x86 rudimentary BCD support.
Alternatives.
Adding BCD numbers using these opcodes is a complex task, and requires many instructions to add even modest numbers. It can also require a large amount of memory. If only doing integer calculations, then all integer calculations are exact, so the radix of the number representation is not important for accuracy. On an x86 processor, calculations with binary numbers are usually a lot faster than the same calculations with BCD numbers.