[var] 和 var 之间的汇编差异 [英] Assembly difference between [var], and var
问题描述
我正在学习汇编程序,但实际上我对 [variable]
和 variable
之间的区别一无所知.正如教程所说,两者都是指针,那么这有什么意义呢?为什么我必须在 []
之前使用 type Identifier
?我的汇编程序:nasm x86_64 在 Linux 上运行-->Ubuntu
I'm learning Assembler and getting to the point where I actually have no clue about the difference between [variable]
and variable
. As the tutorials say, both are pointers, so what is the point of this? And why do I have to use a type Identifier
before []
?
my assembler: nasm x86_64 running on Linux--> Ubuntu
推荐答案
在 x86 Intel 语法中 [expression]
表示地址 expression
处的内存内容.
(除了在 MASM 中,当 expression
是没有寄存器的数字文字或 equ
常量时,那么它仍然是立即的)
In x86 Intel syntax [expression]
means content of memory at address expression
.
(Except in MASM when expression
is a numeric literal or equ
constant with no registers, then it's still an immediate)
expression
不带括号取决于您使用的汇编程序.
expression
without brackets depends on Assembler you are using.
NASM 风格(NASM、YASM):
NASM-style (NASM, YASM):
mov eax,variable ; moves address of variable into eax
lea eax,[variable] ; equivalent to the previous one (LEA is exception)
mov eax,[variable] ; loads content of variable into eax
MASM 风格(还有 TASM 甚至 GCC/GAS .intel_syntax noprefix
):
MASM-style (also TASM and even GCC/GAS .intel_syntax noprefix
):
mov eax,variable ; load content of variable (for lazy programmers)
mov eax,OFFSET variable ; address of variable
lea eax,[variable] ; address of variable
mov eax,[variable] ; content of variable
GAS(AT&T 语法):这不是 Intel 语法,请参阅AT&T 标签维基.GAS 还使用不同的指令(例如 .byte
而不是 db
),即使在 .intel_syntax
模式下也是如此.
GAS (AT&T syntax): It's not Intel syntax, see the AT&T tag wiki. GAS also uses different directives (like .byte
instead of db
), even in .intel_syntax
mode.
在所有情况下,variable
都是符号的别名,用于标记内存中出现标签的特定位置.所以:
In all cases the variable
is alias for symbol marking particular place in memory, where the label appeared. So:
variable1 db 41
variable2 dw 41
label1:
在符号表中产生三个符号,variable1
、variable2
和label1
.
produces three symbols into symbol table, variable1
, variable2
and label1
.
当你在代码中使用它们中的任何一个时,比如 mov eax,
,它没有任何信息是由 db
或 定义的dw
或作为标签,所以当你做 mov [variable1],ebx
(覆盖定义的第一个字节之外的 3 个字节)时它不会给你任何警告.
When you use any of them in the code, like mov eax,<symbol>
, it has no information whether it was defined by db
or dw
or as label, so it will not give you any warning when you do mov [variable1],ebx
(overwriting 3 bytes beyond the defined first byte).
它只是内存中的一个地址.
It's simply just an address in memory.
(MASM 除外,其中数据部分中标签后的 db 或 dd 确实将大小与其变量名称"相关联.)
(Except in MASM, where the db or dd after a label in a data section does associate a size with it that "variable name".)
当不能从指令操作数本身推导出类型时,类型标识符仅在大多数汇编器中是必需的.
Type identifier is only required in most of the assemblers when the type can't be deduced from the instruction operands itself.
mov [ebx],eax ; obviously 32 bits are stored, because eax is 32b wide
mov [ebx],1 ; ERROR: how "wide" is that immediate value 1?
mov [ebx],WORD 1 ; NASM syntax (16 bit value, storing two bytes)
mov WORD [ebx],1 ; NASM syntax (16 bit value, storing two bytes)
mov WORD PTR [ebx],1 ; MASM/TASM syntax
这篇关于[var] 和 var 之间的汇编差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!