如何在GCC中使用thumb-2指令

时间:2016-09-08 11:54:00

标签: gcc assembly compilation thumb

我编写了以下简单的memcpy32函数,作为了解如何为cortex-m4编写汇编代码的一种方法。

    .section .text
    .align 2
    .global as_memcpy32
    .type as_memcpy32, %function
as_memcpy32:
     push    {r4, lr}
     movs    r3, #0
start_loop: 
     cmp     r3, r2
     bge   end_loop
     ldr   r4, [r1]
     str   r4, [r0]
     add   r0, #4
     add   r1, #4
     add    r3, #1
     b     start_loop
end_loop:   
    pop     {r4, pc}

上面的代码编译并运行。这些只是16位指令。我也想使用32位thumb2指令,因为Cortex-M4支持它们。编写程序集的主要目的是更快地运行我的代码。

我应该能够根据STM32F4手册使用以下形式的ldr和str指令

op{type}{cond} Rt, [Rn], #offset; post-indexed

我向GCC提供以下选项。

arm-none-eabi-gcc" -c -g -x assembler-with-cpp -MMD -mcpu=cortex-m4 -DF_CPU=168000000L -DARDUINO=10610 -DARDUINO_STM32DiscoveryF407 -DARDUINO_ARCH_STM32F4  -DMCU_STM32F406VG -mthumb -DSTM32_HIGH_DENSITY -DSTM32F2 -DSTM32F4 -DBOARD_discovery_f4   -mthumb -D__STM32F4__ memcpy.S" -o memcpy.S.o

当我尝试使用以下ldr和str

的说明时
ldr r4, [r1], #4
ldr r4, [r0], #4

我收到以下错误。

memcpy.S: Assembler messages:

memcpy.S:11: Error: Thumb does not support this addressing mode -- `ldr r4,[r1],#4'

memcpy.S:12: Error: Thumb does not support this addressing mode -- `str r4,[r0],#4'

exit status 1
Error compiling for board STM32 Discovery F407.

我无法理解问题所在。实际上编译器本身生成了更复杂的寻址操作码。

ldr.w   r4, [r1, r3, lsl #2]
str.w   r4, [r0, r3, lsl #2]

感谢

1 个答案:

答案 0 :(得分:3)

我刚发现我应该说

.syntax unified

下面

.section

以下主题涉及其他事项,但我在那里看到并尝试过。它奏效了。

How to generate the machine code of Thumb instructions?