Page 37 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 37
14 Chapter \ Basic Computer Structure and the 6812
almost never the shortest or the clearest. The measure of a program has to be made on
one of the qualities, or on one of the qualities based on reasonable limits on the other
qualities, according to the application. Also, the quality of clarity is difficult to measure
but is often the most important quality of a good program. Nevertheless, we discuss the
shortness, speed, and clarity of programs to help you develop good programming style.
The number of bytes in a program (its length) and its execution time are something
we can measure. A short program is desired in applications where program size affects
the cost of the end product. Consider two manufacturers of computer games. These
products feature high sales volume and low cost, of which the microcomputer and its
memory are a significant part. If one company uses the shorter program, its product may
need fewer ROMs to store the program, may be substantially cheaper, and so may sell in
larger volume. A good program in this environment is a short program. Among all
programs doing a specific computation will be one that is the shortest. The quality of
one of these programs is the ratio of the number of bytes of the shortest program to the
number of bytes in the particular program. Although we never compute this static
efficiency of a program, we will say that one program is more statically efficient than
another to emphasize that it takes fewer bytes than the other program.
The CPU12RG/D manual Instruction Set Summary gives the length of each
instruction by showing its format. For instance, the LDAA #$2F instruction is shown
alphabetically under LDAA in the line IMM. The pattern 86 ii, means that the opcode is
$86 and there is a one-byte immediate operand ii, so the instruction is two bytes long.
The speed or execution time of a program is prized in applications where the
microcomputer has to keep up with a fast environment, such as in some communication
switching systems, or where the income is related to how much computing can be done.
A faster computer can do more computing and thus make more money. However, speed
is often overemphasized: "My computer is faster than your computer." To show you that
this may be irrelevant, we like to tell this little story about a computer manufacturer.
This is a true story, but we will not use the manufacturer's real name for obvious
reasons. How do you make a faster version of a computer that executes the same
instruction? The proper answer is that you run a lot of programs, and find instructions
that are used most often. Then you find ways to speed up the execution of those often-
used instructions. Our company did just that. It found one instruction that was used very,
very often: It found a way to really speed up that instruction. The machine should have
been quite a bit faster, but it wasn't! The most common instruction was used in a routine
that waited for input-output devices to finish their work. The new machine just waited
faster than the old machine that it was to replace. The moral of the story is that many
computers spend a lot of time waiting for input-output work to be done. A faster
computer will just wait more. Sometimes speed is not as much a measure of quality as it
is cracked up to be. But then in other environments, it is the most realistic measure of a
program. As we shall see in later chapters, the speed of a particular program can depend
on the input data to the program. Among all the programs doing the same computation
with specific input data, there will be a program that takes the fewest number of clock
cycles. The ratio of this number of clock cycles to the number of clock cycles in any
other program doing the same computation with the same input data is called the
dynamic efficiency of that program. Notice that dynamic efficiency does depend on the
input data but not on the clock rate of the microprocessor. Although we never calculate