Intel BCD opcodes

Intel BCD opcodes

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.

Usage

Number representation

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.
*Unpacked (8 bits):In unpacked decimal representation a decimal digit is stored in one byte.:The values 10 to 255 are not used.

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

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 subtracted directly.

First the numbers are added 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 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.

Then the quotient and remainder of the division are obtained as usual using div.

The quotient and remainder will be in normal binary representation.

History

Binary-coded decimal (BCD) numbers were in the past 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.

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.For instance adding two binary numbers with the binary equivalent of nine decimal digits can be done with just one instruction, add.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Intel 8087 — Intel C8087 Architecture du 8087 Les Intel 8087 furent les …   Wikipédia en Français

  • Intel 8087 — The 8087 was the first math coprocessor for 16 bit processors designed by Intel (the I8231 was older but designed for the 8 bit Intel 8080); it was built to be paired with the Intel 8088 and 8086 microprocessors. The purpose of the 8087, the… …   Wikipedia

  • Intel 8087 — Coprocesador numérico Intel 8087. Microarquitectur …   Wikipedia Español

  • List of Intel microprocessors — For a list of Intel s microcontrollers, see List of common microcontrollers#Intel. This generational and chronological list of Intel microprocessors attempts to present all of Intel s processors from the pioneering 4 bit 4004 (1971) to the… …   Wikipedia

  • AAS — is a three letter acronym that may refer to:In science and mathematics: * Angle Angle Side, a method for showing congruency between triangles. * ASCII Adjust after Subtraction, an Intel 80x86 processor instruction; see Intel BCD opcodes * Atomic… …   Wikipedia

  • AAA — or Triple A may just mean something that is high quality, premier, or excellent, or be a three letter acronym. As a proper word Aaa, in the English language it is only found in the name of the Aaa Water Treader ( Cavaticovelia aaa ), a true bug… …   Wikipedia

  • AAM — is a three letter acronym that may refer to: * Air to air missile, a guided missile fired from an aircraft for the purpose of destroying another aircraft * American Association of Museums, an organization for museum professionals * American Axle… …   Wikipedia

  • Microprocessor — Intel 4004, the first general purpose, commercial microprocessor A microprocessor incorporates the functions of a computer s central processing unit (CPU) on a single integrated circuit,[1] (IC) or at most a few integrated circuits …   Wikipedia

  • MOS Technology 6502 — The MOS Technology 6502 is an 8 bit microprocessor that was designed by Chuck Peddle for MOS Technology in 1975. When it was introduced, it was the least expensive full featured CPU on the market by a considerable margin, costing less than one… …   Wikipedia

  • Zilog Z80 — One of the first Z80 microprocessors manufactured; the date stamp is from June 1976. Produced 1976 Common manufacturer(s) Zilog …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”