1. Overview

In this tutorial, we’ll discuss two components that control the execution of the instructions in a computer: program counter and instruction register.

We’ll talk about the importance of the program counter and instruction register. Finally, we’ll explore the core differences between them.

2. Basic of the Program Counter (PC)

A program counter is basically a special purpose register in a computer. It contains the memory address or location of the instruction being executed by a CPU in the current time. As soon as the CPU finishes the execution of the current instruction, the program counter increases its value by \mathsf{1} and points to the next instruction to be executed by the OS.

A program counter contains the memory location of the next instruction. We can view a program counter as a modern digital counter. It facilitates faster execution of the instructions. Furthermore, it provides tracking of the execution points while the CPU executes the instructions.

3. Fundamentals of the Instruction Register (IR)

Modern processors can execute several instructions in parallel. An instruction register contains the actual or encoded instructions, not the addresses. It’s part of the CPU control unit.

We can find several registers in the CPU control unit like index register, data register, and instruction register. A CPU carries out several operations on the memory unit such as decode, fetch, execute based on the availability of the registers.

An instruction register is particularly important as it holds the machine instruction executed currently by a CPU. The CPU control unit gets the information regarding the machine instructions and generates the timing slots. Moreover, the timing slots control the execution of the instructions.

4. Workflow of the PC and IR

A computer program consists of a sequence of instructions. All the instructions are not the same in size. Some instructions are bigger than others:

dsafsadfdsf-1

In this example, a computer program is consists of \mathsf{3} instructions. Additionally, we can see all \mathsf{3} instructions in the computer program are different in size. The program memory stores the computer programs.

The size or width of the program memory is limited. Hence, if the width of the instructions is too big, we may need to break it and store them in separate locations in the memory:

dsafsadfdsf-3

Depending on the width of the program memory, we split the instruction \mathsf{2} into \mathsf{2} parts and instruction \mathsf{3} into \mathsf{3} parts.

A CPU contains the program counter and instruction register. At the starting, the program counter is set to \mathsf{0}:

fddhg-1

Now the CPU starts its operation. The first step is to fetch the instruction from the program memory. The fetching of instruction is based on the memory location displayed by the program counter. Initially, it’s \mathsf{000}. Hence, we’ll fetch the instruction with memory address \mathsf{000} from the program memory:

fddhg-5

Here the CPU goes into the decoding cycle and executes the instruction. After completing the execution, the CPU is ready to fetch the next instruction. Furthermore, we increase the program counter value by \mathbf{1}:

fddhg-3

Now the CPU fetches the instruction with memory address \mathsf{001} from the program memory. But the instruction is not complete. Hence, the CPU can’t execute it and goes to the decode cycle. The program counter is incremented by \mathsf{1} to proceed further:

fddhg-4

Now the CPU can execute the instruction and increment the program counter by \mathsf{1}. Similarly, we can see the instruction \mathsf{3} is stored in \mathsf{3} different addresses in memory. Hence, when the program counter is \mathsf{005}, the CPU  fetches all the parts in the instruction register and execute the instruction \mathsf{3}:

fddhg-6

5. Differences Between the PC and IR

Now we know the fundamentals of the program counter and instruction register. Let’s discuss the core differences between them:

Program Counter

Instruction Register

Holds the memory address of the next instruction to be executed.

Holds the actual instruction.

After fetching an instruction the address value is increased by 1.

After fetching a instruction, it contains the next instruction.

A CPU locates the instructions in the memory from here.

A CPU decodes and executes the instructions from here.

6. Conclusion

In this tutorial, we discussed two CPU units in a computer: program counter and instruction register. We presented a visual explanation of how these two works in a CPU unit. We also explored the core differences between them.