Fetching instructions from memory

assemblymachine-code

So, I am a noob in assembly language and machine instructions.

When the discussion came up as to how funtion calls are implemented at the processor level, someone said that the CALL instruction pushes the address of the next instruction to be executed on the stack before updating the EIP and then someone asked how do we know the address of the next instruction.

And then someone asked while EIP points to the current instruction in memory how does the processor know how many bytes from EIP need to be read so that the whole instruction is read.

I assumed that all instructions are fixed length so the processor would read those many bytes as would constitute the instruction and then the decoding process would determine how many more bytes need to be read to fetch the operands. However when searching about the instructions I came to know that the instruction sizes vary.

So we are back to the same question:
How does the processor know the number of bytes to read to get the instruction from memory to the instruction cache?

Best Answer

In most CPUs, the next-instruction-pointer is updated as the first part of executing the instruction. The CPU, of course, can determine the length of the instruction from its opcode and/or modifier bits. Every architecture has a different way of indicating this, but it is always well defined.

Related Topic