Reputation: 689
I am trying to store value of SP register to a variable in C, here is my code:
int address = 0;
asm volatile ("STR sp, [%0]\n\t"
: "=r" ( address)
);
but after executing this code, os print "Segmentation fault" message in screen and terminate the programme. can any one give me a advise to solve the problem?
Upvotes: 1
Views: 8541
Reputation: 6354
You shouldn't even touch the stack pointer in inline assembly. It's a taboo!!!
You aren't supposed to do that to start with. Just leave it to the compiler.
If you aren't altering the stack pointer or writing anything onto the stack, it might work, you just used the wrong instruction.
What you want to do: copy the stack pointer to a 32bit register.
What you did: store the stack pointer itself to the address 0. ==> segmentation fault.
Replace STR sp, [%0]\n\t
with mov %0, sp\n\t
Upvotes: 4