Intel BCD Opcode
   HOME

TheInfoList



OR:

The
Intel Intel Corporation is an American multinational corporation and technology company headquartered in Santa Clara, California. It is the world's largest semiconductor chip manufacturer by revenue, and is one of the developers of the x86 seri ...
BCD opcodes are a set of six
x86 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel based on the Intel 8086 microprocessor and its 8088 variant. The 8086 was introd ...
instructions that operate with
binary-coded decimal In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used for ...
numbers. The
radix In a positional numeral system, the radix or base is the number of unique digits, including the digit zero, used to represent numbers. For example, for the decimal/denary system (the most common system in use today) the radix (base number) is t ...
used for the representation of numbers in the
x86 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel based on the Intel 8086 microprocessor and its 8088 variant. The 8086 was introd ...
processor Processor may refer to: Computing Hardware * Processor (computing) **Central processing unit (CPU), the hardware within a computer that executes a program *** Microprocessor, a central processing unit contained on a single integrated circuit (I ...
s is 2. This is called a
binary numeral system A binary number is a number expressed in the base-2 numeral system or binary numeral system, a method of mathematical expression which uses only two symbols: typically "0" (zero) and "1" ( one). The base-2 numeral system is a positional notatio ...
. However, the x86 processors do have limited support for the
decimal numeral system The decimal numeral system (also called the base-ten positional numeral system and denary or decanary) is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers of the Hindu–Arabic numeral ...
. In addition, the
x87 x87 is a floating-point-related subset of the x86 architecture instruction set. It originated as an extension of the 8086 instruction set in the form of optional floating-point coprocessors that worked in tandem with corresponding x86 CPUs. These ...
part supports a unique 18-digit (ten-byte) BCD format that can be loaded into and stored from the
floating point In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be ...
registers, from where ordinary FP computations can be performed. The integer BCD instructions are no longer supported in
long mode In the x86-64 computer architecture, long mode is the mode where a 64-bit operating system can access 64-bit instructions and registers. 64-bit programs are run in a sub-mode called 64-bit mode, while 32-bit programs and 16-bit protected mode pr ...
.


Usage


Number representation

BCD numbers can be represented in two ways in integer registers: packed decimal and unpacked decimal. * Packed (4 bits) ** In packed decimal representation a
decimal digit A numerical digit (often shortened to just digit) is a single symbol used alone (such as "2") or in combinations (such as "25"), to represent numbers in a positional numeral system. The name "digit" comes from the fact that the ten digits (Latin ...
is stored in one
nibble In computing, a nibble (occasionally nybble, nyble, or nybl to match the spelling of byte) is a four-bit aggregation, or half an octet. It is also known as half-byte or tetrade. In a networking or telecommunication context, the nibble is oft ...
. ** The values 10 to 15 are not used. * Unpacked (8 bits) ** In unpacked decimal representation a decimal digit is stored in one
byte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit ...
. ** 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). BCD numbers are generally assumed to be stored in the lowest byte of a register, e.g. AL; operations on unpacked BCD numbers expect the least significant digit in the lowest byte of a register, e.g. AL, and the most significant digit in the second lowest byte, e.g. AH.


Adding

Only the decimal numbers 0 to 99 can be
add Addition (usually signified by the plus symbol ) is one of the four basic operations of arithmetic, the other three being subtraction, multiplication and division. The addition of two whole numbers results in the total amount or '' sum'' of ...
ed directly. First the numbers are added as usual using add (or adc if you need the
carry flag In computer processors the carry flag (usually indicated as the C flag) is a single bit in a system status register/flag register used to indicate when an arithmetic carry or borrow has been generated out of the most significant arithmetic logic ...
). 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. * 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.


Subtraction

Only the decimal numbers 0 to 99 can be
subtract Subtraction is an arithmetic operation that represents the operation of removing objects from a collection. Subtraction is signified by the minus sign, . For example, in the adjacent picture, there are peaches—meaning 5 peaches with 2 taken ...
ed directly. First the numbers are
subtract Subtraction is an arithmetic operation that represents the operation of removing objects from a collection. Subtraction is signified by the minus sign, . For example, in the adjacent picture, there are peaches—meaning 5 peaches with 2 taken ...
ed 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. * 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 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 In arithmetic, a quotient (from lat, quotiens 'how many times', pronounced ) is a quantity produced by the division of two numbers. The quotient has widespread use throughout mathematics, and is commonly referred to as the integer part of a ...
(just the integral part) in the most significant byte of the result and the
remainder In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division). In algebr ...
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 Division or divider may refer to: Mathematics *Division (mathematics), the inverse of multiplication *Division algorithm, a method for computing the result of mathematical division Military *Division (military), a formation typically consisting ...
are obtained as usual using div, and will be present in normal binary representation.


In x87

The
x87 x87 is a floating-point-related subset of the x86 architecture instruction set. It originated as an extension of the 8086 instruction set in the form of optional floating-point coprocessors that worked in tandem with corresponding x86 CPUs. These ...
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 In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used for ...
(BCD) numbers are used for storing decimal numbers, especially in financial software. The
opcode In computing, an opcode (abbreviated from operation code, also known as instruction machine code, instruction code, instruction syllable, instruction parcel or opstring) is the portion of a machine language instruction that specifies the operat ...
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. 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.


See also

*
Bit Manipulation Instruction Sets Bit manipulation instructions sets (BMI sets) are extensions to the x86 instruction set architecture for microprocessors from Intel and AMD. The purpose of these instruction sets is to improve the speed of bit manipulation. All the instructions ...


References

{{DEFAULTSORT:Intel Bcd Opcode X86 instructions