简体   繁体   English

如果使用 Intel 语法,GAS 可以在非 Intel 系统上组装吗?

[英]Can GAS assemble on non-Intel systems if Intel syntax is used?

Can I use the Intel syntax with GAS (GNU Assembler) if I assemble on non-Intel systems?如果我在非 Intel 系统上组装,我可以将 Intel 语法与 GAS (GNU Assembler) 一起使用吗?

Essentially, I am starting out with assembly programming though I have many years of experience in higher-level languages and I would like to choose an assembler that is suitable for both x86 and ARM.本质上,我是汇编编程开始的,尽管我在高级语言方面有多年的经验,我想选择一个既适用于 x86 又适用于 ARM 的汇编程序。 Possibly other architectures as well but this is not a pressing need right now.可能还有其他架构,但这不是目前的紧迫需求。

So far I have been happy with NASM but I also need ARM so GAS seems a nice candidate.到目前为止,我对 NASM 感到满意,但我也需要 ARM,因此 GAS 似乎是一个不错的选择。 Not having written anything much complex yet I am equally open to AT&T as well as to Intel syntax.没有写过任何复杂的东西,但我对 AT&T 和 Intel 语法同样持开放态度。

The only thing I am not sure if can I use GAS for cross-architecture programming as well if I pick the Intel syntax?如果我选择 Intel 语法,我唯一不确定是否也可以使用 GAS 进行跨架构编程?

I realise that naturally instruction sets will be different but I would just like to settle on one tool to cover both the architectures - my thinking is that if with time I need a third one there will be less tooling to learn and maintain if I just do everything in GAS from day one.我意识到指令集自然会有所不同,但我只想选择一种工具来覆盖这两种架构——我的想法是,如果随着时间的推移我需要第三个,那么如果我只是这样做,学习和维护的工具就会减少从第一天开始,GAS 中的所有内容。

Intuitively, I would say that there should not be any obstacles to using Intel syntax instead of AT&T to assemble programs on ARM or other non-Intel architectures, because GAS likely builds AST and then emits architecture-specific code regardless of the frontend's syntax, but unfortunately I do not have access to an ARM system to try it out and, that notwithstanding, I would very much like to confirm it with more knowledgeable people.直觉上,我会说使用 Intel 语法而不是 AT&T 在 ARM 或其他非 Intel 架构上组装程序应该没有任何障碍,因为 GAS 可能会构建 AST,然后不管前端的语法如何,都会发出特定于架构的代码,但是不幸的是,我无法访问 ARM 系统来试用它,尽管如此,我还是非常想与更多知识渊博的人确认一下。 Thank you.谢谢你。

EDIT: I came up with an analogy that may perhaps make it clearer what I have in mind.编辑:我想出了一个类比,可能会让我更清楚我的想法。 Supposing that someone learns to use vim effectively on Linux to work with Python, all that knowledge will transfer easily to Mac for development with Objective-C.假设有人学会了在 Linux 上有效地使用 vim 来使用 Python,那么所有这些知识将很容易转移到 Mac 以使用 Objective-C 进行开发。

Needless to say, Python and Objective-C are two distinctly different languages but still, the knowledge of tooling (vim, in this example) will be useful.不用说,Python 和 Objective-C 是两种截然不同的语言,但工具知识(在本例中为 vim)仍然很有用。

If one day the person needs to develop Java on Windows, the same will still hold and the programmer will be able to use vim or a derivative on the third system.如果有一天这个人需要在 Windows 上开发 Java,同样的情况仍然存在,程序员将能够在第三个系统上使用 vim 或衍生产品。

Ultimately, the actual differences between the languages, Python vs. Obj-C vs. Java will naturally dwarf any tooling-related issues but when someone is just starting out, the idea of using the same tool for several different needs is quite appealing.最终,语言之间的实际差异,Python 与 Obj-C 与 Java,自然会使任何与工具相关的问题相形见绌,但当有人刚开始时,使用相同工具满足多种不同需求的想法非常有吸引力。

This is the kind of reusability that I am thinking of.这就是我正在考虑的可重用性。

Some of your intuition is right.你的一些直觉是对的。 Gas certainly does support multiple architectures , and there are core features like assembler directives which will enable you to transfer some working knowledge between architecture ports of Gas. Gas 当然支持多种架构,并且有一些核心功能,比如汇编指令,可以让你在 Gas 的架构端口之间转移一些工作知识。 Certain command line concepts will be shared;某些命令行概念将被分享; others will differ by what the architecture port maintainers were thinking at the time.其他人会因架构端口维护者当时的想法而有所不同。

