for
CSCI 1301: Great Ideas in Computer Science
(c) 1993 - 2007 John E. Howland, All Rights Reserved
Department of Computer Science
Trinity University
One Trinity Place
San Antonio, Texas 78212-7200
Internet: jhowland@ariel.cs.trinity.edu
The processor itself is divided into three components that carry out the various functions that the computer is capable of performing. The first component of the processor is the controller. The controller acts as a foreman that oversees the tasks of the processor. The controller looks at the next instruction to be executed and assigns the sub-tasks that must be accomplished to carry out that instruction to the other components of the processor.
Another component of the processor is the Arithmetic-Logic Unit (ALU). This unit is the part of the processor which performs the mathematical computations and logical tasks that we expect a computer to be able to do. Addition, subtraction, comparison, etc., are all carried out by the ALU.
The last component of the processor is a collection of one or more registers. Registers are special named memory cells in the processor where information is temporarily stored during various stages of a computation. The currently executing instruction, for example, resides in a register called the Instruction Register. Since modern processors can execute millions of instructions per second it is expected that information would not stay in a register for more than a few millionths of a second.
The memory unit of a computer is the place were both data and programs are stored. John Von Neuman, a mathematician and physicist, is generally credited with the idea of structuring a computer so that both data and programs are stored in the memory of a computer. Earlier computer designs had computer programs hard-wired into the processor which meant that when it was necessary to re-program a machine, the machine literally had to be re-wired. This was a very time consuming and error prone process. Modern computers, which store both programs and data in their memories, are sometimes called Von Neuman machines in honor of his contribution of this key idea.
A computer memory system is accessed (or read) by specifying the location (called an address) of the memory cell. The memory system then responds by producing a copy of the contents of that cell. The original value of the cell is not changed by this process. This is sometimes called a non-destructive read.
A computer memory system is changed (or written) by specifying the location of the memory cell together with the new value for that cell. The previous value stored in the cell is replaced by the new value. This is sometimes called a destructive write.
The values stored in a computer memory are simply numbers. Numbers are used to represent both data and instructions. In fact, one cannot distinguish instructions from data when examining the contents of a memory system. It is up to the programmer or operating system to keep track of which memory cells hold data and which are program instructions. Programs have been written which manipulate and produce programs. Such programs treat instructions as data. The usefullness of this idea was recognized by Von Neuman and was, perhaps, one of the motivating reasons for his stored program design for a computer.
The processor and memory unit are wired together wiring connections called buses. A bus is a low resistance connection consisting of 1 or more wires. There are three such connections. The first, called the address bus, is used by the processor to tell the memory system the number or location of the memory cell the processor wishes to access. The second bus is used to send data out to the memory. The third bus is used to transmit data and instructions to the processor.
A typical computer might be organized as indicated in the following diagram:

The processor contains a few memory cells, called registers, which are used for efficient temporary storage:

The Contents of a memory cell or a register is simply a number. For purposes of illustration, suppose that each memory cell is large enough to hold numbers up to 4 decimal digits in size. Later, we will learn that most computers use the binary number system, but for now we will use the decimal number system in our model computer descriptions.
If a memory cell holds an an instruction, use the first two decimal digits represent the operation and the remaining digits to represent the adderess or location of the instruction operand.

Using this scheme, we could set up the following operation codes:
Operation Code Function add 01 c(acc)=c(acc)+c(addr) sub 02 c(acc)=c(acc)-c(addr) load 03 c(acc)=c(addr) store 04 c(addr)=c(acc)
Note: These codes do not correspond to any known real computer, but rather, they are the operation codes for a hypothetical model computer which we will use to illustrate important aspects of computer organization.
q =: rTo accomplish this task, it is necessary to first load the value of r into the accumulator and then store the accumulator in the memory cell q.
load r store qConsider a program which computes the following expression:
c =: a + b load a add b store cIt is possible to write a program which will automatically translate from the J version of this program to the symbolic form of the machine instructions shown above. Such a program, which translates from the J notation, c =: a + b to the machine language:
load a add b store c
The program must be stored in memory before it may be executed. Since we don't know exactly where in memory we wish to put either the program or the data items a, b or c, we can only partially translate the program into machine readable form as:
op location 03 a 01 b 04 cThe memory system is a collection of memory cells, each having a number or address. The following diagram illustrates the organization of memory

Suppose we decide to locate the above program beginning in memory cell 4. Since the program takes 3 cells we might put the data in the first free cell after the last instruction in the program which means that we could define:
Symbol Location a 7 b 8 c 9The final translated program then looks like:
location operation address 04 03 07 05 01 08 06 04 09A program which translates from the symbolic form for a program
load a add b store cto the machine language form is called a symbolic assembler, or simply an assembler.

