Intel BCD opcodes
Encyclopedia
The Intel BCD opcodes are a set of x86 instructions that operates with BCD numbers.

The radix
Radix
In mathematical numeral systems, the base or radix for the simplest case is the number of unique digits, including zero, that a positional numeral system uses to represent numbers. For example, for the decimal system the radix is ten, because it uses the ten digits from 0 through 9.In any numeral...

 used for the representation of numbers in the x86 processor
Central processing unit
The central processing unit is the portion of a computer system that carries out the instructions of a computer program, to perform the basic arithmetical, logical, and input/output operations of the system. The CPU plays a role somewhat analogous to the brain in the computer. The term has been in...

s is 2.
This is called a binary numeral system
Binary numeral system
The binary numeral system, or base-2 number system, represents numeric values using two symbols, 0 and 1. More specifically, the usual base-2 system is a positional notation with a radix of 2...

.
However the x86 processors do have limited support for the decimal numeral system
Decimal
The decimal numeral system has ten as its base. It is the numerical base most widely used by modern civilizations....

.

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
Nibble
In computing, a nibble is a four-bit aggregation, or half an octet...

.
The values 10 to 15 are not used.
  • Unpacked (8 bits)
In unpacked decimal representation a decimal digit is stored in one byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...

.
The values 10 to 255 are not used.

Adding

Only the decimal numbers 0 to 99 can be add
Addition
Addition is a mathematical operation that represents combining collections of objects together into a larger collection. It is signified by the plus sign . For example, in the picture on the right, there are 3 + 2 apples—meaning three apples and two other apples—which is the same as five apples....

ed directly.

First the numbers are added as usual using add (or adc if you need the carry flag
Carry flag
In computer processors the carry flag is a single bit in a system status register used to indicate when an arithmetic carry or borrow has been generated out of the most significant ALU bit position...

).

Then the result is adjusted, depending on the number representation.
  • Packed
The processor will have 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.
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.
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.
  • 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 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.

Subtraction

Only the decimal numbers 0 to 99 can be subtract
Subtraction
In arithmetic, subtraction is one of the four basic binary operations; it is the inverse of addition, meaning that if we start with any number and add any number and then subtract the same number we added, we return to the number we started with...

ed directly.

First the numbers are subtract
Subtraction
In arithmetic, subtraction is one of the four basic binary operations; it is the inverse of addition, meaning that if we start with any number and add any number and then subtract the same number we added, we return to the number we started with...

ed as usual using sub (or sbb if you need the carry flag).
  • Packed
The processor will have 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.
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.
  • 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.

Multiplication

Only unpacked representation is supported. Only two single digit numbers can be multipli
Multiplication
Multiplication is the mathematical operation of scaling one number by another. It is one of the four basic operations in elementary arithmetic ....

ed.

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
Quotient
In mathematics, a quotient is the result of division. For example, when dividing 6 by 3, the quotient is 2, while 6 is called the dividend, and 3 the divisor. The quotient further is expressed as the number of times the divisor divides into the dividend e.g. The quotient of 6 and 2 is also 3.A...

 (just the integr
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...

al part) in the most significant byte of the result and the remainder
Remainder
In arithmetic, the remainder is the amount "left over" after the division of two integers which cannot be expressed with an integer quotient....

 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.

Then the quotient and remainder of the division
Division (mathematics)
right|thumb|200px|20 \div 4=5In mathematics, especially in elementary arithmetic, division is an arithmetic operation.Specifically, if c times b equals a, written:c \times b = a\,...

 are obtained as usual using div.

The quotient and remainder will be in normal binary representation.

History

Binary-coded decimal
Binary-coded decimal
In computing and electronic systems, binary-coded decimal is a digital encoding method for numbers using decimal notation, with each decimal digit represented by its own binary sequence. In BCD, a numeral is usually represented by four bits which, in general, represent the decimal range 0 through 9...

 (BCD) numbers were in the past used for storing decimal numbers, especially in financial software.

The opcode
Opcode
In computer science engineering, an opcode is the portion of a machine language instruction that specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor in question...

s 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.

All integer calculations are exact, so the radix of the number representation is not important for accuracy. Therefore even financial software today usually stores values in binary representation and only converts to decimal for input and output.

On an x86 processor calculations with binary numbers are usually a lot faster than the same calculations with BCD numbers.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK