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:
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;
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.
mov al, 0x45 ; BCD 45 add al, 0x27 ; BCD 27 → binary result 0x6C daa ; adjusts to 0x72 (BCD 72)
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:
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;
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.
mov al, 0x45 ; BCD 45 add al, 0x27 ; BCD 27 → binary result 0x6C daa ; adjusts to 0x72 (BCD 72)
⬤ | We are offline | c |
| E-mail: |