3
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ What research have you done? Have you read in your computer architecture textbook how to build an adder? If yes, then you know how to add 1 to something. $\endgroup$
    – D.W.
    Commented Dec 5, 2014 at 18:46
  • $\begingroup$ I know how to add 1 :P But what I do not understand is, why would you get the next instruction by adding 1? When the current instruction is stored at address 10001111, then the next instruction should be stored at 10010000? Is this right? $\endgroup$
    – Joey
    Commented Dec 5, 2014 at 18:49
  • $\begingroup$ @Kevin Yes, This is right. Well there may be issues regarding byte address versus word address. But this is minor. A byte address is just a word address with 2 or 3 zero bits appended (depending on whether you have 32 or 64 bits machine, i.e. 4 or 8 bytes. But I do not see what you perceive as a problem. $\endgroup$
    – babou
    Commented Dec 7, 2014 at 15:42
  • $\begingroup$ 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
    Commented Oct 9, 2020 at 6:48

7 Answers 7

3
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ "every instruction is composed of 4 bytes" -- careful; cf RISC vs CISC. $\endgroup$
    – Raphael
    Commented Dec 6, 2014 at 8:45
  • $\begingroup$ Typical Intel processor: Anything from 1 to 15, ignoring branch and conditional branch. $\endgroup$
    – gnasher729
    Commented Mar 10, 2023 at 12:18
  • $\begingroup$ @gnasher729 x86 does not have a PC, it has an IP.☺ $\endgroup$
    – user4577
    Commented Mar 10, 2023 at 17:09
  • $\begingroup$ Multiplication by 4 is shift by 2, not by 4. $\endgroup$ Commented May 4 at 17:11
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

Instructions are not necessarily of length 1 (byte).

After an instruction has been read from memory and interpreted*, the address held by program counter is incremented by the appropriate number of bytes (simply using an adder), ready to fetch the next instruction.

*In case of variable length instructions, the instruction decoder determines the instruction length on the fly.

$\endgroup$
0
$\begingroup$

You're correct that the program counter (PC) is a register that stores the memory address of the next instruction to be executed by the processor. After the current instruction is completed, the PC is incremented to point to the next instruction in memory.

Regarding your question on how the PC is incremented by 1, it's important to understand that the size of an instruction in memory is not always fixed. Depending on the architecture and instruction set of the processor, instructions can have different sizes.

For example, in a typical x86 processor, the size of an instruction can range from 1 byte (e.g., NOP instruction) to 15 bytes (e.g., complex instructions with prefixes). In such processors, the PC is incremented by the size of the instruction that was just executed.

To do this, the processor uses a technique called "instruction decoding". This involves analyzing the bits of the instruction in memory to determine its size, and then incrementing the PC accordingly. The processor may use various techniques to optimize this process, such as pre-fetching instructions from memory or caching recently executed instructions.

So, the size of the instruction is determined during the decoding phase, and the PC is incremented by that size to point to the next instruction in memory.

$\endgroup$
0
$\begingroup$

Actually this is a very important question. To state its utmost importance, GCC written by Richard Stallman in 1983 required that addressing must be byte.. if this is not heeded then the whole of open source software cannot port said CPU to the GCC platform meaning Linux. Byte addressing began with the IBM-360 in 1964 in which Dennis Ritchie the inventor of C in early 1970's followed suit. GCC adhered to Ritchie C. The x86 which began from 8086 had program counter auto increment by 1.. yes..one what? This is not a trivial question-answer. Wrong answer and open source/GCC compatibility collapses. Yes, later x86 Pentium and so on read a row of instructions at one go but program counter steps by one BYTE after completion of last instruction. Later 32-bit RISC CPUs like the ARM steps program counter also by one, but by 1-WORD of 4 bytes. Later Thumb version steps by one 16-bit word. The questioner is not naive to not know any decent adder can add one.. his question was 1 what? To verify this please download the old IBM PC-XT Technical reference which came at 628 pgs the 2nd half of the book contained the 8kB BIOS listing. There it is clear as day that the assembly listing had no gaps in codes, meaning virtually every address was filled with executable codes at every byte locations: odd, even, byte, 2-byte, n-byte.. remember this classic CISC is an n-byte instruction architecture. x86 has 1-byte instructions; IRET, RET, XLAT, PUSHA, POPA, HLT, and these can sit anywhere in program space, and if there are no gaps in code listing, the program counter must auto increment to the next 1-byte opcode.. ie., count in bytes.

$\endgroup$
1
  • $\begingroup$ Byte addressability of data memory is not tied intimately with addressability of program memory - think Harvard architecture. And yes, these are still around in a great many microprocessors (several having e.g.12- or 13-bit instructions). Addressing program memory in anything but instructions is not useful, with the possible exception of modifying code. $\endgroup$
    – greybeard
    Commented May 5 at 13:16
-2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Welcome to COMPUTER SCIENCE @SE. Please provide a reference or other context for the statement in your answer. $\endgroup$
    – greybeard
    Commented Oct 9, 2020 at 6:51
-2
$\begingroup$

Program Counter- it is a location where the instruction executed is stored at a current time, and where the next execution is set.

$\endgroup$
1
  • $\begingroup$ I don't see how this answers the question that was asked. The question didn't ask what the PC is, it asked how it is incremented. Any flags from the community? $\endgroup$
    – D.W.
    Commented Mar 10, 2023 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.