The Stored Program 

The Algorithm

So far we have added the following to our computer:
  1. At least one input device
  2. A memory (one or more of the different types discussed before)
  3. An ALU.

Now we need to define the set of necessary steps and their proper sequence that are required to obtain the average height from the set of input data. These steps would make what is called an algorithm . The algorithm is made of the following simple steps:

  1. Read the students heights one by one
  2. Add them one by one
  3. Divide the final sum by the number of students to get the average

The Program

The above Algorithm is an abstract description of the solution, i.e. it does not show details on how our digital system can actually perform these steps. We need to write a more detailed description of the solutions steps, this would be the program .

The following is a possible program that can achieve the developed Algorithm:

A Pseudo Program for the Average Height Problem
    --- First, the declaration section ---
  1. Define the required variables to hold the different values involved in our program:
    1. A variable n to hold the number of students,
    2. Another variable I to be used as a counter,
    3. An array of n variables h(n) to hold the heights of students
    4. A variable Average to hold the average
    --- Second, the calculation section ---
  2. Read (through an input device) the number of students n and store it (in the memory)
  3. Set Average to 0 and store it
  4. Set I to 1 and store it
  5. Read from the input device h(I) and store it
  6. Read both h(I) and Average (from the memory)
  7. Add h(I) to Average and store the new value of Average
  8. Read I
  9. Increment I (i.e. set I = I+1) and store the new value
  10. Read n and I and check if they are equal
  11. If they are not equal go to step 5 otherwise continue to step 12
  12. Read n and Average
  13. Set Average = Average / n and store the new value. This value now contains the final answer.

This program can be stored in the memory so that it can be used over and over. In Fact, the program can be manipulated, moved, deleted or have more appended to it in the memory. This is the concept of the stored program and it is the basis for all modern computers.

So the memory contains both data and instructions. These are the two types of information handled by computers.

Instructions and Machine Language

Each command of the program is called an instruction (it instructs the computer what to do)
Computers only deal with binary data, hence the instructions must be in binary format (0s and 1s)
The set of all instructions (in binary form) makes up the computer's machine language . This is also referred to as the instruction set
Hence, the program above has to be translated to the computer's machine language before it is stored in the memory
A machine language program written for one type of computers, say a Sun-Ultra 20 machine wont work with another type of computers say an IBM-compatible PC.


The computer breaks down machine instructions into smaller operations that are called micro-operations
These micro-operations are performed by the computer automatically (i.e. the programmer does not specify them)
In the program above, fetching the operands, adding them and then writing back the result to the memory are examples of micro-operations. In fact some of the above instructions will combine into a single machine instruction. For example instructions 6 and 7 will be combined as an ADD instruction.

Instruction Fields

Machine language instructions usually are made up of several fields. Each field specifies different information for the computer. The major two fields are:
Opcode field which stands for operation code and it specifies the particular operation that is to be performed. Each operation has its unique opcode and may take the computer several micro-operations to accomplish.
Operands fields which specify where to get the source and destination operands for the operation specified by the opcode. The source/destination of operands can be the memory or one of the general-purpose registers.

The figure below (Fig. m010114.1) shows the correspondance between several assembly instructions and their machine code equeivellant.

Fig. m010114.1 Correspondence between Assembly and Machine language instructions showing the opcodes and operands

The following table shows a number of assembly instructions and their corresponding machine code. The first column shows the address at which the instruction is stored, the second column shows the machine code for the instruction and the third column shows the corresponding assembly instruction.

Instruction AddressMachine CodeAssembly Instruction
0005 B8 0001 MOV AX, 1
0008 B8 0002 MOV AX, 2
000B B8 0003 MOV AX, 3
000E B8 0004 MOV AX, 4
0011 BB 0001 MOV BX, 1
0014 B9 0001 MOV CX, 1
0017 BA 0001 MOV DX, 1
001A 8B C3 MOV AX, BX
001C 8B C1 MOV AX, CX
001E 8B C2 MOV AX, DX
0020 83 C0 01 ADD AX, 1
0023 83 C0 02 ADD AX, 2
0026 03 C3 ADD AX, BX
0028 03 C1 ADD AX, CX
002A 03 06 0000 ADD AX, i
002E 83 E8 01 SUB AX, 1
0031 2B C3 SUB AX, BX
0033 05 1234 ADD AX, 1234h

As can be see from the table, the instruction sizes are not the same. Some instructions have 2 bytes, some 3 and some 4 bytes. For example, you can see that the instruction MOV AX, 1 is stored at address 0005. Since the size of this instruction is 3 bytes, the next instruction will be stored at address 0005+3=0008. As you can see, for the first seven instructions the first byte encodes the opcode and also the code for the register. The last two bytes encode the constant. Since the register is 16 bits, the constant size is also 16 bits and this is why 2 bytes are allocated. It can be observed that the least significant 2 bits are encoding the register.

You can observe that when both the operands are registers that the instruction size is 2 bytes. Note the difference between the machine code for the instructions MOV AX, 1 and ADD AX, 1. Also, note that for the instruction ADD AX, i two bytes are allocated to store the address of variable i which is in this example 0000. In 8086 machines, an address requires 2 bytes. In order for one to completely understand the mapping between assembly instructions and machine code, it is required to know the various instructions formats and the syntax and semantics of each format.