Intel BCD opcode

From HandWiki
Jump to: navigation, search

The Intel BCD opcodes are a set of 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.

BCD instructions are no longer supported in long mode.

Usage

Number representation

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

  • Packed (4 bits)
In packed decimal representation a decimal digit is stored in one nibble.
The values 10 to 15 are not used.[1]
  • Unpacked (8 bits)
In unpacked decimal representation a decimal digit is stored in one byte.
The values 10 to 255 are not used.
The upper nibble is ignored, and can either be zero, or the leading-nibble for the ASCII character (value 3).[1]

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.

  • Packed
The result is adjusted using daa (decimal adjust after addition): If the least significant nibble of the result is 10 or higher, or if the adjust flag is set, then the processor adds 6 to the result and discards any overflow of the nibble.
Then, if the most significant nibble of the result is 10 or higher, or if the carry flag is set, then the processor adds 96 (6 times 16) to the result and sets the carry flag.[1][2]
  • Unpacked
The result is adjusted using aaa (ASCII adjust after addition): If the least significant nibble of the result is 10 or higher, then the processor adds 6 to it and discards any overflow of the nibble, and stores it in the least significant byte.
The most significant byte is incremented.
Note that at this point the most significant byte may not contain a valid decimal number.[1][2]

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.

  • Packed
The result is adjusted using das (decimal adjust after subtraction): If the least significant nibble of the result is 10 or higher, or if the adjust flag is set, then the processor subtracts 6 from the result.
Then, if the most significant nibble of the result is 10 or higher, or if the carry flag is set, then the processor subtracts 96 (6 times 16) from the result and sets the carry flag.[1][2]
  • Unpacked
The result is adjusted using aas (ASCII adjust after subtraction): If the least significant nibble of the result is 10 or higher, then the processor subtracts 6 from it and stores it in the least significant byte.
The most significant byte is decremented.
Note that at this point the most significant byte may not contain a valid decimal number.[1][2]

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.[1][2]

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.[1][2]

Application

Binary-coded decimal (BCD) numbers are used for storing decimal numbers, especially in financial software.[1]

The opcodes mentioned above give the x86 rudimentary BCD support.[1]

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.[1] 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.[1]

See also

References

  1. 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 Randall Hyde (September 2003). "Decimal Arithmetic". The Art of Assembly Language Programming. No Starch Press. Archived from the original on 2008-11-02. https://web.archive.org/web/20081102170717/http://webster.cs.ucr.edu/AoA/Windows/HTML/AdvancedArithmetica6.html#1000255. Retrieved 2008-10-18. 
  2. 2.0 2.1 2.2 2.3 2.4 2.5 "Volume 2A: Instruction Set Reference, A-M" (PDF). Intel 64 and IA-32 Architectures Software Developer’s Manual. Intel. 2007-05-17. Archived from the original on 2008-03-15. https://web.archive.org/web/20080315112124/http://download.intel.com/design/processor/manuals/253666.pdf. Retrieved 2007-06-27.