*** Welcome to piglix ***

Intel BCD opcode


The Intel BCD opcodes are a set of x86 instructions that operates with BCD 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.

BCD instructions are no longer supported in long mode.

BCD numbers can be represented in two ways: packed decimal and unpacked decimal.

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.

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.

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.

Only unpacked representation is supported. Operands must fall in the range 0 to 99.


...
Wikipedia

...