Accessing a register without using inline assembly with gcc?

Up vote 6 down vote favorite 2 share g+ share fb share tw.

I want to read the stack pointer register value without writing inline assembly. The reason I want to do this is because I want to assign the stack pointer register value to an element of an array and I find it cumbersome to access an array using inline assembly. So I would want to do something like that.

Register "rsp" long rsp_alias.

There's a shortcut: register long rsp asm ("rsp"); Demo: #include void foo(void) { register long rsp asm ("rsp"); printf("RSP: %lx\n", rsp); } int main() { register long rsp asm ("rsp"); printf("RSP: %lx\n", rsp); foo(); return 0; } Gives: $ gdb . /a. Out GNU gdb (Gentoo 7.2 p1) 7.2 ... Reading symbols from /home/seigneur/tmp/a.out...done.

(gdb) break foo Breakpoint 1 at 0x400538: file t. C, line 7. (gdb) r Starting program: /home/seigneur/tmp/a.

Out RSP: 7fffffffdb90 Breakpoint 1, foo () at t. C:7 7 printf("RSP: %lx\n", rsp); (gdb) info registers .... rsp 0x7fffffffdb80 0x7fffffffdb80 .... (gdb) n RSP: 7fffffffdb80 8 } Taken from the Extended ASM documentation.

Thanks Mat, this was the answer I was looking for. – MetallicPriest Nov 20 '11 at 12:06.

Register const long rsp_alias asm volatile("rsp").

– MetallicPriest Nov 20 '11 at 12:06 @MetallicPriest: between register and long would be the most "natural" way, and indeed, it's a good idea. – Mat Nov 20 '11 at 12:08 yup -- I've noticed compiler optimizing away the selected register, thats why I included volatile. – Anthony Blake Nov 20 '11 at 12:10.

Why can't you use an asm instruction inside a C function compiled by GCC" - Because I don't want to, as mentioned in the question. – MetallicPriest Nov 20 '11 at 12:05 But you cannot be sure that the compiler won't change the stack pointer in the generated code later... (e.g. Alloca). – Basile Starynkevitch Nov 20 '11 at 12:08.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions