tags:

views:

81

answers:

4

Hi,

I want to get the current value of the EIP register with assembly language. Is that possible?

Thanks.

+6  A: 
    call foo
foo:
    pop eax ; address of foo
Abyx
This approach has a subtle issue. Modern processors try to predict return addresses - a call which is not paired with a return messes up the prediction. See http://blogs.msdn.com/b/oldnewthing/archive/2004/12/16/317157.aspx
Paul Baker
@Paul Baker usually it isn't a critical issue. For example `call @f / db '123',0 / @@:` is a common practice
Abyx
call foo, pop eax, push eax, ret
Jens Björnhager
@Jens Björnhager it's infinite loop. `call foo / foo: pop eax / add eax, bar-foo / push eax / ret / bar:`
Abyx
@Abyx Then at least you know at which EIP your program is stuck! :)
Jens Björnhager
If foo isn't inline, then you won't get stuck and will get the EIP of **the next* instruction.
Jens Björnhager
+2  A: 

Since EIP is the program counter, there's no way to access it directly (i.e. it can't be used as the source of a MOV instruction).

There are two ways to access it indirectly:

  • Use an interrupt and get the saved EIP from the stack,
  • Use a specially crafted function that fetches its return address (the saved EIP) from the stack.

See http://www.programmersheaven.com/mb/x86_asm/357735/357735/get-the-value-of-eip/#357740.

Frédéric Hamidi
+6  A: 

Assuming 32-bit x86, use the following function:

get_eip: mov eax, [esp]
         ret

Then, to get the value of EIP in EAX, simply:

call get_eip
Paul Baker