Basics of Instruction Execution
CPU (central processing
unit): contains ALU (arithmetic-logic unit), instruction interpreter,
a little bit of memory. The memory in the CPU is called
``registers.'' We need to have memory actually in the CPU because
getting it from ``memory'' takes too long (one of the major
determiners of how powerful a computer is, is how much memory it has
in the CPU, and how much can be transferred at a time between
``memory'' and the CPU); one of the ways to make a program run faster
is to make sure you have the most-used data present in the CPU at all
times.
We'll be introducing the memory present in the CPU a little bit at a
time, as needed. For today, we need the PC (program counter) register, and
the B accumulator.
The PC always contains the address of the next instruction to be
executed. B is a register where we ``accumulate'' results.
Let's take a look at a fragment of code in memory. Suppose we have
f802: CB
f803: 12
Also, let's suppose that before we start, the PC contains
f802
and B contains 24
. Here's what the CPU
is going to do with this:
- Read from memory location
f802
, getting the data
CB
back. This is the instruction fetch. -
Add one to the PC.
- Figure out what
CB
means. We
can find out what it means by looking in the pocket guide on page 15
(middle of the page). Looking across the line that this appears on,
we see:
- It takes one operand. Looking on page 18, we see that a
ii
operand means one byte of immediate data.
- It is an
ADDB (opr)
instruction.
- Its addressing mode is
IMM
(which is short for
``immediate.'').
- It will take two cycles to execute.
If we look up the ADD instruction in the reference manual on page 496,
we get a little more information about what's going on.
- Operation: says to add the contents of an accumulator with the
contents of a memory location, and put the results in the
accumulator. The notation used is described on page 488.
- Description: tries to tell you what it means in something
resembling English.
- Condition Codes and Boolean Formulae: never mind for now.
- Source Forms: what it can look like.
- Addressing Modes, etc: tells how it can be used, and gives a
detailed breakdown of what happens on the bus while it's doing it.
This process of figuring out what the instruction is going to do is
the instruction decode.
- Looking at
ADDB(IMM)
in the table, we see we've
already accomplished the first cycle shows: we read a byte from the
address of the instruction, and gotten an CB
back.
- On the next cycle, we put the new value of the PC on the bus, and
read again. What we read is our operand,
(ii)
, which
we've decided will contain a 12.
- Add one to the PC again.
- Add the value we read from memory (12) to the value in
accumulator B (24), and put the result back in accumulator B.
All the stuff after the decode is the instruction execution.