bad old days, programmers had to enter programs into the
computer directly in machine language (after having walked five miles in
the snow uphill to work). In the very early stages of computing, this was done
by flipping switches. The programmer toggled strings of 1s and 0s into the
computer’s very limited memory, ran the program, and then pored over the
resulting strings of 1s and 0s to decode the answer.
Once memory sizes and processing power increased to the point where
programmer time and effort were valuable enough relative to computing
time and memory space, computer scientists devised ways of allowing the
computer to use a portion of its power and memory to take on some of the
burden of making its cryptic input and output a little more human-friendly.
In short, the tedious task of converting human-readable programs into
machine-readable binary code was automated; hence the birth of assembly
language programming. Programs could now be written using mnemonics,
register names, and memory locations, before being converted by an
assembler into machine language for processing.
In order to write assembly language programs for a machine, you have
to understand the machine’s available resources: how many registers it has,
what instructions it supports, and so on. In other words, you need a well-
defined model of the machine you’re trying to program.
The Programming Model
The programming model is the programmer’s interface to the microprocessor.
It hides all of the processor’s complex implementation details behind a
relatively simple, clean layer of abstraction that exposes to the programmer
all of the processor’s functionality. (See Chapter 4 for more on the history
and development of the programming model.)
Figure 2-7 shows a diagram of a programming model for an eight-register
machine. By now, most of the parts of the diagram should be familiar to you.
The ALU performs arithmetic, the registers store numbers, and the input-
output unit (I/O unit) is responsible for interacting with memory and the rest of the system (via loads and stores). The parts of the processor that we haven’t yet met lie in the control unit . Of these, we’ll cover the program counter and the instruction register now.
The Instruction Register and Program Counter
Because programs are stored in memory as ordered sequences of instruc-
tions and memory is arranged as a linear series of addresses, each instruction
in a program lives at its own memory address. In order to step through and
execute the lines of a program, the computer simply begins at the program’s
starting address and then steps through each successive memory location,
fetching each successive instruction from memory, placing it in a special
register, and executing it as shown in Figure 2-8.
26
Chapter 2
Control Unit
Registers
Program Counter
A
B
Instruction Register
C
D
Proc. Status Word (PSW)
E
F
G
H
Data Bus
I/O Unit
ALU
Address Bus
Figure 2-7: The programming model for a simple eight-register machine
The instructions in our DLW-1 computer are two bytes long. If we
assume that each memory cell holds one byte, then the DLW-1 must step
through memory by fetching instructions from two cells at a time.
Main Memory
Instruction
Register
Registers
ALU
CPU
Figure 2-8: A simple computer with instruction and
data registers
The Mechanics of Program Execution
27
For example, if the starting address in Program 1-1 were #500, it would
look like Figure 2-9 in memory (with the instructions rendered in machine
language, not assembly language, of course).
#500 #501 #502 #503 #504 #505 #506 #507
load #12, A
load #13, B
add A, B, C store C, #14
Figure 2-9: An illustration of Program 1-1 in memory,
starting at address #500
The Instruction Fetch: Loading the Instruction Register
An instruction fetch is a special type of load that happens automatically for every instruction. It always takes the address that’s
Celine Roberts
Gavin Deas
Guy Gavriel Kay
Donna Shelton
Joan Kelly
Shelley Pearsall
Susan Fanetti
William W. Johnstone
Tim Washburn
Leah Giarratano