Other aspects of your question, particularly the question of AT&T syntax versus Intel syntax are not well placed for the question you're asking.您问题的其他方面,尤其是 AT&T 语法与 Intel 语法的问题并不适合您提出的问题。 I prefer to think of these as dialects of x86 with the challenge being learning the instruction set.我更愿意将这些视为 x86 的方言,挑战在于学习指令集。 What you're asking with respect to changing architecture is more fundamental;您对更改架构的要求更为基本; you're going to learn a new “language” each time, with Gas directives like .balign acting like the only common punctuation marks between those languages.你每次都会学习一种新的“语言”,像.balign这样的 Gas 指令就像这些语言之间唯一常见的标点符号。

Gas does require building a separate version for each architecture you want to target. Gas 确实需要为您想要定位的每个架构构建一个单独的版本。 On any individual system, you are probably operating “natively” as in your want to write assembler for your current machine.在任何单独的系统上,您可能正在“本地”操作,就像您想为当前机器编写汇编程序一样。 That's not the only way to use Gas, so one way to “trial” it if you don't have an Arm machine to hand would be to install a cross-assembler (for example, on Ubuntu https://packages.ubuntu.com/focal/binutils-arm-linux-gnueabi ).这不是使用 Gas 的唯一方法,因此,如果您手头没有 Arm 机器,“试用”它的一种方法是安装交叉汇编器(例如,在 Ubuntu https://packages.ubuntu 上。 com/focal/binutils-arm-linux-gnueabi )。

I would like to choose an assembler that is suitable for both x86 and ARM我想选择一个同时适用于 x86 和 ARM 的汇编程序

What exactly do you want to do?你到底想做什么?

  1. Writing programs on an ARM computer (in x86 syntax!) that shall later run on an x86 PC or writing programs on an x86 PC (in ARM syntax!) that shall later run on an ARM CPU?在 ARM 计算机上编写程序(以 x86 语法!)稍后将在 x86 PC 上运行,或者在 x86 PC 上编写程序(以 ARM 语法!)稍后将在 ARM CPU 上运行?
  2. Writing assembly programs that shall run both on an ARM and on an x86 PC?编写可以在 ARM 和 x86 PC 上运行的汇编程序?

If the answer is 1.:如果答案是 1.:

Many CPUs in smaller devices (for example WLAN routers or smartpones) are ARM CPUs.小型设备(例如 WLAN 路由器或智能手机)中的许多 CPU 都是 ARM CPU。 However, you want to develop programs for such devices on your PC, which has an x86 CPU.但是,您希望在具有 x86 CPU 的 PC 上为此类设备开发程序。

What you do is using a GAS version with the "target" ARM and the "host" x86.您所做的是使用带有“目标”ARM 和“主机”x86 的 GAS 版本。 This means that GAS is running on an x86 CPU but generates code for an ARM CPU.这意味着 GAS 在 x86 CPU 上运行,但为 ARM CPU 生成代码。

However, your "source code" (assembly program) must be an ARM assembly program.但是,您的“源代码”(汇编程序)必须是 ARM 汇编程序。

As far as I know, GAS supports only one syntax variant for ARM CPUs;据我所知,GAS 只支持 ARM CPU 的一种语法变体; there is nothing like the "AT&T" syntax for ARM.没有什么比 ARM 的“AT&T”语法更好的了。

If you have an ARM computer and you want to write x86 programs on it, you can of course use a GAS version with the "target" x86 and the "host" ARM.如果您有一台 ARM 计算机并且想在其上编写 x86 程序,您当然可以使用带有“目标”x86 和“主机”ARM 的 GAS 版本。 If the "target" is x86, GAS supports "AT&T" and "Intel" syntax independently of the "host".如果“目标”是 x86,则 GAS 支持独立于“主机”的“AT&T”和“Intel”语法。

If the answer is 2.:如果答案是 2.:

This won't work!这行不通!

In assembly language, one assembly instruction typically represents one instruction of the CPU.在汇编语言中,一条汇编指令通常代表CPU的一条指令。 And different CPU architectures have completely different instructions and therefore completely different assembly code.而且不同的CPU架构有完全不同的指令,因此也有完全不同的汇编代码。

Here an example program for x86 and for ARM:这里有一个 x86 和 ARM 的示例程序:

Intel CPU, Intel syntax         ARM (non-Thumb) CPU
-----------------------         -------------------
mov eax, 4
mov ebx, 1                      ldr r0, =1
mov ecx, offset myText          ldr r1, =myText
mov edx, 6                      ldr r2, =6
int 0x80                        svc #0x900004

shr edi, 6
add eax, edi                    add r0, r0, r6, lsr #6

                                ldr r5, =someVariable
add [someVariable], eax         ldr r7, [r5]
                                add r7, r0
                                str r7, [r5]

You can see that there is no 1:1 relation for the instructions:可以看到指令没有 1:1 的关系:

The instruction add [someVariable], eax on the x86 requires 4 instructions on the ARM; x86 上的指令add [someVariable], eax在 ARM 上需要 4 条指令; the instruction add r0, r0, r6, lsr #6 on the ARM requires 2 instructions on the x86. ARM 上的指令add r0, r0, r6, lsr #6在 x86 上需要 2 条指令。

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

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