Program Counter?

Ockham picture Ockham · Apr 11, 2012 · Viewed 10.4k times · Source

If I understand this correctly, the program counter points to the address of the instruction to be executed and in most cases you add four to the program counter to advance to the next instruction address. But say you have a program counter that's pointing to a word (e.g word 15) in memory and you want to advance to the next instruction, are you suppose to add 4 directly to 15 in order to get the next instruction?? Any explanation would be appreciated

Answer

sharptooth picture sharptooth · Apr 11, 2012

That thing is called instruction pointer. Once the processor decodes the current instruction it finds how many bytes it occupies and knows how much to add to the current instruction pointer value to advance to the next instruction so that when the current instruction gets executed the processor knows what to do next.

So for example the processor starts with the instruction pointer storing value 15 as in your example - it looks what happens to be at that address, there happens to be an instruction occupying 5 bytes, no problem - it adds 5 to the current value and this yields 20 and so the instruction pointer now stores value 20 and the processor then executes the current instruction.