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

1.3 A Few Instructions And Some Simple Programs                      ! 5















                     Figure 1.10. Most Efficient Program for 16-Bit Addition

        dynamic efficiency explicitly, we do say that one program is more dynamically efficient
        than another to indicate that the first program performs the same computation more
        quickly than the other one over some range of input data.
            The CPU12RG/D manual Instruction Set Summary gives the instruction timing.
        For instance, the LDAA #$2F instruction is shown alphabetically under LDAA for the
        mode IMM. The Access Detail column indicates that this instruction takes one memory
        cycle of type P, which is a program word fetch. Generally, a memory cycle is 125 ns.
            The clarity of a program is hard to evaluate but has the greatest significance in
        large programs that have to be written by many programmers and that have to be
        corrected and maintained for a long period. Clarity is improved if you use good
        documentation techniques, such as comments on each instruction that explain what you
        want them to do, and flowcharts and precise definitions of the inputs, outputs, and the
        state of each program, as explained in texts on software engineering. Some of these
        issues are discussed in Chapter 5. Clarity is also improved if you know the instruction
        set thoroughly and use the correct instruction, as developed in the next two chapters.
            While there are often two or more equally good programs, the instruction set may
        provide significantly better ways to execute the same operation, as illustrated by Figure
         1.10. The 6812 has an instruction LDD to load accumulator D, an instruction ADDD to
        add to accumulator D, and an instruction STD to store accumulator D, which, for
        accumulator D, are analogous to the instructions LDAA, ADDA, and STAA for
        accumulator A. The following program performs the same operations as the programs
        given above but is much more dynamically and statically efficient and is clearer.
            If you wish to write programs in assembly language, full knowledge of the
        computer's instruction set is needed to write the most efficient, or the clearest, program.
        The normal way to introduce an instruction set is to discuss operations first and then
        addressing modes. We will devote Chapter 2 to the discussion of instructions and Chapter
        3 to the survey of addressing modes.
            In summary, you should aim to write good programs. As we saw with the example
        above, there are equally good programs, and generally there are no best programs. Short,
        fast, clear programs are better than the opposite kind. Yet the shortest program is rarely
        the fastest or the clearest. The decision as to which quality to optimize is dependent on
        the application. Whichever quality you choose, you should have as a goal the writing of
        clear, efficient programs. You should fight the tendency to write sloppy programs that
        just barely work or that work for one combination of inputs but fail for others.
        Therefore, we will arbitrarily pick one of these qualities to optimize in the problems at
   33   34   35   36   37   38   39   40   41   42   43