You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何强制GNU AS生成指定ARM SUB指令Opcode(1e40而非3801)

How to Force GNU AS to Emit 1E40 (32-bit Thumb-2 subs r0, r0, #1) Instead of 3801 (16-bit Thumb subs r0, #1)

Great question! Let's unpack what's happening here and how to get the exact opcode you want.

First, the two instructions you're seeing are functionally equivalent, but they use different Thumb instruction encodings:

  • 3801 is a 16-bit Thumb instruction (subs r0, #1): This is a compact, optimized version that implicitly uses the same register for source and destination.
  • 1E40 is a 32-bit Thumb-2 instruction (subs r0, r0, #1): This is the full, explicit two-operand form of the same operation.

GNU AS (from the ARM 2017 Q4 GCC toolchain) defaults to generating the smallest possible instruction when using unified syntax, so it automatically optimizes subs r0, r0, #1 into the 16-bit subs r0, #1 encoding. To override this and force the 32-bit opcode, you have two reliable options:

Option 1: Use the .w Suffix to Force a Wide (32-bit) Instruction

In ARM unified syntax, adding the .w suffix to an instruction tells the assembler to generate a 32-bit Thumb-2 instruction, even if a smaller 16-bit version exists.

Here's how to modify your assembly code:

.syntax unified
.thumb

your_function:
    subs.w r0, r0, #1  @ This will emit the 0x1E40 opcode

Assemble this with your toolchain, then verify with objdump:

arm-none-eabi-as -o output.o your_code.s
arm-none-eabi-objdump -d output.o

You'll see the 1E40 opcode for that line, exactly as you want.

Option 2: Hardcode the Opcode Directly with .inst

If you want to bypass the assembler's instruction parsing entirely, you can use the .inst pseudo-instruction to write the raw opcode directly:

.syntax unified
.thumb

your_function:
    .inst 0x1E40  @ Explicitly emits the subs r0, r0, #1 opcode

This works, but it's less readable and maintainable than using the .w suffix, so it's only recommended if you have a specific reason to hardcode opcodes.

Why This Works

The .w suffix is part of the ARM unified syntax specification, designed to resolve ambiguity between 16-bit and 32-bit Thumb instructions. Since your code already uses .syntax unified, this suffix will be recognized and honored by GNU AS.

内容的提问来源于stack exchange,提问作者The19thFighter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:30:15