简体   繁体   English

ARM 程序集中的立即数是否需要散列?

[英]Is the hash required for immediate values in ARM assembly?

I've been working on reading through some different arm assembly code generated by gcc, and I came across something that I haven't been able to find in the spec.我一直在阅读由 gcc 生成的一些不同的 arm 汇编代码,但我遇到了一些我在规范中找不到的东西。

movw    r0, #39784
movt    r0, 1

Obviously the first one is moving the value 39784 into the bottom 16bits or r0, but the movt's operand of '1' is odd because it doesnt have the hash before it, and I was under the impression that immediate values required the hash.显然,第一个是将值 39784 移动到底部 16 位或 r0,但 movt 的操作数 '1' 是奇怪的,因为它前面没有散列,我的印象是立即数需要散列。 Is it somehow optional in certain situations?在某些情况下它是否是可选的? or am I missing something magical?还是我错过了一些神奇的东西?

The GNU assembler does not require an octothorpe before an immediate operand for ARM assembly code. GNU 汇编器在 ARM 汇编代码的立即数操作数之前不需要 octothorpe。 Your impression is incorrect.你的印象是错误的。

GNU gas behavior for ARMv7 depends on .syntax ARMv7 的 GNU gas行为取决于.syntax

The docs say https://sourceware.org/binutils/docs-2.26/as/ARM_002dInstruction_002dSet.html#ARM_002dInstruction_002dSet :文档说https://sourceware.org/binutils/docs-2.26/as/ARM_002dInstruction_002dSet.html#ARM_002dInstruction_002dSet

Two slightly different syntaxes are support for ARM and THUMB instructions.两种稍微不同的语法是支持 ARM 和 THUMB 指令。 The default, divided, uses the old style where ARM and THUMB instructions had their own, separate syntaxes.默认情况下,划分使用旧样式,其中 ARM 和 THUMB 指令具有自己的独立语法。 The new, unified syntax, which can be selected via the .syntax directive, and has the following main features:新的、统一的语法,可以通过 .syntax 指令选择,具有以下主要特点:

  • Immediate operands do not require a # prefix.立即数操作数不需要 # 前缀。

and https://sourceware.org/binutils/docs-2.26/as/ARM_002dChars.html#ARM_002dChars says:https://sourceware.org/binutils/docs-2.26/as/ARM_002dChars.html#ARM_002dChars说:

Either '#' or '$' can be used to indicate immediate operands. '#' 或 '$' 可用于指示立即操作数。

For ARMv8 # is always optional对于 ARMv8 #始终是可选的

https://sourceware.org/binutils/docs-2.26/as/AArch64_002dChars.html#AArch64_002dChars documents: https://sourceware.org/binutils/docs-2.26/as/AArch64_002dChars.html#AArch64_002dChars文档:

The '#' can be optionally used to indicate immediate operands.可以选择使用“#”来指示立即操作数。

Test测试

Ubuntu 16.04, Binutils 2.26.1. Ubuntu 16.04,Binutils 2.26.1。

v7.S: v7.S:

   /* These fail */
   mov r0, 1
   mov r0, 0x1

   /* These work */
   mov r0, #1
   mov r0, #0x1
   mov r0, $1
   mov r0, $0x1
.syntax unified
   mov r0, 1
   mov r0, #1
   mov r0, 0x1
   mov r0, #0x1
   mov r0, $1
   mov r0, $0x1

v8.S: v8.S:

   mov x0, 1
   mov x0, #1
   mov x0, 0x1
   mov x0, #0x1

Assemble:集合:

arm-linux-gnueabi-as v7.S
aarch64-linux-gnu-as v8.S

Outcome: v8 suceeds, v7 fails on the divided lines without # :结果:v8 成功,v7 在没有#divided线上失败:

v7.S:1: Error: immediate expression requires a # prefix -- `mov r0,1'
v7.S:2: Error: immediate expression requires a # prefix -- `mov r0,0x1'

TODO去做

Hmmm, but there are some v7 instructions for which # is actually optional, eg there are no errors for movw and movt :嗯,但有一些 v7 指令实际上#是可选的,例如movwmovt没有错误:

   movw r0, 1
   movt r0, 0x1

but there are errors for:但有以下错误:

   movw r0, $1
   movt r0, $0x1

ARM reference manual ARM 参考手册

TheARMv8-fb manual has assembly/disassembly recommentations/requirements in itself, at C1.2 "Structure of the A64 assembler language": ARMv8-fb 手册本身有汇编/反汇编建议/要求,在 C1.2“A64 汇编语言的结构”:

The A64 assembly language does not require the # character to introduce constant immediate operands, but an assembler must allow immediate values introduced with or without the # character. A64 汇编语言不需要 # 字符来引入常量立即数操作数,但汇编程序必须允许使用或不使用 # 字符引入立即数。 Arm recommends that an A64 disassembler outputs a # before an immediate operand. Arm 建议 A64 反汇编器在立即操作数之前输出 #。

Personal recommendation个人推荐

Use .syntax unified in your v7 code, and never use # on any literal on either v7 or v8.在 v7 代码中使用.syntax unified ,不要在 v7 或 v8 的任何文字上使用#

Unified syntax is newer and better, and those # and $ signs are just more code noise.统一语法更新更好,而那些#$符号只是更多的代码噪音。

The Linux kernel agrees with me: https://github.com/torvalds/linux/blob/v4.19/arch/arm/include/asm/unified.h#L23 Linux内核同意我: https : //github.com/torvalds/linux/blob/v4.19/arch/arm/include/asm/unified.h#L23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM