Skip to main content

Bcd Commands 'link' May 2026

ld a, $45 add a, $27 daa ; A becomes $72 The Z80 DAA uses carry and half-carry flags to correct the accumulator. ARM (especially Thumb and Cortex-M) does not have dedicated BCD arithmetic instructions. BCD operations must be implemented via software routines, using bit manipulation and conditional addition/subtraction.

| Command | Effect in Decimal Mode | |---------|------------------------| | | Set Decimal Mode (D=1) | | CLD | Clear Decimal Mode (D=0, back to binary) | | ADC | Adds with carry in BCD | | SBC | Subtracts with borrow in BCD | bcd commands

The 8051 has a DA (Decimal Adjust) instruction that operates on the accumulator after addition. ld a, $45 add a, $27 daa ;

uint8_t bcd_add(uint8_t a, uint8_t b) uint16_t sum = (a & 0x0F) + (b & 0x0F) + (((a >> 4) + (b >> 4)) << 4); if ((sum & 0x0F) >= 10) sum += 6; if ((sum >> 4) >= 10) sum += 0x60; return (uint8_t)sum; | Command | Effect in Decimal Mode |

| Instruction | Description | Operands | |-------------|-------------|----------| | | Decimal Adjust AL after Addition (packed BCD) | implicit (AL) | | DAS | Decimal Adjust AL after Subtraction (packed BCD) | implicit (AL) | | AAA | ASCII Adjust after Addition (unpacked BCD) | implicit (AL, AH) | | AAS | ASCII Adjust after Subtraction (unpacked BCD) | implicit (AL, AH) | | AAM | ASCII Adjust after Multiply (unpacked) | implicit (AX) | | AAD | ASCII Adjust before Division (unpacked) | implicit (AX) |

mov al, 0x45 ; BCD 45 add al, 0x27 ; BCD 27 → binary result 0x6C daa ; adjusts to 0x72 (BCD 72)