问题描述
我正在学习汇编程序,直到我对[variable]
和variable
之间的区别一无所知.正如教程所说,两者都是指针,那么这有什么意义呢?为什么我必须在[]
之前使用type Identifier
?我的汇编器:nasm x86_64 running on 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标签Wiki .即使在.intel_syntax
模式下,GAS也会使用不同的指令(例如.byte
而不是db
).
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,<symbol>
,它不知道它是由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之间的汇编差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!