关于 AT&T x86 语法设计的问题 [英] Questions about AT&T x86 Syntax design

查看:28
本文介绍了关于 AT&T x86 语法设计的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 谁能向我解释为什么 AT&T 语法中的每个常量前面都有一个$"?
  2. 为什么所有寄存器都有一个%"?
  3. 这是否只是让我做大量蹩脚打字的又一次尝试?
  4. 另外,我是不是唯一一个发现:16(%esp)[esp+16] 相比真的违反直觉的人吗?
  5. 我知道它编译成同样的东西,但为什么有人想要输入很多 '$' 和 '%' 而不需要呢?- 为什么 GNU 选择这种语法作为默认语法?
  6. 还有一件事,为什么 at&t 语法中的每条指令都以:l 开头?- 我知道它的操作数大小,但是为什么不让汇编程序弄清楚呢?(我是否想对不是那么大的操作数执行 movl?)
  7. 最后一件事:为什么 mov 参数颠倒了?
  1. Can anyone explain to me why every constant in AT&T syntax has a '$' in front of it?
  2. Why do all registers have a '%'?
  3. Is this just another attempt to get me to do a lot of lame typing?
  4. Also, am I the only one that finds: 16(%esp) really counterintuitive compared to [esp+16]?
  5. I know it compiles to the same thing but why would anyone want to type a lot of '$' and '%'s without a need to? - Why did GNU choose this syntax as the default?
  6. Another thing, why is every instruction in at&t syntax preceded by an: l? - I do know its for the operand sizes, however why not just let the assembler figure that out? (would I ever want to do a movl on operands that are not that size?)
  7. Last thing: why are the mov arguments inverted?

这不是更多合乎逻辑的:

eax = 5
mov eax, 5

at&t 在哪里:

where as at&t is:

mov 5, eax
5 = a (? wait what ?)

注意:我不是要拖钓.我只是不明白他们所做的设计选择,我想知道他们为什么要这样做.

Note: I'm not trying to troll. I just don't understand the design choices they made and I'm trying to get to know why they did what they did.

推荐答案

1, 2, 3 和 5:符号有点多余,但我发现在汇编中开发时这是一件好事.冗余有助于阅读.让汇编程序自己算出来"的观点很容易变成让读代码的程序员自己算出来",我不喜欢自己读.编程不是只写任务;就算是程序员自己也得自己读代码,语法冗余还是挺有帮助的.

1, 2, 3 and 5: the notation is somewhat redundant, but I find it to be a good thing when developing in assembly. Redundancy helps reading. The point about "let the assembler figure it out" easily turns into "let the programmer who reads the code figure it out", and I do not like it when I am the one doing the reading. Programming is not a write-only task; even the programmer himself must read his own code, and the syntax redundancy helps quite a bit.

另一点是 '%' 和 '$' 意味着可以在不破坏向后兼容性的情况下添加新寄存器:添加没有问题,例如,一个名为 xmm4 的寄存器,因为它将是写成 %xmm4,不能与名为 xmm4 的变量混淆,后者将不带%".

Another point is that the '%' and '$' mean that new registers can be added without breaking backward compatibility: no problem in adding, e.g., a register called xmm4, as it will be written out as %xmm4, which cannot be confused with a variable called xmm4 which would be written without a '%'.

至于打字量:通常情况下,在汇编编程时,瓶颈是大脑,而不是手.如果 '$' 和 '%' 减慢了你的速度,那么要么你思考的速度比通常认为对人类可行的速度快得多,要么,更有可能的是,你手头的任务太机械了,不应该在部件;它应该留给自动代码生成器,通俗地称为C 编译器".

As for the amount of typing: normally, when programming in assembly, the bottleneck is the brain, not the hand. If the '$' and '%' slow you down, then either you are thinking way faster than what is usually considered as doable for a human being, or, more probably, your task at hand is too mechanical and should not be done in assembly; it should be left to an automatic code generator, something colloquially known as a "C compiler".

'l' 后缀被添加来处理一些汇编程序无法"弄清楚的情况.例如,这段代码:

The 'l' suffix was added to handle some situations where the assembler "cannot" figure it out. For instance, this code:

mov  [esp], 10

是模棱两可的,因为它不会告诉您是要写入值为 10 的字节,还是要写入具有相同数值的 32 位字.然后英特尔语法要求:

is ambiguous, because it does not tell whether you want to write a byte of value 10, or a 32-bit word with the same numerical value. The Intel syntax then calls for:

mov  byte ptr [esp], 10

当你想到它时,这很丑陋.AT&T 的人想要做一些更理性的事情,所以他们想出了:

which is quite ugly, when you think about it. The people at AT&T wanted to make something more rational, so they came up with:

movb   $10, (%esp)

他们更喜欢系统化,并且无处不在后缀b"(或l"或w").请注意,后缀并不总是必需.例如,你可以写:

and they preferred to be systematic, and have the 'b' (or 'l' or 'w') suffix everywhere. Note that the suffix is not always required. For instance, you can write:

mov   %al, (%ebx)

并让 GNU 汇编程序弄清楚"既然您在谈论 '%al',则移动是针对单个字节的.真的行 !不过,我还是觉得指定大小比较好(这确实对读者有帮助,而程序员本人是自己代码的第一个也是最重要的读者).

and let the GNU assembler "figure out" that since you are talking about '%al', the move is for a single byte. It really works ! Yet, I still find it better to specify the size (it really helps the reader, and the programmer himself is the first and foremost reader of his own code).

对于反转":相反.Intel 语法模仿 C 中发生的情况,其中值在右侧计算,然后写入左侧的内容.因此,考虑到阅读是从左到右,写作是从右到左,在相反"的方向上.AT&T 语法恢复到正常"方向.至少他们是这么认为的;由于他们决定无论如何都使用自己的语法,因此他们认为可以按照他们认为的正确顺序"使用操作数.这主要是一种约定,但不是不合逻辑的约定.C 约定模仿数学符号,除了数学是关于定义值(让 x 成为值 5")而不是关于分配值(我们写值 5进入一个名为x"的插槽).AT&T 的选择是有道理的.只有当您将 C 代码转换为汇编代码时才会令人困惑,这项任务通常应该留给 C 编译器.

For the "inversion": it is the other way round. The Intel syntax mimics what occurs in C, in which values are computed on the right, then written to what is on the left. Thus, the writing goes right to left, in the "reverse" direction, considering that reading goes left-to-right. The AT&T syntax reverts to the "normal" direction. At least so they considered; since they were decided about using their own syntax anyway, they thought that they could use the operands in what they thought of as "the right ordering". This is mostly a convention, but not an illogical one. The C convention mimics mathematical notation, except that mathematics are about defining values ("let x be the value 5") and not about assigning values ("we write the value 5 into a slot called 'x'"). The AT&T choice makes sense. It is confusing only when you are converting C code to assembly, a task which should usually be left to a C compiler.

从历史的角度来看,问题 5 的最后一部分很有趣.x86 的 GNU 工具遵循 AT&T 语法,因为当时他们试图在 Unix 世界中占据一席之地(GNU"意味着GNU 不是 Unix")并与 Unix 工具竞争;Unix 由 AT&T 控制.这是在 Linux 甚至 Windows 3.0 出现之前;PC 是 16 位系统.Unix 使用 AT&T 语法,因此 GNU 使用 AT&T 语法.

The last part of your question 5 is interesting, from an historical point of view. The GNU tools for x86 followed the AT&T syntax because at that time, they were trying to take hold in the Unix world ("GNU" means "GNU is Not Unix") and competing with the Unix tools; Unix was under control of AT&T. This is before the days of Linux or even Windows 3.0; PC were 16-bit systems. Unix used the AT&T syntax, hence GNU used AT&T syntax.

那么好问题是:为什么 AT&T 认为发明自己的语法是明智之举?如上所述,他们有一些理由,但并非没有道理.当然,使用自己的语法的代价是限制了互操作性.在那个年代,C 编译器或汇编器作为单独的工具并没有什么实际意义:在 Unix 系统中,它们应该由操作系统供应商提供.此外,英特尔在 Unix 世界中并不是一个大玩家.大型系统主要使用 VAX 或 Motorola 680x0 衍生产品.没有人想到 MS-Dos PC 会在二十年后成为台式机和服务器领域的主导架构.

The good question is then: why did AT&T found it smart to invent their own syntax ? As described above, they had some reasons, which were not without merit. The cost of using your own syntax, of course, is that it limits interoperability. In those days, a C compiler or assembler made no real sense as a separate tool: in a Unix system, they were meant to be provided by the OS vendor. Also, Intel was not a big player in the Unix world; big systems mostly used VAX or Motorola 680x0 derivatives. Nobody had figured out that the MS-Dos PC would turn into, twenty years later, the dominant architecture in the desktop and server worlds.

这篇关于关于 AT&T x86 语法设计的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