eip register in gcc inline assembly

Programming, for all ages and all languages.
Post Reply
CyberP1708

eip register in gcc inline assembly

Post 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)
AR

Re:eip register in gcc inline assembly

Post 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));
CyberP1708

Re:eip register in gcc inline assembly

Post by CyberP1708 »

Thank you
It's compiling
AR

Re:eip register in gcc inline assembly

Post 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.
Post Reply