
Conditional and Arithmetic Instructions 63
values such as ASCII characters. One way to perform a comparison would
be to subtract the two numbers and then test the flags to determine their
relationship. If the two values are equal to each other, the zero flag will be
set. The carry flag, sign flag, and so on, can also be tested to determine
other inequalities. The particular flags that must be tested depends on
whether the comparison is between signed or unsigned quantities. In
fact, it is important to test for overflow as well. This may result in a
requirement to test two or more flags.
A disadvantage in performing a subtraction to compare two numbers
is the fact that the destination operand will be changed. Since the desti
nation operand must be one of the values to be compared, its value will
be destroyed. We could, of course, make a copy. The following exam
ple determines if the contents of register DO are equal to the contents of
register Dl.
HOVB.L D0,D2
SUB.L D1,D2
BEQ EQUAL
•
EQUAL: . EQUAL
There is a better way to compare numbers. A special compare instruc
tion, CMP, works almost like the SUB instruction except that the result
of the subtraction is not actually saved—in other words, the operands
remain unchanged. All the flags that would be set or reset by the SUB
instruction are also set or reset by the CMP instruction. The destination
operand must be a data register, but the source operand can be a regis
ter, variable, or constant. In fact, the source operand can use any of the
addressing modes that will be discussed in Chapter 6.
The previous example can be rewritten using the CMP instruction.
CMP.L Dl ,DO
BEQ EQUAL
EQUAL: !
Since we are using the CMP instruction and not an SUB, the value of DO
does not have to be copied.
In general we will desire to compare both signed and unsigned num
bers. The standard inequalities all have unique conditional branches that
are used with the CMP instruction. For inequalities other than equal or
MAKE A COPY OF DO
SUBTRACT Dl PROM D2
BRANCH IF THEY ARE =
NOT EQUAL
Kommentare zu diesen Handbüchern