I think it stores the address of the current instruction. And if this instruction is completed the program counter is incremented by 1, to get the next instruction. But now my question is, how do you increment the program counter by 1? It would mean you increment an address by 1, how does this work?
Assuming a 32-bit architecture (with alignment), then instructions are words; that is, every instruction is composed of 4 bytes.
That implies that the bottom two bits of the address are byte offsets within the word; that is, the address 10001111 denotes 4th byte of the word at 10001100. When we're talking about instructions, however, we'll never need to fetch individual bytes -- we want whole words.
Different textbooks handle the program counter differently, but given that whichever paradigm you're using has the program counter incremented by 1 (rather than 4), that leads me to believe that the PC stores word address, rather than byte address.
Then, if the program counter is at 10001111 and we increment by 1, we do in fact get 10010000. To turn this word address into a byte address (so we can fetch the instruction from memory), we can simply left-shift by 4.
In short, if the program counter is at 10001111 (the word address), then the byte address of the instruction we'll actually be fetching is 1000111100. To fetch the next instruction, we can just increment the program counter by 1.
-
1$\begingroup$ "every instruction is composed of 4 bytes" -- careful; cf RISC vs CISC. $\endgroup$ – Raphael♦ Dec 6 '14 at 8:45
The program counter indicates the memory address of the current instruction. Depending on the architecture of the CPU it may be incremented by a fixed or a variable amount in order to point to the address of the next instruction. "You" the author of the program running on the CPU do not need to increment the PC, and it may not even be accessible to you. Instead the CPU itself handles incrementing it, which could be part of the circuitry of a hardware CPU, or part of the software of a CPU implement by a virtual machine.
The program counter stores the address of the next instruction i.e the instruction that is next to be executed. When the current instruction is executed,the program counter is incremented by 1.
-
$\begingroup$ Welcome to COMPUTER SCIENCE @SE. Please provide a reference or other context for the statement in your answer. $\endgroup$ – greybeard Oct 9 '20 at 6:51
the program counter is incremented by 1
while I don't think a hard-wired non-linear sequence has been commercialised, performance has been investigated - let's say the program counter is advanced or stepped. $\endgroup$ – greybeard Oct 9 '20 at 6:48