How is program counter(PC) incremented when instruction takes more than one word? How does PC know when to increment by one word or by more than one word since incrementation is done before decoding the instruction(if I am correct)?
1 Answer
Who says incrementing the program counter would have to happen before decoding the instruction? Modern decoders will actually decode several instructions simultaneously (if the first instructions are simple enough so that the decoder can find the start of the next instructions with negligible delay).
That's what Intel processors with their fabulously complex instruction set do. The decoder is given a large range of bytes. For some instructions it's easy to determine the length, for some it's difficult.
So the decoder tries to find the length of the instruction starting at the first byte, and very quickly finds that it is a simple instruction say of 3 bytes, or something more complicated. The decoder does the same thing simultaneously assuming an instruction starting at byte 1, at byte 2, at byte 3 and so on. (Duplicating hardware is cheap when a chip can have billions of transistors).
Once the decoder has these numbers (instruction at #0 is 3 bytes, instruction at #1 is difficult, instruction at #2 is 6 bytes, instruction at #3 is 2 bytes, instruction at #4 is difficult, instruction at #5 is difficult etc. ) it combines these results and knows there is a 3 byte instruction at #0, followed by a 2 byte instruction at #3, followed by a difficult instruction at #5. The information about the instruction starting at #1, #2, #4, #6 etc. is thrown away.
And then the decoder gets really going: It can decode several simple and one complex instruction. So the processor decodes the simple instruction at #0, the simple instruction at #3, and the complex instruction at #5. And when it has the length of that complex instruction at #5, then it increases to program counter.