pointersassemblyx86eip

How does the EIP register get its value?


I've just started to learn assembly in school, and we're starting to dive into registers and how to use them. A point that I can't seem to understand is how does the instruction pointer get the address of the next instruction? For instance take the following code:

nop
pushl    %ebp
movl    %esp, %ebp
subl    $4, %esp

In the previous code the instruction pointer gets incremented after each line, and I'd like to know how does it know which instruction to do next (i.e mov,sub,push,...etc.)? Are all the previous instruction first loaded into RAM when we first run the program and the address of the first instruction (nop in this case) gets automatically loaded into eip, then it just goes over them one by one? Or am I missing something?

Any help is appreciated.


Solution

  • EIP is updated by the microcode (firmware) in the CPU itself each time an instruction is retrieved and decoded for execution. I don't believe you can even access it is in the usual sense. However it can be modified using a jmp instruction, which is functionally (not include pipeline issues and so forth) the same as mov %eip,address. It is also updated on conditional jumps, call, and ret instructions.

    Once your program is loaded into memory (during this process you can think of you program as simply data like any other file), the OS (or some other loader program) performs a jmp to the start of your program. Of course the code you are showing as example code is the real start of the program but simply a function that main has called.