Barry J. Stern's Home Page

Indianapolis, Indiana

Email: bjstern2@comcast.net

Click Below To Download this Computer Simulated in Visual Basic or Logisim:
Visual Basic Version

Logisim Version

Project: Computer

             

Introduction

The above Java Applet simulates a simple computer consisting of 32 8 bit memory words. The computer operates by fetching a word in memory pointed to by the Program Counter (PC) into the Instruction Register (IR)and then executing this instruction. The Program Counter is then incremented by one and the process is repeated until a Halt instruction is fetched at which time the computer stops and the binary number in the Accumulator (ACC)can be read.

For a more sophisticated Java Applet running Focal click on

PDP-8

Demonstration of Generating Prime Numbers

To demonstrate the operation of the Computer load the program Prime into the computer by selecting Prime in the List Box and depressing Load. Now press Start. The computer will halt with the number 2 in the ACC register. Subsequent pressing of Start will result in the computer halting with the number 3 , 5, 7, 11,13, 17,..... etc. in the ACC Register.

Simple Program

The operation of the computer can be illustrated by writing a program to compute A+B-C. The expression can be decomposed into the operations of loading A into the accumulator, then adding B to the accumulator, then subtracting C and then Halting the computer and examining the contents of the Accumulator. These operations can then be arranged into a table of four columns with each row representing an operation. The first column is a label for the line.The second column holds the operation. The third column holds the operand, and the fourth column comments.
Location Operation Operand Comment
START LDA A Load Accumulator
ADD B Add B to the Accumulator
SUB C Sub C from the Accumulator
HLT Halt the Computer
JMP START Branch to START
A DB 4 Initialize A to 4
B DB 3 Initialize B to 3
C DB 2 Initialize C to 2

Translating the program to Machine Code

In order for the above program to run it must be translated into machine code. In order to perform this task a Starting Location in memory in which the program must reside must be chosen. Since it is easy to clear the Program Counter location 0 will be chosen.

The next task is to assign each subsequent line with a memory location. Since each operation and operand is packed in one byte each line becomes the previous line plus one. The above table will now appear as follows:
Location Operation Operand Comment
00000 LDA A Load Accumulator
00001 ADD B Add B to the Accumulator
00010 SUB C Sub C from the Accumulator
00011 HLT Halt the Computer
00100 JMP START Branch to START
00101 DB 4 Initialize A to 4
00110 DB 3 Initialize B to 3
00111 DB 2 Initialize C to 2
At this point a Symbol Table can be generated which gives the Memory Location (ML) of each symbol.
Symbol ML
START 00000
A 00101
B 00110
C 00111
The operation codes for each instruction is contained in the following table.
Operation OpCode Operand Comment
HLT 000 XXXXX Halts the fetch & execute cycle
LDA 001 XXXXX Loads ML XXXXX into the ACC
ADD 010 XXXXX Clears C & Adds ML XXXXX to the ACC
STO 011 XXXXX Stores the ACC in ML XXXXX
SUB 100 XXXXX Sets C and Subtracts ML XXXXX from the ACC
BNZ 101 XXXXX Branches to XXXXX if the ACC is not zero
BNC 110 XXXXX Branches to XXXXX if the C is equal to 0
JMP 111 XXXXX Branches to XXXXX
The final step is to replace each symbolic code and operand with its machine code as listed in the above two tables. Note that DB is a directive which reserves a Memory Location for the symbol and initializes this location to any binary number from 0 to 255 .

The final machine code for the program is in the following Table.
Location Operation Operand Comment
00000 001 00101 Load accumulator with Location 5
00001 010 00110 Add Location 6 to the Accumulator
00010 100 00111 Sub Location 7 from the Accumulator
00011 000 00000 Halt the Computer
00100 111 00000 Branch to Location 0
00101 000 00100 Value of A = 4 in this Location 5
00110 000 00011 Value of B =3 in this Location 6
00111 000 00010 Value of B =2 in this Location 7

Loading the Program into the Computer

The above machine code can be loaded into the computer by following the following steps. Note that any bit in any register can be toggled by clicking on it and all bits in a register may be cleared by pushing the Clear (C) button associated with each register.
  1. Clear the Program Counter by clicking on the C button.
  2. Enter the Binary number 00100101 in the accumulator.
  3. Depress the Write Button.
  4. Check to see that the accumulator is cleared and the program counter has increased by one.
  5. Repeat steps 2,3 & 4 for each line of the program.

Checking the Program in Memory

It is always a good idea to read back and check the program before running the program. To perform this task execute the following steps.
  1. Clear the Program Counter .
  2. Depress the Read Button.
  3. Check to see that the accumulator is 00100101 and the program counter has increased by one.
  4. Repeat steps 2,3 for each line of the program.

Correction of an Error in a Location

In the event you discover that a particular memory location is in error it can be changed without reloading other Locations.
  1. Place the address of the location in the Program Counter.
  2. Place the correct word in the accumulator.
  3. Depress the Write Button.

Running the program

After loading and checking the code it is time to run the code.
  1. Clear the Program Counter .
  2. Depress the Start Button.
  3. The Accumulator should read 00000101, i.e 5
  4. The Instruction Register should be cleared.
  5. The program Counter should read 00000100, i.e 4
  6. The program can be run again by simply pressing Start.

Single Stepping the Program

In the event that after running this program the accumulator did not have the correct answer it would be necessary to debug the program. As an aid to this process is a Single Step Mode for the computer. This mode fetches and executes one instruction and then Halts so all registers in the computer can be examined. To run the program in Single Step Mode:
  1. Clear the Program Counter .
  2. Depress the Step Button.
  3. The Accumulator should read 00000100, i.e 4, the PC 00001, and the IR 00100101.
  4. Repeat Step 2 & 3 and verify each line of the program.

Halting a Running Program

In the event that due to a programming error that results in an endless loop or one simply desires to end a long running program the solution is to depress the Halt Button.

More Advanced Program

This Program will calculate the factors of a number. One enters the number 0-255 into the accumulator and depresses Start. When the computer halts the number in the accumulator will represent the largest number that can be divided into the original number evenly. A subsequent push of the start Button will result in the next largest number. This will continue until the accumulator will read 1. If you now press start you will enter an endless loop, i.e. trying to divide by zero!

The algorithm is simply trying to divide the original number N by a number F. If as a result of this operation there is no remainder, then F is a factor. F will initially be initialized to N-1 and will be decremented after each failed test as a factor. Here is the program:
Location Operation Operand Comment
START STO N The Original Number N
LP0 SUB ONE Initialize F to the factor to try
STO F Store Trial Factor
LDA N Begin Trial Division
LP1 SUB F By successive subtraction
BNC LP2 Branch if Result of subtraction is negative
BNZ LP1 Branch for another Subtraction if remainder > 0
LDA F Bring Back F to Display
HLT Halt with Factor in ACC
LP2 LDA F Bring Back F to Find next Factor
JMP LP0 Return to Loop
ONE DB 1 Initialize One to 1
N DB Reserve this Byte for N
F DB Reserve this Byte for F
The Symbol Table for this program:
Symbol ML
START 00000
LP0 00001
LP1 00100
LP2 01001
ONE 01011
N 01100
F 01101
The Machine Code for this Program: .
Location Operation Operand
00000 011 01100
00001 100 01011
00010 011 01101
00011 001 01100
00100 100 01101
00101 110 01001
00110 101 00100
00111 001 01101
01000 000 00000
01001 001 01101
01010 111 00001
01011 000 00001

Logic Simulators

Windows

Java

Dos

Linux