The program counter register always contains the location of the next instruction to be processed. To get started executing this program we somehow have to set the contents of the program counter (pc) to 4 because this is the location of the first instruction of our program.
The following flow chart best explains how the processor executes a program.
Step 0 Set initial value for the pc
pc 04Step 1 (fetch)
ir ?
acc ?
pc 04Step 2 (increment pc)
ir 0307
ac ?
pc 05Step 3 (execute)
ir 0307
ac ?
pc 05Step 4 (fetch)
ir 0307
ac 22 (here we are supposing that C(07) has been set to 22 somehow and also suppose C(08) has been set to 3)
pc 05Step 5 (increment pc)
ir 0108
acc 22
pc 06Step 6 (execute)
ir 0108
acc 22
pc 06Step 7 (fetch)
ir 0108
acc 25
pc 06Step 8 (increment pc)
ir 0409
acc 25
pc 07Step 9 (execute)
ir 0409
acc 25
pc 07Step 10 (fetch)
ir 0409
acc 25 (contents of 09 changed to 25)
pc 07Step 11 (increment pc)
ir 0022
acc 25
pc 08Step 12 (execute)
ir 0022
acc 25
pc 08Of course, at this point we have a problem since the ir does not contain an instruction.
ir 0022
acc 25
This means we need some kind of more elaborate setup for placing programs in memory and starting and stopping programs. These are some of the tasks performed by a computer operating system.
noun =: 0
adverb =: 1
conj =: 2
monad =: 3
dyad =: 4
define =: : 0
from =: {
amend =: }
append =: ,
take =: {.
drop =: }.
rep =: #:
base =: #.
time =: 6 !: 2
display =: 1 !: 2 & 2
format =: ":
trace =: 0
A
memory system is modeled as a list of numbers. For example, a small memory
containing a machine language program for the program c =: a + b may
be modeled as:
mem =: 0 0 0 0 307 108 409 22 3 0We next consider two verbs, access and store, which are used to access and modify memory.
access =: monad def 'y. from mem' store =: monad def 'mem =: (1 from y.) (0 from y.) amend mem'Then to access the value (load instruction) stored in memory cell 4 you could:
access 4To store the value 10 in cell 3 you would write:
store 3 10To see the contents of all of the memory system write:
mem
pc =: 0 ir =: 0 ac =: 0Following is the processor model:
proc =: monad define
whilst. y. do.
NB. show registers and memory if tracing
displayRegisters ''
NB. fetch instruction
ir =: access pc
NB. increment pc
pc =: pc + 1
NB. decode instruction
('opCode' ; 'address') =. 100 100 rep ir
NB. interpret instruction
if. opCode = 1
do. ac =: ac + access address continue. end.
if. opCode = 2
do. ac =: ac - access address continue. end.
if. opCode = 3
do. ac =: access address continue. end.
if. opCode = 4
do. store address , ac continue.
else. 'invalid operation code' break. end.
end.
)
NB. the tracing function
displayRegisters =: monad define
if. trace
do.
display 'pc= ' , (format pc) , ', ir= ' , (format ir) , ', ac= ' , format ac
display 'mem= ' , format mem
else. 0
end.
)
The processor contains a facility for tracing the execution of a program. If the global trace is zero, then no tracing occurs. If trace is one, then a display of the processor registers and memory occurs before the execution of each machine instruction.
trace =: 0 NB. Define a memory mem =: 0 0 0 0 307 108 409 22 3 0 NB. Set the program counter so that the first instruction will NB. executed. pc =: 4 proc 1 invalid operation code ac 25 mem 0 0 0 0 307 108 409 22 3 25 pc 8Now we try the same thing, but turn tracing on so we can see the result of each processor machine cycle. The display of the processor registers occurs at the beginning of each machine cycle.
NB. Reload the contents of memory mem =: 0 0 0 0 307 108 409 22 3 0 trace =: 1 displayRegisters '' pc= 8, ir= 22, ac= 25 mem= 0 0 0 0 307 108 409 22 3 0 pc =: 4 proc 1 pc= 4, ir= 22, ac= 25 mem= 0 0 0 0 307 108 409 22 3 0 pc= 5, ir= 307, ac= 22 mem= 0 0 0 0 307 108 409 22 3 0 pc= 6, ir= 108, ac= 25 mem= 0 0 0 0 307 108 409 22 3 0 pc= 7, ir= 409, ac= 25 mem= 0 0 0 0 307 108 409 22 3 25 invalid operation code displayRegisters '' pc= 8, ir= 22, ac= 25 mem= 0 0 0 0 307 108 409 22 3 25To cause the processor to execute a single instruction at a time, enter the expression proc 0. For example:
NB. Reload the contents of memory mem =: 0 0 0 0 307 108 409 22 3 0 displayRegisters '' pc= 8, ir= 22, ac= 25 mem =: 0 0 0 0 307 108 409 22 3 0 pc =: 4 proc 0 pc= 4, ir= 22, ac= 25 mem= 0 0 0 0 307 108 409 22 3 0 proc 0 pc= 5, ir= 307, ac= 22 mem= 0 0 0 0 307 108 409 22 3 0
d =: (a + b) - (a - b)2. Write a machine language program which is equivalent to the following J program:
b =: 3 * a + 6 % 3