我们如何检查c中内联汇编中的寄存器值?

How can we check the register value in inline-assembly in c?

我想在 uVision 中使用 STM32F 进行简单的内联汇编实验,代码如下。

但是我在编译时遇到了一些错误信息的问题。

unsigned int bar(unsigned int r0)

{
  unsigned int r1;
  unsigned int r4 = 1234;
__asm
{


    MOVS  r0,#0
    LDR   r1,[r0]     ; Get initial MSP value
    MOV   SP, r1
    LDR   r1,[r0, #4] ; Get initial PC value
    BX    r1
}

  return(r1);

}

我编译的时候出现了下面的错误信息。

*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'STM32F429_439xx'
compiling main.c...
../main.c(79): error:  #3061: unrecognized instruction opcode
                                LDR   r1,[r0]     ; Get initial MSP value
../main.c(80): error:  #20: identifier "SP" is undefined
                                MOV   SP, r1
../main.c(81): error:  #3061: unrecognized instruction opcode
                                LDR   r1,[r0, #4] ; Get initial PC value
../main.c(82): error:  #1084: This instruction not permitted in inline assembler
                                BX    r1
../main.c(71): warning:  #177-D: variable "r4"  was declared but never referenced
    unsigned int r4 = 1234;
../main.c(82): error:  #114: label "r1"  was referenced but not defined
                                BX    r1
../main.c: 1 warning, 5 errors
"STM32F429_439xx\STM32F429_439xx.axf" - 5 Error(s), 1 Warning(s).
Target not created.
Build Time Elapsed:  00:00:01

我应该怎么做才能解决这个问题?

我有一种挥之不去的感觉,您在这里尝试做的事情并不完整,进行软重置可能会更好。然而;

http://www.keil.com/support/man/docs/armcc/armcc_chr1359124249383.htm

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.

...

No variables are declared for the sp (r13), lr (r14), and pc (r15) registers, and they cannot be read or directly modified in inline assembly code.

但是,CMSIS 提供以下内容: https://www.keil.com/pack/doc/CMSIS/Core/html/group__Core__Register__gr.html#gab898559392ba027814e5bbb5a98b38d2

__STATIC_INLINE uint32_t __get_MSP(void)
{
  register uint32_t __regMainStackPointer     __ASM("msp");
  return(__regMainStackPointer);
}