be any size, one byte to n bytes. If an operand of four bytes or 32
bits is required, for example, numbers from 0 to 4,294,967,296 may
be handled (or the equivalent range of negative and positive num-
bers). To add or subtract two four-byte operands, a carry or borrow
must be propagated to the higher-order bytes. This means that an
add or subtract to the three higher orders will not suffice; there must
be an add or subtract with carry (carry also represents a borrow
in the Z-80 and other machines). The following code performs a
four-byte add and subtract on two four-byte operands located in
OP1 and OP2. OP2 is added or subtracted to OPl and the result put
in OPl. The first add or subtract is of the lowest order and no carry
or borrow exists from previous orders, therefore, an ADD or SUB is
used. Subsequent adds and subtracts utilize the ADC (add with
carry) and SBC [subtract with carry (borrow) ] instructions to
propagate the carry or borrow.
ADD4
NE
LD
LD
LD
ADD
LD
LD
ADC
LD
LD
ADC
LD
LD
ADC
LD
S
IX,OP1+3
IY,OP2 + 3
A,(IX)
A,( IY)
(IX),A
A,(IX-1)
A,( IY-1)
(IX-1),A
A,(IX-2)
A,( IY-2)
(IX-2 ),A
A,(IX-3)
A,( IY-3)
(IX-3),A
POINT TO LOW-ORDER BYTE
POINT TO LOW-ORDER BYTE
OPl + OP2 BYTE 3
STORE RESULT IN OP1+3
OP1 + OP2 BYTE 2
STORE RESULT IN OP1 + 2
OP1 + OP2 BYTE 1
STORE RESULT IN OP1 + 1
OP1 + OP2 BYTE 0
STORE RESULT IN OP1
SUB4
LD
LD
IX , OP1+3
IY,OP2 + 3
POINT TO LOW-ORDER BYTE
POINT TO LOW- ORDER BYTE
LD
XOR
B,4
A
INITIALIZE COUNT
CLEAR CARRY
LOOP4
LD
SBC
LD
DEC
DEC
DEC
JP
A,( IX)
(IY)
(IX),A
IX
IY
B
NZ , LOOP4
LOAD BYTE
OP1 - OP2
STORE RESULT
POINT TO NEXT HIGH-ORDER
DECREMENT COUNT
GO IF NOT DONE
DONE
The examples of add and subtract illustrate two different ap-
proaches to the solution of the same problem. The add example
164
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers