6.44 Accessing sp (r13), lr (r14), and pc (r15)

The following methods enable you to access the sp, lr, and pc registers correctly in your source code.

The first method uses the compiler intrinsics in inline assembly, for example:
void printReg()
{
    unsigned int spReg, lrReg, pcReg;
    __asm
    {
        MOV spReg, __current_sp()
        MOV pcReg, __current_pc()
        MOV lrReg, __return_address()
    }
    printf("SP = 0x%X\n",spReg);
    printf("PC = 0x%X\n",pcReg);
    printf("LR = 0x%X\n",lrReg);
}
The second method uses embedded assembly to access physical ARM registers from within a C or C++ source file, for example:
__asm void func()
{
    MOV r0, lr
    ...
    BX lr
}
This enables the return address of a function to be captured and displayed, for example, for debugging purposes, to show the call tree.

Note

The compiler might also inline a function into its caller function. If a function is inlined, then the return address is the return address of the function that calls the inlined function. Also, a function might be tail called.
Related concepts
6.26 Embedded assembler support in the compiler
Related reference
9.127  __return_address intrinsic
9.104 __current_pc intrinsic
9.105 __current_sp intrinsic
Non-ConfidentialPDF file icon PDF versionARM DUI0375E
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.