Chapter
6. Programming
Techniques
MUL TIBYTE ADDITION AND SUBTRACTION
The carry flag and the
ADC
(add with carry) instructions
may
be
used to add unsigned data quantities
of
arbitrary length. Consider the following addition of two three-byte unsigned hexadecimal numbers:
32AF8A
+84BA90
B76A1A
To perform
this addition, add to the low-order byte using an ADD instruction. ADD sets the carry flag for use
in
subsequent instructions, but does not include the carry
flag
in
the addition. Then
use
ADC to add to
all
higher order bytes.
32
84
B7
carry = 1
AF
BA
""'Y
= IS
8A
90
lA
The following routine
will
perform this multibyte addition, making these assumptions:
The E register
holds the length of each number to
be
added (in this case, 3).
The numbers
to
be
added are stored from low-order byte to high-order byte beginning
at
memory locations
FI
RST
and SECND, respectively.
The
result
will
be
stored from low-order byte to high-order byte beginning at memory location FIRST, replacing
the original contents
of
these locations.
MEMORY
LOCATION
before
after
FIRST 8A
-..+
~
lA
} carry
FIRST+1
AF
+
~
6A
~
carry
FIRSTt2
32
+-.
B7
SECND
90
90
SECND+l
BA
BA
SECND+2
84
84
6-11