In This Article
What is Program Counter?
A program counter refers to the specific type of register in the processor that mainly holds the address of the subsequent instructions that are to be executed. When an instruction is fetched, it points to the following instruction in the cycle.
Technically, it is a register or a digital counter in a computer processor which increases the stored value by 1 after every instruction is fetched from it by the CPU. When the computer is reset or restarted, the counter returns to 0.
- A program counter is a special purpose digital register in the computer processor that holds the memory location or address of the following instruction to be carried out.
- It increases the store value by 1 when the current instruction is completed to point to the subsequent instruction to be fetched by the operating system.
- The program counter improves the execution time of instructions and also helps track the current execution point when the instruction is carried out by the CPU.
- The width of the program counter is indicated by the number of bits, which, however, depends on the architecture of the processor.
- The design and function of the counter helps in protecting and maintaining a balance between the resources and forward progression of the instructions to ensure a superior level of efficiency.
Understanding Program Counter
All the data used in a computer and its memory have a particular address.
This is the address that is stored in the program counter and the software updates this address when an instruction is executed.
This ensures that the address of the following instruction to be fetched is readily available.
The program counter actually passes the information to the memory address register.
This is a part of the standard fetch operation, execution cycle, or the instruction sequencer which indicates the program sequence.
The CPU then reads the stored address to find the particular instruction to be carried out from the counter and performs it in sequence.
The stored value of the program counter changes continuously. It is increased by 1 when an instruction is fetched and reverts to 0 when the system restarts.
A program counter is also known by different names such as:
- An instruction counter
- An instruction pointer or IP in Intel x86 and Itanium processors
- An instruction address register or IAR
- A sequence control register
From a technical point of view, a program counter is pretty much the same as the other process registers of a computer.
It acts as a store of binary latches wherein each of them is represented by one bit of value.
It usually works in tandem with other process registers that identify a page or a memory segment in order to make out the present instruction.
This allows the program counter to work with fewer bits and assume that the majority of the memory elements of interest are within the current environs.
It can be accessed, modified, or loaded by using access, branch or jump instructions.
Loading can also be done using the address with the help of data processing instructions.
When the processor fetches an instruction from memory, as it is supposed to, the sequence of the control transfer instructions is changed with the new value placed on the program counter. These include:
- Branches, also called jumps
- Subroutine calls
The program counter is modified at some point in the cycle just to ensure that the following instruction is carried out is a different one.
It is also done to ensure that this instruction is available at the beginning of the memory address and is the one that immediately follows the last memory address of the current instruction.
As a binary counter, the value increases when a pulse is applied to the COUNT UP input.
It may also change when the CPU computes a different value and loads it into the counter by applying a pulse to its LOAD input.
Typically, the program counter will specify a memory address as +n every time an instruction is carried out. For example:
- +1 represents a 1-word instruction
- +2 represents a 2-word instruction and so on.
However, it will store the jump destination address if it is an interrupt instruction.
Program Counter Functions
The primary function of the program counter is to receive each instruction and store it sequentially so that when these instructions are fetched by the CPU, it points at the next instruction when the current one is done fetching. This ensures efficient moving of instructions forward.
Typically, the functions of the program counter can be summarized as follows:
- It serves the purpose of a temporary housing of the address of the next instruction to be executed as a part of a string of instructions.
- It queues up the subsequent instruction in the string when one instruction is fetched to ensure a logical progression of instructions.
- It effectively minimizes the delay in execution by maintaining the sequence of all necessary instructions to complete a particular task.
The program counter points to the next instruction to be used even when the previous instruction is being executed and then points to another when the next one is, and so on. This ensures a quick and logical progression of the instructions.
However, depending on the configuration of the counter, it may even point at more than one instruction that may be related to the current instruction under processing.
And, in some designs, the program counter may point to an instruction that is next in the sequence but also, along with it, point to the one that has been just released and is currently at the point of being implemented.
Whatever it is, the function of the program counter is essential for a proper and faster execution of instructions.
Without it, the chances of the sequence being disrupted will be higher, which will, in turn, reduce the chances of the task being completed, if at all.
The counter acts as the conduit that diminishes the anomalies that may prevent the sequence from moving forward in the right manner.
What is a Program Counter in OS?
The program counter in an operating system implements a specific mechanism which ensures that the operating system has the necessary structures so that it does not face any issues in keeping track of the status of all the processes.
One of the most significant values that the operating system needs for a specific process is available in the program counter value.
Ideally, the structure of the current value of the program counter for the process indicates where exactly the process is running currently in the program code.
Most modern operating systems support multitasking which essentially means that they can run several programs at the same time.
However, the number of tasks that can be executed depends on the number of cores or processors. If there are only one or two, there is no way in which you can execute multiple tasks concurrently.
However, it can be achieved if only an illusion can be created that more than one instruction is being executed by only one processor at the same time.
This can be accomplished if the operating system switches between the running and the to-be-executed processes very quickly.
This is how they can do it:
- They proceed with one process
- Then pause it and
- Then they proceed with some other process, and so on.
However, all of these should be done in less than a fraction of a second. This is facilitated by the sequential logic progression technique followed by the program counter.
The operating system can also virtually create program counters for every process so that it can control the flow of the processes with the help of these counters working with other process registers.
This is called scheduling in terms of the operating system.
Where is the Program Counter Stored?
In simple words, it is quite difficult to answer this question. It all depends on the system design and architecture as well as the needs related to the execution of the instructions.
It can be stored in the CPU hardware, in the Reorder Buffer, in the instruction retirement unit or simply as a general purpose register.
Normally, a program counter, being a CPU register, is expected to be stored in the CPU hardware. Well, in most cases, a program counter is a register stored there.
Ideally, the program counters in the modern CPUs are not like other typical registers and therefore there is no one place where they are really stored. These are:
Instruction Retirement Unit:
Apart from the CPU hardware, another place where the program counter can be stored is in the instruction retirement unit.
It is decoupled from the instructions that are being fetched and carried out. The programs see it without any speculation.
This is the third place where the program counter may be stored. This is quite convenient to store the program counter in the reorder buffer for each instruction along with the instruction.
In this case, the whole program counter may not be needed to store. A part of it which is enough to reconstruct the program counter according to the need can be stored.
General Purpose Register:
The program counter may be stored as a general purpose register, especially those instructions and codes that are not position dependent and need the value of the program counter for addressing the memory.
Here, transient copies are stored in the reservation station entries which the instructions use.
Is There a Program Counter in the Control Unit?
Yes, if the system is a traditional von Neumann architecture, there will be a program counter in the control unit, along with an address register as well as a register that stores and decodes the operation codes.
The address register and the other in this case are collectively called the instruction register.
However, you cannot call a program counter a control unit because these two are two different things altogether. For example:
- Program counter is an index register and its size depends on the architecture. It simply holds the value just like any other register but does not control any operation.
- It simply points towards the subsequent instruction that is to be executed in the code segment but does not generate any control signals as the control unit.
- The counter is just a part of the control unit and not the address memory that contains the instructions or addresses it or examines the fetched data.
In short, it is not a piece of circuitry like the control unit that controls the other parts of the CPU and directs operations being performed within it depending on the input instruction.
It basically controls different multiplexors that help the program counter to choose the correct function and decide that the value is incremented, based on the available options and input branch instruction.
Stack Pointer vs Program Counter
- The stack pointer is a register that holds the last program request address in the stack, but, in comparison, the program counter is the register that holds the address of the subsequent instruction to be carried out from the memory.
- The stack pointer CPU register keeps track of a call stack, but, in comparison, the program counter CPU register indicates where the computer is in the program sequence.
- The stack pointer keeps a track of the operations of the stack but the program counter keeps a track of the current execution point.
- The stack pointer signifies the data but the program counter signifies the code.
What are Program Counter Operation Examples?
One example of program counter operation For example, when you switch on your computer, a decimal number F000 is placed into the CPU by a signal.
This signal tells the CPU to look for the first instruction on the flash memory chip on the motherboard.
And, an address stored as 0x8020 in the program counter signifies that the processor is executing the instruction at the address 0x8019, and so on.
What is the Use of Program Counter Register?
A program counter is used as a CPU register that holds the memory address of the instruction to be executed next.
It expedites the overall process and is also used to track the execution points of the instructions.
What is the Size of Program Counter?
The width or size of the program counter is measured by the number of bits or latches. This size however is dependent on the CPU architecture.
Typically, it is a CPU register measuring 16 bits. This provides 16 lines, which is enough to store any memory address.
The program counter stores the address of the next instruction of a program that needs to be executed.
Holding all the instructions in a proper sequence, it helps with logical progression.