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

12                           Chapter! Basic Computer Structure and the 6812










                         Figure 1.6. Bits in the Condition Code Register

            We will now look at condition code bits in general and the carry bit in particular.
        The carry bit is really pretty obvious. When you add by hand, you may write the carry
        bits above the bits that you are adding so that you will remember to add it to the next
        bit. When the microcomputer has to add more than eight bits, it has to save the carry
        output from one byte to the next, which is Cout in Figure 1.4b, just as you remembered
        the carry bits in adding by hand. This carry bit is stored in bit C of the condition code
        register shown in Figure 1.6. The microcomputer can input this bit into the next
        addition as Cin in Figure 1.4b. For example, when adding the 2-byte numbers $349E and
        $2570, we can add $9E and $70 to get $OE, the low byte of the result, and then add $34,
        $25, and the carry bit to get $5A, the high byte of the result. See Figure 1.7. In this
        figure, C is the carry bit obtained from adding the contents of locations 11 and 13; (re) is
        used to denote the contents of memory location m, where m may be 10,11,12, etc. The
        carry bit (or carry for short) in the condition code register (Figure 1.6) is used in
        exactly this way.
            The carry bit is also an error indicator after the addition of the most significant bytes
        is completed. As such, it may be tested by conditional branch instructions, to be
        introduced later. Other characteristics of the result are similarly saved in the controller's
        condition code register. These are, in addition to the carry bit C, N (negative or sign bit),
        Z (zero bit), V (two's-complement overflow bit or signed overflow bit), and H (half-
        carry bit) (see Figure 1.6). How 6812 instructions affect each of these bits is shown in
        the CPU 12 Reference Guide, Instruction Set Summary, in the rightmost columns.
            We now look at a simple example that uses the carry bit C. Figures 1.8 and 1.9 list
        two equally good programs to show that there is no way of having exactly one correct
        answer to a programming problem. After the example, we consider some ways to know
        if one program is better than another. Suppose that we want to add the two 16-bit
        numbers in locations $810, $811 and $812, $813, putting the sum in locations $814,
        $815. For all numbers, the higher-order byte will be in the smaller-numbered location,
        One possibility for doing this is the following instruction sequence, where, for
        compactness, we give only the memory location of the first byte of the instruction.













                          Figure 1.7. Addition of Two-Byte Numbers
   30   31   32   33   34   35   36   37   38   39   40