ARM 程序集不能同时使用立即数和 ADDS/ADCS

ARM assembly cannot use immediate values and ADDS/ADCS together

我目前正在尝试使用汇编来加速我在 Cortex-M0 (Freescale KL25Z) 上的一些 C 函数。我遇到这个最小测试程序的问题:

@.syntax unified
.cpu cortex-m0
.text
  .global test
  .code 16
test:
  mov r0, #0
  adds r0, r0, #1
  bx lr

当我尝试将我的 .s 文件 assemble 转换为 .o 文件时,出现此错误

$ arm-none-eabi-as test.s -o test.o
test.s: Assembler messages:
test.s:8: Error: instruction not supported in Thumb16 mode -- `adds r0,r0,#1'

错误消息对我来说没有意义,ADDS 是根据 this document. I found a possible answer on Whosebug 的有效指令并在程序开头添加了行(“.syntax unified”有效,就像第二个答案一样建议)。这解决了这个问题,我现在可以使用 ADDS 和 ADCS 等指令,但我确实收到了一个新错误:

$ arm-none-eabi-as test.s -o test.o
test.s: Assembler messages:
test.s:7: Error: cannot honor width suffix -- `mov r0,#0'

某些使用立即值的指令会出现此错误。我正在 Mac OS 10.9.5 上编译。我无法通过 Google 或 Whosebug 找到解决方案,也不知道如何解决这些错误。

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.9.3 20150303 (release) [ARM/embedded-4_9-branch revision 221220]
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ arm-none-eabi-as --version
GNU assembler (GNU Tools for ARM Embedded Processors) 2.24.0.20150304
Copyright 2013 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `arm-none-eabi'.

在拇指模式下,你不能使用adds,你只有add。因此,正确的代码是:

.cpu cortex-m0
.text
  .global test
  .code 16
test:
  mov r0, #0
  add r0, r0, #1
  bx lr

有点讽刺的是,通过使用 UAL 语法解决第一个问题,您现在遇到了几乎相同的问题,但反过来却出现了更加神秘的症状。

(非标志设置)mov 具有立即操作数的唯一 Thumb 编码是 32 位编码,但是 Cortex-M0 不支持这些编码,因此汇编器最终会在其上窒息自己的约束。在 UAL 中,您必须明确使用 movs 才能获得 the only "move immediate" instruction Cortex-M0 actually has.

小丑说的话:

.cpu cortex-m0
.text
  .thumb

  .thumb_func
  .global test
test:
  mov r0, #0
  add r0, r0, #1
  bx lr

这给出了

   0:   2000        movs    r0, #0
   2:   3001        adds    r0, #1
   4:   4770        bx  lr

或者如果你使用统一的语法,那么你必须把 s 放在那里

.syntax unified
.cpu cortex-m0
.text
  .thumb

  .thumb_func
  .global test
test:
  movs r0, #0
  adds r0, r0, #1
  bx lr

这也给出了

00000000 <test>:
   0:   2000        movs    r0, #0
   2:   3001        adds    r0, #1
   4:   4770        bx  lr