The Wikipedia article about x86 assembly says that "the IP register cannot be accessed by the programmer directly."
Directly means with instructions like mov and add, the same way we can read and write EAX.
Why not? What is the reason behind this? What are the technical restrictions?
There are special instructions like jmp to set it, and call to push the old value before setting a new one. (And in x86-64, read with LEA using a RIP-relative addressing mode.) See Reading program counter directly for details.
You can’t access it directly because there’s no legitimate use case. Having any arbitrary instruction change
eipwould make branch prediction very difficult, and would probably open up a whole host of security issues.You can edit
eipusingjmp,callorret. You just can’t directly read from or write toeipusing normal operationsSetting
eipto a register is as simple asjmp eax. You can also dopush eax; ret, which pushes the value ofeaxto the stack and then returns (i.e. pops and jumps). The third option iscall eaxwhich does a call to the address in eax.Reading can be done like this: