Back to the 2022 paper

Module 2: x86 Architecture, CPU Control, I/O, Memory System Design

202214m

The x86 architecture includes an instruction called decimal adjust after addition (DAA). DAA performs the following sequence of instructions:

if((AL AND 0FH) > 9) OR (AF = 1) then
    AL ← AL + 6;
    AF ← 1;
else
    AF ← 0;
endif;
if(AL > 9FH) OR (CF = 1) then
    AL ← AL + 60H;
    CF ← 1;
else
    CF ← 0;
endif.

"H" indicates hexadecimal. AL is an 8-bit register that holds the result of addition of two unsigned 8-bit integers. AF is a flag set if there is a carry from bit 3 to bit 4 in the result of an addition. CF is a flag set if there is a carry from bit 7 to bit 8. Explain the function performed by the DAA instruction.

Worked SolutionAI Assisted

Function of DAA Instruction

1. Context

x86 AL holds two packed BCD digits (Binary Coded Decimal): high nibble = tens digit 0-9, low nibble = units digit 0-9, e.g. 0x27 = decimal 27. Binary ADD AL, BL adds them as 8-bit binary and sets flags AF (auxiliary carry from bit3) and CF. Result may be invalid BCD (nibble >9 or unadjusted carry) and needs correction to remain valid packed BCD.

DAA = Decimal Adjust after Addition converts the binary result in AL to correct packed BCD and sets CF/AF to represent decimal carry.

2. What the pseudo-code does

if (low_nibble >9) OR (AF=1) then AL+=6; AF=1 else AF=0;  // correct low digit
if (AL >9FH) OR (CF=1) then AL+=0x60; CF=1 else CF=0;      // correct high digit
  • AL AND 0FH isolates low nibble.
  • +6 = 0000 0110 : adds 6 to low nibble; causes carry to high nibble if needed, skipping the 6 invalid hex values A-F.
  • +60H = 0110 0000 : adds 6 to high nibble (60H = 96 = 6*16).

AF and CF after DAA represent decimal carries (not binary), for multi-byte BCD addition (propagate with ADC+DAA).

3. Why +6 ?

In BCD, counting after 9 should go to 10 (0x10), but binary after 0x09 goes to 0x0A. Gap = 6 (0x0A - 0x0F are invalid). Adding 6 forces carry and wraps to correct BCD value:
9+1 = 0x0A -> +6 = 0x10 (correct BCD for 10).

4. Examples

Example 1: AL=0x29 + 0x15 = 0x3E (binary). Low nibble E=14>9

  • First IF true: AL=0x3E+0x06=0x44, AF=1
  • Second IF: AL=0x44 <=0x9F and CF=0 -> CF=0
  • Result 0x44 = BCD 44 . Correct: 29+15=44 decimal.

Example 2: AL=0x49+0x38=0x81 . AF=0? 9+8=17 -> low nibble 1 with AF=1 (carry from bit3)

  • First IF: (1>9? No) but AF=1 -> AL=0x81+0x06=0x87, AF=1
  • Second: 0x87 <=9F and CF=0 -> CF=0
  • Result 0x87 = 87 . Correct: 49+38=87.

Example 3: AL=0x99+0x99=0x32 binary (0x132 truncated) with CF=1, AF=1

  • First IF: 0x2>9?No but AF=1 -> AL=0x32+0x06=0x38, AF=1
  • Second IF: CF=1 -> AL=0x38+0x60=0x98, CF=1
  • Result 0x98 with CF=1 => BCD 198 (CF is hundreds carry). Correct: 99+99=198.

5. Summary of Function

DAA performs binary-to-BCD correction after an 8-bit ADD of two packed BCD numbers:

  • Makes each 4-bit nibble a valid decimal digit 0-9
  • Propagates decimal carry correctly between digits
  • Leaves AL = low two decimal digits of sum (00-99) and CF = high decimal carry (0 or 1) for extended BCD arithmetic (e.g., ADC + DAA chain for multi-byte BCD such as 4-digit BCD with AL+AH).

Without DAA, binary addition would yield hex digits A-F which have no BCD meaning. With DAA, x86 can do decimal arithmetic directly used in financial/cobol applications where binary rounding errors unacceptable.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.