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

8                              Chapter 1 Basic Computer Structure and the 6812

        1.3 A Few Instructions and Some Simple Programs

        Now that we have examined the instruction from static and dynamic points of view, we
        will look at some simple programs. The machine code for these programs will be
        described explicitly so that you can try out these programs on a real 6812 and see how
        they work, or at least so that you can clearly visualize this experience. The art of
        determining which instructions have to be put where is introduced together with a
        discussion of the bits in the condition code register. We will discuss what we consider to
        be a good program versus a bad program, and we will discuss what is going to be in
        Chapters 2 and 3, We will also introduce an alternative to the immediate addressing mode
        using the load instruction. Then we bring in the store, add, software interrupt, and add
        with carry instructions to make the programs more interesting as we explain the notions
        of programming in general.
            We first consider some variations of the load instruction to illustrate different
        addressing modes and representations of instructions. We may want to put another
        number into register A. Had we wanted to put $3E into A rather than $2F, only the
        second byte of the instruction would be changed, with $3E replacing $2F. The same
        instruction as

                                     LDAA #$3E

        could also be written using a decimal number as the immediate operand: for example,

                                       LDAA #62

        Either line of source code would be converted to machine code as follows:






        We can load register B using a different opcode byte. Had we wanted to put $2F into
        accumulator B, the first byte would be changed from $86 to $C6 and the instruction
        mnemonic would be written
                                      LDAB #$2F

        We now introduce the direct addressing mode. Although the immediate mode is useful for
        initializing registers when a program is started, the immediate mode would not be able to
        do much work by itself. We would like to load words that are at a known location but
        whose actual value is not known at the time the program is written. One could load
        accumulator B with the contents of memory location $0840. This is called direct
        addressing, as opposed to immediate addressing. The addressing mode, direct, uses no
        pound sign "#" and a 2-byte address value as the effective address; it loads the word at this
        address into the accumulator. The instruction mnemonic for this is

                                     LDAA   $0840
        and the instruction appears in memory as the three consecutive bytes.
   26   27   28   29   30   31   32   33   34   35   36