So far we have added the following to our computer:
At least one input device
A memory (one or more of the different types discussed before)
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 algorithmAn abstract description of a solution to a problem represented by a sequence of steps.. The algorithm is made of the following simple steps:
Read the students heights one by one
Add them one by one
Divide the final sum by the number of students to get the average
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 programA set of instructions when executed by the computer achieve a specific function/outcome..
The following is a possible program that can achieve the developed Algorithm:
A Pseudo Program for the Average Height Problem
--- First, the declaration section ---
Define the required variables to hold the different
values involved in our program:
A variable n to hold the number of students,
Another variable I to be used as a counter,
An array of n variables h(n) to hold the heights of students
A variable Average to hold the average
--- Second, the calculation section ---
Read (through an input device) the number of students n
and store it (in the memory)
Set Average to 0 and store it
Set I to 1 and store it
Read from the input device h(I) and store it
Read both h(I) and Average (from the memory)
Add h(I) to Average and store the new value of Average
Increment I (i.e. set I = I+1) and store the new value
Read n and I and check if they are equal
If they are not equal go to step 5 otherwise continue to step 12
Read n and Average
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 programA program that is stored in the memory so that it can be used repeatedly, manipulated, moved, deleted or have more instructions appended to it. 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 instructiona command that instructs the computer what to do. (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 languageThe set of all instructions (in binary form) of the computer.. This is also referred to as the instruction setThe set of all instructions (in binary form) of the computer.
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.
Machine language instructions usually are made up of several fields. Each field specifies different information for the computer. The major two fields are:
Opcode(Operation code) An instruction field that specifies the particular operation that is to be performed by the instruction. 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.
OperandsInstruction fields that specify where to get the source and destination operands for the operation specified by the opcode. 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.
MOV AX, 1
MOV AX, 2
MOV AX, 3
MOV AX, 4
MOV BX, 1
MOV CX, 1
MOV DX, 1
MOV AX, BX
MOV AX, CX
MOV AX, DX
83 C0 01
ADD AX, 1
83 C0 02
ADD AX, 2
ADD AX, BX
ADD AX, CX
03 06 0000
ADD AX, i
83 E8 01
SUB AX, 1
SUB AX, BX
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.