2
$\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$
  • 1
    $\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. Dec 5 '14 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 Dec 5 '14 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 Dec 7 '14 at 15:42
2
$\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$
  • $\begingroup$ "every instruction is composed of 4 bytes" -- careful; cf RISC vs CISC. $\endgroup$ – Raphael Dec 6 '14 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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