rather than binary operations and data was retained in memory in
bed form. The Z-80 combines the simplicity of bed representation
with the efficiency of binary storage by the equivalent of bed adds
and subtracts. The binary add, or subtract, is first performed and
then the DAA, or Decimal Adjust A instruction, is performed to ad-
just the binary result in the A register to bed form. Multiple-preci-
sion bcd adds and subtracts may be performed as easily as multiple-
precision binary operations. Suppose that two two-byte bed oper-
ands are held in locations BCD1 and BCD2. Each operand consists
of four bed digits as shown in Fig. 11-2. The following code performs
a bed add of BCD2 to BCD1 with result stored in BCD1. The sec-
ond example subtracts BCD2 from BCD1, stores results in BCD1.
BCDADD LD A,(BCD2+1) GET LS BCD DIGITS OP2
LD B,A
LD A,(BCDI+1) GET LS BCD DIGITS OP2
ADD A,B
DAA
BCD
ADD
LD (BCD 1+1),A STORE LS RESULT
LD A,(BCD2) GET MS BCD DIGITS OP2
LD B,A
LD A,(BCD1) GET MS BCD DIGITS OP1
ADC A,B
DAA BCD ADD
LD (BCD1),A STORE MS RESULT
DONE
c
BCDSUB LD IX,BCD1+1 POINT TO LS OP1
LD IY,BCD2+1 POINT TO LS OP2
LD B,-2 LOOP COUNT
OR A,A CLEAR CARRY
LOOP LD A,(IX) GET BCD DIGITS OP1
LD B,(IY) GET BCD DIGITS OP2
SBC A,B OP1 - OP2
DAA DECIMAL ADJUST
LD (IX),A STORE RESULT
DEC IX POINT TO HIGHER ORDER
DEC IY
INC B BUMP LOOP COUNT
JP NZ,LOOP CONTINUE
DONE
As the subtract example shows, the routines may be easily gener-
alized to operate on (N x 2) bed digits. The carry flag is always
set by the CPU after the DAA operation to represent the carry from
the bcd operation so that the carry (or borrow) is properly propa-
gated to higher-order bed operations.
173
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers