Back

Instructions as coded bit patterns

Introduction
All Central Processing Units (CPU) use programs made up of machine code instructions, which are patterns of ones and zeros. These programs are stored in Random Access Memory (or RAM for short). The CPU fetches an instruction, decodes it and then executes it, one instruction at a time. When you write any program using a programming language, it has to be translated first into machine code so that the CPU can understand and run the program. The code you write is known as source code. It is then 'translated' into object code, which the CPU can understand.

A typical instruction
A machine code is made up of two parts, an 'opcode' and an 'operand'.

    • The opcode holds the instruction, what you actually want to do (e.g. Add some data, subtract some data, store some data and so on).
    • The operand holds the address in RAM of the data you want to work with.

A typical machine code instruction has a fixed length. The length of the code depends on the CPU you are using. If your CPU used codes that were 8 bits long then it might use 3 bits for the opcode and 5 bits for the operand. Now if you allow 3 bits for the opcode, that means that there are 8 different patterns available (23) : 000, 001, 010, 011, 100, 101, 110 and 111. Each one of those codes could be allocated to a job e.g. 000 could mean Add, 010 could mean Subtract and so on. This wouldn't give you a wide range of instructions. In addition, you would only be able to address 25 or 32 different memory locations.

If the CPU used 24 bits per instruction, and split it 6 bits for the opcode and 18 for the address, then it would improve things. You would have 64 different opcodes and could address up to 218 different memory locations in RAM.

Mnemonics
So far, I have referred to instructions using words like Add and Subtract and Store. We know that in practice, the computer system doesn't store opcodes using words. It stores the opcodes as patterns of ones and zeros and these are stored in RAM. However, when we want to document our program (so we can modify it or correct bugs in the future) we don't want to be writing down binary patterns. When we want to write programs, we don't want to write in binary or hex. We would make mistakes and it would be very difficult to read and understand. We therefore use 'mnemonics'.

A mnemonic is a code for an operation that we can easily remember. For example, we could use SUB for the opcode that subtracts data. We could use ADC for the opcode that Adds with a Carry. NOP could mean No Operation. By using mnemonics, we can write assembly programs more easily and which are more understandable than if they were written in binary, and we can document our programs and follow them a lot easier compared to using binary.

Back