Page 30 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 30
1,2 The Instruction 7
Machine code is quite useful for making small changes in a program that is being
ran and corrected or debugged. However, writing even a moderately long program in
machine code is a punishment that should be reserved for the fifth level of Dante's
inferno. In Chapter 4 we discuss how text produced by an editor is converted by an
assembler to the machine code stored in the computer's memory. The text input to the
assembler is called source code. In a line of source code, to make remembering the
instructions easier, a three- or four-character mnemonic is used to describe the operation,
and its addressing information may be given as a hexadecimal or a decimal number. A
tine of source code, consisting of mnemonics and addressing information, can be
converted by hand into their hexadecimal machine code equivalents using Motorola's
CPU 12 Reference Guide (you can order it from Motorola by using reference number
CPU12RG/D). In the first three chapters, we want to avoid using the assembler, to see
clearly just how the computer ticks. We will hand-convert mnemonics and addressing
information to hexadecimal machine code and work with hexadecimal machine code.
We now look at a load immediate instruction in detail, to introduce concepts about
instructions in general. The load instruction will move a byte into an accumulator, either
A or B. Its simplest addressing mode is called immediate. For instance, to put a specific
number, say $2F, in accumulator A, execute the instruction whose source code line is
LDAA #$2F
where the symbol "#" denotes immediate addressing and the symbol "$" is used to
indicate that the number that follows is in hexadecimal. This instruction is stored in
memory as the two consecutive bytes:
(Look in CPU 12 Reference Guide, Instruction Set Summary, for the mnemonic LDAA
and, under it, find $86 in under the Machine Coding column, in the row beginning
LDAA #opr8i, which also has the addressing mode IMM for immediate addressing.)
Looking dynamically at an instruction, an operation (e.g., add, subtract, load, clear,
etc.) may be carried out with inputs (or operands) and may produce a result. The
instruction is executed in the instruction cycle as follows.
1. Fetch the first byte of the instruction from memory.
2. Increment the PC by one.
3. Decode the opcode that was fetched in step 1.
4. Repeat steps 1 and 2 to fetch all bytes of the instruction.
5. Calculate the effective address to access memory, if needed.
6. Recall the operand from memory, if needed.
7. Execute the instruction, which may include writing the result into memory.
The controller fetches the first byte, $86. The program counter is incremented. The
controller decodes $86. The controller fetches the second byte, $2F, putting it into
accumulator A. The program counter is incremented. After this instruction is executed,
another instruction is fetched and executed.