i am writing a simple c program and my requirement is to print RIP(Instruction Pointer) from some function of the program. i dont want to use ptrace.
the one thing i tried with inline asm is:
asm(“movl %%rip, %0;” : “=r”(val) )
this should copy my rip register value to variable val, but i am getting compilation error.
if i use ebp/esp which are base pointer and stack pointers for 32 bit machine, i dont get any compilation error and my val has some hexadecimal number assigned.
i have few questions here:
1) as my machine is 63 bit, how was above instruction able to read 32 bit registers?
2) why i am not able to read any register for 64 bit, is there any issue b’caz of ‘r’?
3) when i use eip which is for 32 bit i get compilation error, does it mean IP registers are restricted for reading?
gcc, try using the-m64flag, or read the compiler documentation for more information.rip, theeipregister cannot be directly accessed. You can get the value ofeipin the manner described by Jim in his answer.[1] you would be able to read the 32-bit registers from a 64-bit executable anyway; the 32-bit registers are still available in 64-bit mode, just like you can access the 16-bit registers in 32-bit mode.
There are a few problems still in your example:
First, although
ripis accessible in 64-bit mode, it’s accessible as an addressing mode; it’s not a normal register. If you want to load its value, you need to useLEA, notMOV.Second, because
ripis a 64-bit register, you need to use theqsuffix on your instructions instead ofl. Here’s a sample program with these two issues addressed:which seems to work just fine on my machine.