Laboratory using a machine simulator.
Software: An Internet Browser

In this lab you will run a program that simulates a simple computing machine. You will

The simple computer that is simulated in this exercise is very primitive, but it illustrates a number of things characteristic of most computers we actually use. Unlike real machines, this machine has only 32 memory locations. These locations are used to store both the program the computer is executing and the data for the program. Actual machines store both the program instructions and the data in memory also.

Again unlike real machines, the CPU (central processing unit) of the computer is very limited. It contains only three registers. A register is a memory location that is located on the CPU chip rather than on separate memory chips. These registers are displayed as text boxes in the simulator. The registers are the following:

PC Program Counter. This points to the next program line to execute. It is reset to 0 by the Initialize button.
IR Instruction Register. It contains the mnemonic code for the instruction that is executing.
ACC Accumulator. This is where the machine can perform arithmetic instructions, input and output.

The Program Counter keeps track of which instruction in the program is the next to be executed. It starts off at location 0, because that is where the first instruction will always be placed. After each new instruction is fetched into the CPU, it is increased by one. The Instruction Register is used to store the instruction the computer is currently working on. Each instruction in turn is copied from memory into this register.

The Accumulator accumulates the results of the arithmetic operations. All data that is read into the computer using the Input command is stored in the Accumulator. When numbers are added together, the result is placed in this register. And before any number can be displayed on the screen using the Output command, it must first appear in the Accumulator.

The CPU box also contains two buttons, one to Fetch the next instruction into the Instruction Register, and the other to Execute the command in the Instruction Register. When you click on the Fetch button, the simulator finds the instruction in the memory location given by the Program Counter and copies it into the Instruction Register. It then adds one to the number in the Program Counter.

When you click on the Execute button, the instruction in the Instruction Register is performed. If this is an ADD instruction, two numbers will be added, etc. If the instruction is either an INP (Input) or OUT (Output) instruction, the text box on the Screen will either display a message asking for an input or display the number in the Accumulator. If the instruction is INP (Input), you should type a number into the Keyboard box and then click the Enter button. Do not use the actual Enter key on your own keyboard.

Choose and click on an instruction from the instruction list at the bottom right of the form. If it is one of the instructions that requires an address, type the address into the box labeled Address. Then click on the Enter Instruction button. After you enter the HLT instruction, the list of instructions will disappear. To see the list again, click on the Load Program button.

The program that you enter will be stored in the computer’s memory, beginning at location 0. It is important that each program contain a HLT (halt) instruction. In an actual computer, this instruction tells the computer to stop running the program.

The instructions for the machine are listed below. There are two kinds. The first six only use the Accumulator and not the memory of the computer. They perform operations such as input, output, and storing either a zero or a one in the Accumulator. The last seven instructions use both the Accumulator and one of the memory locations. In the definition of the instruction, the letter A (for address) indicates the memory location. For example, to store the number in the Accumulator into memory location 30, use the instruction, STO 30.
Instructions using only the CPU and not the memory.
CLR Clear the accumulator (store a zero in it)
ONE Store the number 1 in the accumulator
NEG Negate the contents of the accumulator (multiply the contents by minus 1)
INP Input a number into the accumulator
OUT Output a number from the accumulator
HLT Halt
Instructions referring to a memory location, idicated by the letter A.
LOD A Load number in A into accumulator
STO A Store number in accumulator in A
ADD A Add number in A to accumulator
MUL A Multiply the number in accumulator by the number in A
DIV A Divide accumulator by number in A
BRC A If accumulator is clear, branch to the instruction in A
BRP A If the number in the accumulator is positive, branch to the instruction in A

You can now run the following programs using the simulator.

1. The first is a program to read in two numbers, add them together, and output the result. Note that you do not have to store the second number because the ADD instruction will add the number in location 31 to the number in the accumulator.
INP Input first number to accumulator
STO 31 Store number in accumulator in location 31
INP Input the second number into accumulator
ADD 31 Add the first number to the number in the accumulator
OUT Output the sum from the accumulator
HLT Halt

