Candidates should be able to:
- explain how instructions are coded as bit patterns
- explain how the computer distinguishes between instructions and data.
How are program instructions coded?
Machine code instructions are binary numbers and are coded as bit patterns, for example, a 16 bit machine code instruction could be coded as 001010101101001011.
In machine code the instructions are usually made up of 2 parts, an operator (op code) and an operand (typically a memory address). The CPU decodes the operator (for example, the bit pattern 001 could be the code for ADD) to decide what action to take with the operand.
The number of memory bits needed for each instruction (operator plus operand) is important. If only 8 bits were used then 3 bits could be used for the operation codes leaving 5 for the location in memory where the data is stored. This would however be very limiting as there could only be 8 possible operation codes and only memory addresses from 00000 to 11111 could be accessed.
A particular CPU will be designed to process a particular set of machine code instructions and will know:
- How many bits are used for each instruction.
- How many of the instruction bits are used for the operator (OP code) and how many for the operand.
The table below represents 9 memory address. Memory address 00000 holds an operator and operand (using 8 bits) and address 00100 holds some data. The CPU would follow the instruction and ADD the value in memory location 00100 to the accumulator.
|Memory address||Memory contents||Explanation|
|00000||00100100||This memory address has an instruction stored in it, made up of:
– the operator (in this case the code for ADD the contents of a memory address to the contents of the accumulator)
– the operand (in this case, the memory address of the data to be added to the accumulator).
|00100||00010101||This memory address has data stored in it (in this case the number 21 stored in binary)|
How does the computer distinguish between instructions and data?
In the Von Neumann architecture used by most computers memory locations are used to store both program instructions and data. The CPU cannot therefore distinguish between instructions and data just by reading the bit pattern stored at a memory address.
The CPU program counter should therefore always contain the memory location of an instruction. If the CPU is instead pointed to a memory address that contains data (either by mistake or because an instruction has somehow been overwritten by data) then the program would fail to run correctly because the CPU would try and interpret the data as an instruction.