0

It is possible to access Sp, Lr, and Pc and store them in normal C variable?... if yes then how to do it.? please explain it with c code..

I'm using arm gcc compiler

Mustak U
  • 115
  • 2
  • 11
  • 1
    You haven't specified which C compiler you're using. – Michael Apr 07 '15 at 11:05
  • 2
    Sure, with inline assembly or a call to a separate assembly function you can get any register value you want. How meaningful they are is another matter - `pc` and `sp` wouldn't tell much more than you can already know from C (take the address of a local variable or the current function), and apart from at the very start of a function `r14` could have _anything_ in it - 99 times out of 100 when people want to inspect `lr`, it means they really want their C compiler's `return_address` intrinsic. – Notlikethat Apr 07 '15 at 11:16
  • 1
    possible duplicate of [get the caller's lr from subroutine into C variable - arm](http://stackoverflow.com/questions/18270702/get-the-callers-lr-from-subroutine-into-c-variable-arm) – artless noise Apr 07 '15 at 16:15

1 Answers1

4

In GCC:

uint32_t some_variable;
__asm__ __volatile__ ("mov %0, lr" : "=r" (some_variable));

This tells the compiler to allocate a register for some_variable (which is represented by the placeholder %0), and then emit the instruction mov %0, lr. The effect is to store the value of lr into some_variable.

__volatile__ tells the compiler not to reorder this instruction, which I assume you want.

This should also work with pc or sp. (Or any other register, but doing it with "normal" registers won't be very useful - among other things, the compiler might choose to allocate some_variable to the register you're trying to look at)

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    It might be not very useful to access any other register. But if the compiler allocates the register in question as the target of the `move` it would have still the correct value. – harper Apr 07 '15 at 11:35