简体   繁体   English

在 GAS.intel-syntax 程序集中在 memory 标签和寄存器上使用“偏移”和“[]”有什么区别?

[英]What is the difference between using “offset” and “[ ]” on memory labels and registers in GAS .intel-syntax assembly?

Using GCC, compiling for 32-bit Intel architecture, with .intel_syntax noprefix .使用 GCC,编译为 32 位 Intel 架构,使用.intel_syntax noprefix Let's suppose I have the following .data section:假设我有以下.data部分:

A: .int 0x1, 0x2

What is the difference between the following, when used as an operand: A , [A] , offset A ?当用作操作数时,以下内容有什么区别: A[A]offset A Also, what is the difference when [..] is used on a register and on a memory label?此外,在寄存器和 memory label 上使用[..]时有什么区别?

The difference between writing the register simply and putting it into [..] was answered here well.简单地编写寄存器和将其放入[..]之间的区别在这里得到了很好的回答。

Let me give you a brief explanation.让我给你一个简短的解释。
[] brackets mean something like "do not work with the content, but with the address". [] 括号的意思是“不要使用内容,而是使用地址”。 When used labelled DATA, you can omit them (it depends on your assembler's syntax, but in MASM it definitely works like that).当使用带标签的 DATA 时,您可以省略它们(这取决于您的汇编器的语法,但在 MASM 中它肯定是这样工作的)。 Why?为什么? There is no way of working with data in memory directly;无法直接处理 memory 中的数据; instead, you just work with a data somewhere in memory (on some address) .相反,您只需使用memory (on some address) 中某处的数据。 So no disambiguity can happen, you always work with data on address.因此,不会发生歧义,您始终使用地址数据。 When you use them with registers, it's a quite different story:当您将它们与寄存器一起使用时,情况就完全不同了:

MOV EAX, 10

simply loads 10 (0x0000000A) in EAX register.只需在 EAX 寄存器中加载 10 (0x0000000A)。 You work directly with the register.您直接使用收银机。 But:但:

MOV EAX, 666
MOV BYTE PTR [EAX], 77

loads 77 into memory adress 666. The BYTE PTR directive is necessary, because assembler doesn't know if it should use 1, 2, 4 etc. bytes.将 77 加载到 memory 地址 666 中。 BYTE PTR指令是必需的,因为汇编程序不知道它是否应该使用 1、2、4 等字节。 The [EAX] says "do not work with EAX, instead, work with ADDRESS (memory location) contained in EAX. [EAX]说“不要使用 EAX,而是使用 EAX 中包含的 ADDRESS(内存位置)。

If you want to find out a difference between [VAR] , VAR and OFFSET VAR , try to step-by-step this code:如果您想找出[VAR]VAROFFSET VAR之间的区别,请尝试逐步执行此代码:

.DATA
VAR DWORD 77

.CODE
    MOV EAX, VAR
    MOV EBX, OFFSET VAR
    MOV ECX, [VAR]
    MOV EDX, OFFSET [VAR]

You will clearly see the difference.你会清楚地看到差异。

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

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