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 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:
In this example, a computer program is consists of instructions. Additionally, we can see all 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:
Depending on the width of the program memory, we split the instruction into parts and instruction into parts.
A CPU contains the program counter and instruction register. At the starting, the program counter is set to :
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 . Hence, we’ll fetch the instruction with memory address from the program memory:
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 :
Now the CPU fetches the instruction with memory address 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 to proceed further:
Now the CPU can execute the instruction and increment the program counter by . Similarly, we can see the instruction is stored in different addresses in memory. Hence, when the program counter is , the CPU fetches all the parts in the instruction register and execute the instruction :
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:
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.