【问题标题】:How to use thumb-2 instructions with GCC如何在 GCC 中使用 thumb-2 指令
【发布时间】:2016-09-08 11:53:59
【问题描述】:

我编写了以下简单的 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]

谢谢

【问题讨论】:

    标签: gcc assembly compilation thumb


    【解决方案1】:

    我才发现我应该说

    .syntax unified
    

    下面

    .section
    

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

    How to generate the machine code of Thumb instructions?

    【讨论】:

    • .syntax unified 可以是源文件的第一行。 The directive applies globally,不只是在一个部分内。
    • 那么如果我在汇编时使用-mthumb 参数,GNU 汇编器只允许汇编Thumb16 指令,但.syntax unified 指令告诉汇编器它也可以在最终汇编中包含Thumb32 指令?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-03-07
    • 1970-01-01
    • 2020-06-21
    • 2016-02-16
    • 2017-10-28
    相关资源
    最近更新 更多