A recipe tells the cook what to do and with what ingredient. A computer instruction does the same: it tells the processor which operation to perform and where to find the data. The way this information is packed into bits is called the instruction code.
Instruction Code
An instruction code is a group of bits that tells the computer to perform a specific operation. It has an operation code (opcode) that names the operation and an address that tells where the operand is held in memory. In the basic computer the memory holds 4096 words of 16 bits, so 12 bits are needed for the address. The remaining bits hold the opcode and a mode bit I. If I = 0 the address is direct, and if I = 1 it is indirect.
Definition: Instruction code. A binary word that specifies an operation, normally together with the location of the operand, in a form the computer can decode and execute.
Computer Registers
Registers are fast storage places inside the CPU. Table 3.1 lists the registers of the basic computer. The program counter holds the address of the next instruction. The address register supplies the memory address, and the instruction register holds the instruction that is being executed. The accumulator is the main register for arithmetic, and the data register holds the operand read from memory.
Table 3.1: Registers of the basic computer
| Register | Bits | Function |
|---|---|---|
| PC | 12 | Holds the address of the next instruction |
| AR | 12 | Holds the address for memory access |
| IR | 16 | Holds the instruction being executed |
| DR | 16 | Holds an operand read from memory |
| AC | 16 | Accumulator, the main working register |
| TR | 16 | Holds temporary data |
| INPR | 8 | Holds a character from the input device |
| OUTR | 8 | Holds a character for the output device |
Direct and Indirect Addressing
With a direct address the operand is stored at the address given in the instruction. With an indirect address, the given location holds the address of the operand. Indirect addressing allows an address to be changed without changing the instruction.
Example 3.1: Reading an instruction word
Problem: The 16-bit instruction 1001 0000 0000 0101 is read from memory. Identify the mode, opcode and address.
Solution:
- The leftmost bit is I = 1, so the address is indirect.
- The next three bits are 001, which is the ADD operation.
- The last twelve bits are 0000 0000 0101, which is address 5.
Result: The instruction is an indirect ADD with address 5. The operand is found at the address stored in location 5.
word = 0b1001000000000101
print(word >> 15, (word >> 12) & 7, word & 0xFFF)
print(hex(word))
Output
1 1 5
0x9005
Common mistakes
- Treating the address field as the operand itself when I = 1.
- Mixing up the PC, which holds the next address, and the IR, which holds the current instruction.
- Forgetting that the AR and PC have 12 bits because memory has 4096 words.
Key takeaways
- An instruction has a mode bit, a three-bit opcode and a 12-bit address.
- The PC holds the address of the next instruction.
- The AC is the main register for arithmetic and logic.
- Indirect addressing gives the address of the address of the operand.
Practice questions
- How many bits does the address field need for 4096 words?
- What does the program counter hold?
- What does the mode bit I indicate?
Answers to the practice questions
- Twelve.
- The address of the next instruction.
- Whether the address is direct (0) or indirect (1).
Sources
- Official BCA syllabus Computer Architecture, course code 25BCA401DS02, Unit III
