BCD Arithmetic: The DAA Instruction
Binary-Coded Decimal (BCD) is a way to store numbers where each byte holds two decimal digits (0-9). This is vital for financial or numerical applications where decimal accuracy is required, as standard binary math can introduce rounding errors when converting to decimal.
The Problem: If you add 9 and 1 in binary, the result is 10 (0A Hex). In BCD, the result should be 10 (10 Hex).
The Solution: DAA (Decimal Adjust Accumulator)
The DAA
instruction is run immediately after an ADD
or SUB
operation on BCD digits. It automatically corrects the binary result in the Accumulator (A) so that it represents a valid BCD number.
Example: BCD Addition
LD A, 09H ; A = BCD 09
LD B, 01H
ADD A, B ; A becomes 0AH (Binary 10). Carry flag is not set.
DAA ; A is corrected to 10H (BCD 10). Carry flag is set.
Restart (RST) Instructions
The RST
instructions are a special, single-byte form of the CALL
instruction. They are designed for fast, frequent, high-priority routine calls, often used for interrupt handling.
How it Works:
RST P
calls a fixed, pre-defined address in low memory. P is a value that, when multiplied by 8, gives the jump address.
Instruction | Address | Purpose (Typical) |
---|---|---|
RST 08H |
0008H |
Often used for system timers or interrupts. |
RST 18H |
0018H |
Used for I/O operations or fast I/O calls. |
RST 38H |
0038H |
Often the default trap address for unexpected errors. |
Example: Calling a Fast Routine
; Code is running normally
RST 18H ; The fastest way to call the routine at address 0018H
; The routine at 0018H must end with RET to come back here.
Since RST
pushes the return address onto the stack just like CALL
, you must still manage your registers with PUSH
and POP
inside the routine.