Page 66 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 66

2.5 Control Instructions                                             43

                           Table 2.8. Conditional Branch Instructions




























            Conditional branch instructions test the condition code bits. As noted earlier, these
        bits have to be carefully watched, for they make a program look so correct that you want
        to believe that the hardware is at fault. The hardware is rarely at fault. The condition code
        bits are often the source of the fault because the programmer mistakes where they are set
        and which ones to test in a conditional branch. The instructions should now be reviewed
        with regard to how they affect the condition code bits. See the right columns of the
        CPU12RG/D manual Instruction Set Summary. Note that move instructions generally
        either change the N and Z bits or change no bits; arithmetic instructions generally change
        all bits; logic instructions change the N and Z bits; and edit instructions change all bits.
        However, there are many exceptions, and these exceptions are precisely the ones that
        cause mystifying errors. There is sound rationale for which bits are set and the way they
        are set. Some of that is discussed in this chapter. But most of it is simply learned by
        experience. We conclude by reminding you that when your program does not work and
        you have checked every angle, carefully examine the setting and testing of the condition
        code bits. Now we look at the testing of these bits in detail.
             Eight simple branching instructions test only a single condition code register bit:
        BNE, BEQ, BPL, BMI, BVC, BVS, BCC, and BCS. The letters S and C are used for "set"
        and "clear" (to 1 and 0, respectively) in branching instruction mnemonics.
            Frequently, two numbers are compared, as in a compare instruction or a subtraction.
        One would like to make a branch based on whether the result is positive, negative, and
        so forth. Table 2.8 shows the test and the branching statement to make depending on
        whether the numbers are interpreted as signed numbers or unsigned numbers. The branch
        mnemonics for the two's-complement numbers, or signed, numbers case are the ones
        usually described in mathematical "greater or less" prose. For example, BLT for "branch
        if less than," BLE for "branch if less than or equal to," and so forth. The mnemonics for
        unsigned numbers are described in mathematical "high or low" prose, offbeat enough to
   61   62   63   64   65   66   67   68   69   70   71