gcc - 如何在 GCC 中使用拇指 2 指令

标签 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 位拇指 2 指令,因为 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]

谢谢

最佳答案

我刚刚发现我应该说

.syntax unified

下面

.section

以下主题涉及其他内容,但我在那里看到并尝试过。它起作用了。

How to generate the machine code of Thumb instructions?

关于gcc - 如何在 GCC 中使用拇指 2 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39390218/

相关文章:

c++ - static_cast 转换构造函数 vs 转换运算符

assembly - FILD 和 FSTP 指令

assembly - 为什么我们要使用addiu而不是addi?

python - 编译 io 编程语言时出错

C - 读取和设置汇编寄存器

gcc - GCC LTO 是否执行跨文件死代码消除?

c++ - NetBeans IDE gcc 编译器优化选项

c - ICC 是否满足复数乘法的 C99 规范?

c++ - 如何在 ns3 中使用 nvcc 作为编译器

c++ - 分配的值比左值有更多的位