sed ; enable decimal mode lda #$45 ; BCD 45 adc #$27 ; result in A = $72 (BCD 72) cld ; back to binary : The 6502’s decimal mode is famously broken on the CMOS version (65C02) but works on NMOS (original). Many emulators replicate the bug. 3.3 Z80 Processor Z80 lacks automatic BCD mode but provides DAA (Decimal Adjust Accumulator) after addition or subtraction, similar to x86.
| 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) |
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; bcd commands
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.
mov ax, 0x0405 ; unpacked 45 (AH=04, AL=05) add al, 0x03 ; add unpacked 3 → AL=08 aaa ; adjusts if AL>9, increments AH The 6502 has no dedicated BCD arithmetic instructions, but it includes a Decimal Mode flag (D flag in status register). When set, all ADC and SBC instructions automatically perform BCD arithmetic. sed ; enable decimal mode lda #$45 ;
seconds = rtc_read(0x00); // BCD 0x45 = 45 sec // Convert to binary for computation: bin_sec = ((seconds >> 4) * 10) + (seconds & 0x0F); Multiplexed displays often receive BCD directly from a 74LS47 decoder. Processor outputs BCD values to port. 5.3 Financial/Cash Registers Currency amounts must be exact to the cent. BCD arithmetic prevents floating-point errors:
mov ax, dx shr ax, 8 ; high byte of BCD1 mov dx, bx shr dx, 8 ; high byte of BCD2 add al, dl daa mov dl, al mov al, ah adc al, dh daa mov dh, al mov ax, 0x0405 ; unpacked 45 (AH=04, AL=05)
| 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 |