Page 1 of 1

eip register in gcc inline assembly

Posted: Wed Nov 02, 2005 2:47 am
by CyberP1708
Hello,

I just wanted to know how to get the value of the eip register by using gcc inline assembly.
If I use :

Code: Select all

asm volatile("movl %%eip, %%eax":"=a"(reg_eip));
When I compile it tells me :
Bad register name "%eip"

What I want is just as if I would write this with nasm :

Code: Select all

mov [reg_eip], $
(sorry for my bad english)

Re:eip register in gcc inline assembly

Posted: Wed Nov 02, 2005 2:56 am
by AR
AFAIK, you can't access EIP directly, but you can try this:

Code: Select all

__asm__ volatile ("call 1f \n\t"
             "1: pop %0" : "=r"(reg_eip));

Re:eip register in gcc inline assembly

Posted: Wed Nov 02, 2005 3:06 am
by CyberP1708
Thank you
It's compiling

Re:eip register in gcc inline assembly

Posted: Wed Nov 02, 2005 3:35 am
by AR
I should probably state explicitly that that code will actually return the address of the POP instruction rather than the CALL in case that is a problem. Alternatively, you can just:

Code: Select all

__asm__ volatile ("1: movl $1b, %0" : "=r" (reg_eip));
Note that neither this code example or your NASM example are actually reading EIP, they are simply storing the location provided by the linker, so:

Code: Select all

mov eax, $
;Will be assembled into machine code as
mov eax, 400034h
If the code is Position Independant and you want to find where you are then the CALL; POP will be better.