简体   繁体   English

gcc arm __asm内联在参数中传递常量

[英]gcc arm __asm inline passing constant in parameter

I am coding a small cortex M0+ bootloader. 我正在编写一个小皮质M0 + bootloader。 I have a inline assembly below that start the main application from the bootloader by loading the stack pointer and reset handler from application position in Flash. 我在下面有一个内联汇编,通过从Flash中的应用程序位置加载堆栈指针和重置处理程序,从引导加载程序启动主应用程序。

#define FLASH_APP_START 0x1000

[...]

    __asm(
            // Update stack pointer from user code vector table
            "LDR     r0, =%0 \n"
            "LDR     r1, [r0] \n"
            "MOV     sp, r1 \n"

            // Load user code reset handler and jump to the user code
            "LDR     r0, [r0, #4] \n"
            "BX      r0 \n"
            :
            : "X"(FLASH_APP_START)
            :
        );

When compiling this code I get the following error: 编译此代码时,我收到以下错误:

Error: bad expression -- `ldr r0,=#4096' 错误:表达错误 - `ldr r0,=#4096'

GCC add the # before the constant that should not be there. GCC在不应该存在的常量之前添加#。 If I replace the first line by the following it works perfectly. 如果我用以下替换第一行它完美地工作。

LDR r0, =0x1000 LDR r0,= 0x1000

So the question is how could I use the defined constant ? 所以问题是如何使用定义的常量?

Thanks in advance for any help 在此先感谢您的帮助

First there's as no reason to use inline assembly here, the simple solution would be to just write this code as a normal assembly language file. 首先,没有理由在这里使用内联汇编,简单的解决方案是将此代码编写为普通的汇编语言文件。 You can define the FLASH_APP_START macro in a header file that you can include in both the assembly and C source files if necessary. 您可以在头文件中定义FLASH_APP_START宏,如果需要,可以包含在程序集和C源文件中。

If you still want to use inline assembly then next simplest solution would be to let the compiler do most of the work for you and reduce the assembly statement to just what it can't do: 如果您仍想使用内联汇编,那么下一个最简单的解决方案是让编译器为您完成大部分工作,并将汇编语句减少到它不能执行的操作:

    void ** const flash_vector_table = (void **) FLASH_APP_START;
    asm volatile ("mov sp, %[stack]\n\t"
                  "bx %[reset]\n"
                  :
                  :
                  [stack] "r" (flash_vector_table[0]),
                  [reset] "r" (flash_vector_table[1]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM