1.4 Representation of data in computer systems Instructions
How are program instructions coded? The CPU can only process instructions that are in machine code, a series of binary digits. This means all other programming languages have to be translated into machine code to actually be executed. In the case of high level languages this translation is carried out by a compiler and in the case of low level assembly language the translation is carried out by an assembler.machine codehigh level languagesassembly language In machine code the instructions are usually made up of 2 parts, an operation code and a memory address. The CPU uses the operation code (for example, the bit pattern 001 could be the code for ADD) to decide what action to take with the second part, which is usually a memory location holding stored data.
How are program instructions coded? The number of memory bits needed for each instruction (operation codes plus memory address) 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 be very limiting as there could only be 8 possible operation codes and only memory addresses from to 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 operation code (OP code) and how many for the memory address.
How are program instructions coded? The table below represents 9 memory address. Address holds an instruction (using 8 bits) and address holds some data. The CPU would follow the instruction and ADD the value in memory location to the accumulator. accumulator Memory address Memory contentsExplanation This memory address has an instruction stored in it, made up of: - the operator (in this case the code for ADD) - the memory address of the data to be added to the accumulator 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 contents of a memory address. However, when a program is actually running the CPU does distinguish between them, based on which part of the fetch-execute cycle the memory address is accessed.fetch-execute cycle In the fetch part of the cycle the contents of the memory address will be treated as an instruction and copied into the instruction register.instruction register In the execute part of the cycle the contents of the memory address will be treated as data, either being read into one of the general purpose registers such as the accumulator, or being written to with the contents of such a register.accumulator 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.program counter