gpt4 book ai didi

gcc - ARM 汇编中的立即值是否需要哈希?

转载 作者:行者123 更新时间:2023-12-03 03:39:44 26 4
gpt4 key购买 nike

我一直在阅读 gcc 生成的一些不同的arm汇编代码,并且发现了一些我在规范中找不到的东西。

movw    r0, #39784
movt r0, 1

显然第一个是将值 39784 移动到底部 16 位或 r0,但是 movt 的操作数“1”很奇怪,因为它前面没有哈希值,而且我的印象是立即值需要哈希值。在某些情况下它是可选的吗?还是我错过了一些神奇的东西?

最佳答案

ARMv7 的 GNU gas 行为取决于 .syntax

文档说 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. The default, divided, uses the old style where ARM and THUMB instructions had their own, separate syntaxes. The new, unified syntax, which can be selected via the .syntax directive, and has the following main features:

  • Immediate operands do not require a # prefix.

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

Either '#' or '$' can be used to indicate immediate operands.

对于 ARMv8,# 始终是可选的

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

The '#' can be optionally used to indicate immediate operands.

测试

Ubuntu 16.04,Binutils 2.26.1。

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:

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

组装:

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

结果: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'

待办事项

嗯,但是有一些 v7 指令的 # 实际上是可选的,例如movwmovt 没有错误:

   movw r0, 1
movt r0, 0x1

但有错误:

   movw r0, $1
movt r0, $0x1

ARM引用手册

ARMv8-fb manual本身有汇编/反汇编建议/要求,位于 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. Arm recommends that an A64 disassembler outputs a # before an immediate operand.

个人推荐

在 v7 代码中使用 .syntax Unified,并且切勿在 v7 或 v8 上的任何文字上使用 #

统一语法更新更好,而那些 #$ 符号只是更多的代码噪音。

Linux 内核同意我的观点:https://github.com/torvalds/linux/blob/v4.19/arch/arm/include/asm/unified.h#L23

关于gcc - ARM 汇编中的立即值是否需要哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com