Decimal Adjust After Addition (DAA) in x86: How It Converts Binary Sums to Valid BCD
The x86 instruction DAA performs a decimal-correction on the 8-bit sum in AL after adding two unsigned 8-bit values that represent (packed) BCD digits. Conceptually, DAA ensures that the low nibble (bits 0–3) and high nibble (bits 4–7) each become valid decimal digits (0–9) in hexadecimal encoding (i.e., BCD format).2
A typical interpretation is:
- Treat the original operands as packed BCD:
- low nibble = ones digit (0–9)
- high nibble = tens digit (0–9)
- After an ordinary binary addition, some intermediate digit nibbles may exceed 9.
- DAA conditionally adds:
- 6 to fix the low nibble
- 0x60 to fix the high nibble
- It also updates the digit-carry-related flags:
- AF tracks whether a carry occurred from bit 3 to 4 during the low-nibble correction decision
- CF tracks whether the overall decimal correction produced a carry beyond the two-digit BCD range
The pseudocode you provided matches the canonical DAA correction logic: it first checks the low nibble and prior AF state; then it checks the high nibble and the prior CF state.2
Footnotes
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA instruction description (DAA adjusts AL for BCD using AF and rule). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See the instruction reference entry for DAA (decimal adjust after addition). ↩ ↩2
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA second phase rule (high nibble check using or CF=1 and adding ). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See DAA (decimal adjust after addition) for and CF behavior. ↩ ↩2
Key idea: DAA performs two independent “digit validations”
DAA essentially enforces BCD constraints for each nibble after a binary addition:
- Low nibble correction (add
0x06) happens when the ones digit is not valid BCD or when the previous half-digit carry implies it should be corrected.2 - High nibble correction (add
0x60) happens when the tens digit is not valid BCD or when the overall carry implies it should be corrected.2
This produces a packed BCD result in AL (two decimal digits) for many typical uses involving BCD addition.
Footnotes
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA instruction description (DAA adjusts AL for BCD using AF and rule). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See the instruction reference entry for DAA (decimal adjust after addition). ↩ ↩2
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA second phase rule (high nibble check using or CF=1 and adding ). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See DAA (decimal adjust after addition) for and CF behavior. ↩ ↩2
Why “add 6” and “add 0x60”?
If a nibble holds an invalid value between 10 and 15, it must be reduced by 10 (in decimal sense) while keeping BCD encoding aligned. The standard trick is:
- After adding two BCD digits, if the low nibble exceeds 9, then adding 6 adjusts it so the resulting nibble maps to the correct decimal digit.
- Because packed BCD stores tens in the high nibble, the corresponding correction is
0x60(which is6shifted left by 4).
This is exactly what the DAA micro-logic performs: it implements the decimal carry correction for base-10 digits using base-16 arithmetic.2
Footnotes
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA instruction description (DAA adjusts AL for BCD using AF and rule). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See the instruction reference entry for DAA (decimal adjust after addition). ↩
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA second phase rule (high nibble check using or CF=1 and adding ). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See DAA (decimal adjust after addition) for and CF behavior. ↩
What the DAA pseudocode does (line-by-line)
- 1Step 1
Evaluate or use the prior AF as an indicator that a half-digit carry occurred. If true, the ones digit is corrected by adding .
- 2Step 2
If the condition is true, set and set AF to . Otherwise, set AF to .
- 3Step 3
Evaluate whether (meaning the high nibble is effectively > 9) or whether the prior CF indicates a carry that requires decimal correction.
- 4Step 4
If the condition is true, set and set CF to . Otherwise, set CF to .
Relationship to BCD addition correctness
Let the original sum in AL represent “two decimal digits” in packed BCD encoding. After a raw binary addition, each nibble may not reflect a valid digit 0–9. DAA fixes this by:
- correcting the ones digit using
+0x06when necessary - correcting the tens digit using
+0x60when necessary - setting flags so that the correction reflects decimal carry behavior
In other words, DAA converts a binary sum of BCD-encoded operands into a valid packed BCD result, while propagating the proper decimal-carry information via AF/CF.2
Footnotes
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA instruction description (DAA adjusts AL for BCD using AF and rule). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See the instruction reference entry for DAA (decimal adjust after addition). ↩
-
Intel® 64 and IA-32 Architectures Software Developer’s Manual — DAA second phase rule (high nibble check using or CF=1 and adding ). https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html - See DAA (decimal adjust after addition) for and CF behavior. ↩
Mental model
Think of each nibble as a separate base-10 digit. DAA checks whether each digit is still in . If not, it adds (ones) or (tens) to bring it back into BCD-valid range.
DAA is not a generic decimal conversion for arbitrary binary integers
DAA assumes the inputs represent packed BCD digits. If AL contains an arbitrary binary sum that is not derived from valid BCD operands, the correction rules may not produce a meaningful decimal result.
DAA correction decisions (mapping to nibble validity)
DAA uses the low-nibble check first (AF-related), then a high-nibble/carry check (CF-related).
Common interpretations and edge cases
Quick self-check on DAA
DAA correction lifecycle (after an addition)
Compute raw binary sum
1Operands are added normally; result is in AL with binary carry behavior."
Adjust low nibble
2If ones digit invalid (or AF indicates half-carry), add 0x06; set AF accordingly."
Adjust high nibble
3If tens digit invalid (or CF indicates carry), add 0x60; set CF accordingly."
Final corrected packed BCD in AL
4AL now encodes the correct two-digit packed BCD result."
Knowledge Check
Which constant does DAA add to correct an invalid low nibble (ones digit) after addition?