2. The second program inputs a number and outputs its negative. (Note that the negative of a number is what you get by multiplying it by -1.)
INP Input the number
NEG Negate the number
OUT Output the result
HLT Halt

3. The third program inputs two numbers and subtracts the second from the first. Since this machine does not have a subtraction operation, we will first have to find the negation of the second number and then add the two numbers together.
INP Input the first number
STO 31 Store it in location 31
INP Input the second number
NEG Negate the second number
ADD 31 Add the first number to the negation of the second
OUT Output the result
HLT Halt

4. The next program uses the branching instructions. These are instructions that skip to another instruction instead of executing the instruction immediately following. This program accepts two numbers as input. If the first is larger, it outputs a +1, otherwise it outputs a 0.

BRP 11 (branch if positive to address 11) means that if the number in the accumulator is positive, the program will skip to location 11. In all other cases, for example if the accumulator is 0 or negative, the next instruction will be the one that immediately follows the CLR instruction. The number that follows the BRP is the address of the instruction to perform next if the accumulator is positive. Note that this is not the same as the line number, since the instructions start in location 0, not location 1. The same is true for the BRC 13 (branch if clear to address 13) instruction. If the accumulator is clear (zero), then skip to location 13, otherwise continue to the following instruction. Memory locations 11 and 13 are indicated in the list of instructions.

INP Input the first number
STO 30 Store it in location 30
INP Input the second number
NEG Negate the second number
STO 31 Store the negation in location 31
LOD 30 Load first number into the accumulator
ADD 31 Add the negation to the first number
BRP 11 If accumulator is positive, branch to the instruction in location 11
CLR Clear the accumulator
OUT Output the zero in the accumulator
BRC 13 If accumulator is clear, branch to the instruction in location 13
11: ONE Load the number one into the accumulator
OUT Output the one in the accumulator
13: HLT Halt

5. The last program reads in numbers again and again and adds them into a running sum. This program will not stop running until a zero is entered. This is an example of a program loop, because it continues to loop around performing the same instructions over and over again. When a zero is entered as input, the program exits the loop, outputs the sum, and ends execution. As it accumulates, the sum is stored in location 31.

CLR Clear the accumulator
STO 31 Store the zero in location 31 (sum)
2: INP Input a number
BRC 8 If the number is zero, branch to the instruction in location 8
ADD 31 Add the sum to the new number
STO 31 Store the new sum in 31
CLR Clear the accumulator
BRC 2 The accumulator is clear, so branch to the instruction in location 2
8: LOD 31 Load the sum into the accumulator
OUT Output the sum
13: HLT Halt

6. Run the following three programs and explain what they do. The Memory Location on the left shows where the instruction will be stored in the computer’s memory.
Memory Location
Program 1   Program 2   Program 3
0
INP   INP   INP
1
STO 30   STO 30   STO 31
2
INP   MUL 30   INP
3
STO 31 OUT BRC 8
4
INP   HLT   STO 30
5
ADD 30       LOD 31
6
ADD 31       DIV 30
7
OUT       OUT
8
HLT       HLT
 

7. Write a program to input two numbers, multiply them and output the result.

8. Write a program to input two numbers. If they are equal, your program should output a zero. If they are not equal, it should output +1.

Summary: What you have learned and done.

You ran a simulation program that demonstrated what is going on inside of a very simple machine. The actual machines that we use have many of the same parts, only they are much larger and many times more complicated. But they all store both the programs that you are working on and the data for those programs in computer memory. This is called von Neumann architecture, after John von Neumann, a very famous mathematician and scientist, who suggested this organization in the 1940s. There are some machines that are not organized this way, but they account for only a very small percentage of computers in existence.

You ran several sample programs and then created several more. The computer commands used by this machine are a simplified version of Assembly Language. Programming on this level is quite awkward and programs end up being hard to read and understand. High level languages such as C++ and Visual Basic were developed in the years following 1957. They have made it much easier to tell computers what to do. But programs written in these languages must be translated into the low level instructions which the computer itself can understand. Each assembly language instruction, in turn, must be translated into a sequence of bits, zeroes and ones. Only after all this is done can the computer actually run the program.