How to access r11 register of ARM Cortex M3 with i

2019-08-11 07:10发布

问题:

I tried below code, but failed to read the correct value from r11 following below reference of http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472f/Cihfhjhg.html

volatile int top_fp;
__asm
{
    mov top_fp, r11
}

r11's value is 0x20009DCC top_fp's value is 0x00000004

[update] Solution, we have to use embedded assembler:

__asm int getRegisterR11()
{
    mov r0,r11
    BX LR //return, must not omitted!!!
}

回答1:

The link you posted refers only to lr(R13), sp(R14), pc(R5) for legacy code support of code for old versions of ARM ADS and does not apply to general-purpose registers.

In ARM's compiler (also used in Keil's MDK-ARM):

The inline assembler provides no direct access to the physical registers of an ARM processor. If an ARM register name is used as an operand in an inline assembler instruction it becomes a reference to a variable of the same name, and not the physical ARM register.

(Ref: Inline assembler and register access)

Inline assembler in ARM's compiler is subject to optimisation like the C or C++ code it is in-lined within, as such the compiler may generate code that differs from that you have written in any case. If you want assembler code to be generated exactly as you have written you must use embedded assembler rather the inline assembler