Lecture Notes

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

2 Computer Organization

2.1 Processors and Memory

There are two main components which have been part of nearly all computer systems ever designed and built. The first is called the processor (known also as the Central Processing Unit or CPU)and the second is called the memory. The processor is the part of a computer system which does the actual computing. That is, the part which adds, subtracts, multiplies and divides. Most processors can also compare values and perform conditional actions as a result of such comparisons. Many processors have instructions which perform various types of conversions between different representations of data.

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:

2.1.1 Processor Registers

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.

2.1.2 Instruction format

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.

2.2 Two Simple Programs

Perhaps the shortest useful program we might write would move a value from one memory cell to another. This could be modeled by the J expression:
q =: r
To 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   q
Consider a program which computes the following expression:
c =: a + b
load    a
add     b
store   c
It 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

is called a J compiler.

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   c
The 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           9
The final translated program then looks like:

2.3 Machine Language Program

location    operation       address
  04          03              07
  05          01              08
  06          04              09
A program which translates from the symbolic form for a program
load    a
add     b
store   c
to the machine language form is called a symbolic assembler, or simply an assembler.

2.4 How the Computer Executes a Program

We next focus on how a computer executes such a program. Recall the organization of the processor:

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.

2.4.1 Processor Flow Chart

2.4.2 Executing a Program

To illustrate the various steps in the processor flow chart we give the following trace of the execution of Program.

Step 0 Set initial value for the pc


pc	04
ir ?
acc ?
Step 1 (fetch)

pc	04
ir 0307
ac ?
Step 2 (increment pc)

pc	05
ir 0307
ac ?
Step 3 (execute)

pc	05
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)
Step 4 (fetch)

pc	05
ir 0108
acc 22
Step 5 (increment pc)

pc	06
ir 0108
acc 22
Step 6 (execute)

pc	06
ir 0108
acc 25
Step 7 (fetch)

pc	06
ir 0409
acc 25
Step 8 (increment pc)

pc	07
ir 0409
acc 25
Step 9 (execute)

pc	07
ir 0409
acc 25 (contents of 09 changed to 25)
Step 10 (fetch)

pc	07
ir 0022
acc 25
Step 11 (increment pc)

pc	08
ir 0022
acc 25
Step 12 (execute)

pc	08
ir 0022
acc 25
Of course, at this point we have a problem since the ir does not contain an instruction.

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.

2.5 A J Model of the Computer.

The following J definitions provide models of memory systems and processors described in Section 2.1. These definitions are useful in that they provide a more precise description of the model computer than is possible using the English language and, in addition, the provide a working model that can be examined, programmed and forms the basis for experiments.

2.5.1 The Memory Model

In this section, and following sections, we assume the following definitions exist:

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 0
We 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 4
To store the value 10 in cell 3 you would write:
store 3 10
To see the contents of all of the memory system write:
mem

2.5.2 The Processor Model

Next, we model the processor registers as:

pc =: 0
ir =: 0
ac =: 0
Following 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.

2.5.3 A J Session Using the Computer Model

   
   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
8
Now 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 25
To 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

2.6 Exercises

1. Write a machine language program which is equivalent to the following J program:

d =: (a + b) - (a - b)
2. Write a machine language program which is equivalent to the following J program:

b =: 3 * a + 6 % 3