Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
the hardware knows every instruction and places the correct value on the stack for architectures that work that way, others may place the return value in a register. –  dwelch Feb 20 at 20:45

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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