The decimal adjust instruction is used to obtain correct results of decimal addition or subtraction. When adding (or subtracting) decimal values, first calculate them by binary addition (subtraction). Next, adjust the result with the decimal adjust instruction to obtain the correct decimal result.
For example, when calculating 19 + 17, the addition itself is processed as a binary addition. The addition result is 30. This number itself is not a strange number in the decimal system, but in the case of this addition, carry from the lower 4 bits occurs. In the decimal system, carry is done at 10, but in this case, carry occurs at 16. By performing decimal adjustment here, 6 is added to the lower 4 bits and the result, 36, is the correct decimal addition result.
Decimal adjustment is thus performed for adjusting the value and a carry/borrow at every four bits. This must always be executed immediately after each addition/subtraction.
Coffee Break |
Decimal adjustment is done using the contents of register A, a carry flag and an auxiliary carry flag. Actual adjustment is performed by dividing the contents of register A into higher four bits and lower four bits (this division is done for each decimal number digit). In the case of addition, either when the result value is A to F or when carry occurs (when carry or auxiliary carry is set), 6 is added to the four bits (or else, 0 is added). The result is reflected to register A and the carry flag.
Since the addition is executed between the same degits of decimal numbers, [addition result = A to F] and [carry] never occur simultaneously.