YASM/NASM x86组件中立即数与方括号的基本用法 [英] Basic use of immediates vs. square brackets in YASM/NASM x86 assembly
问题描述
假设我声明了以下内容:
Suppose I have the following declared:
section .bss
buffer resb 1
这些说明位于section .text
中:
mov al, 5 ; mov-immediate
mov [buffer], al ; store
mov bl, [buffer] ; load
mov cl, buffer ; mov-immediate?
我正确理解bl将包含值5,而cl将包含变量buffer
的内存地址吗?
Am I correct in understanding that bl will contain the value 5, and cl will contain the memory address of the variable buffer
?
我对
- 将立即数移到寄存器中,
- 将寄存器移至立即数(输入的是数据还是地址?)和
- 将立即数移到没有括号的寄存器中
- 例如
mov cl, buffer
vsmov cl, [buffer]
- moving an immediate into a register,
- moving a register into an immediate (what goes in, the data or the address?) and
- moving an immediate into a register without the brackets
- For example,
mov cl, buffer
vsmov cl, [buffer]
更新:阅读回复后,我认为以下摘要正确无误:
UPDATE: After reading the responses, I suppose the following summary is accurate:
-
mov edi, array
将第零个数组索引的内存地址放入edi
.即标签地址. -
mov byte [edi], 3
将值3放入数组的第零个索引 -
add edi, 3
之后,edi
现在包含数组第三个索引的内存地址 -
mov al, [array]
将第零个索引处的DATA装入al
. -
mov al, [array+3]
将第三个索引处的DATA装入al
. -
mov [al], [array]
无效,因为 x86无法对2个显式编码内存操作数,并且由于al
只有8位,即使在16位寻址模式下也无法使用. 引用内存位置的内容. (x86寻址模式) -
mov array, 3
无效,因为您不能说嘿,我不喜欢存储array
的偏移量,所以我将其称为3".立即数只能是源操作数. -
mov byte [array], 3
将值3放入数组的第零个索引(第一个字节). 需要byte
说明符,以避免字节之间的歧义/word/dword包含内存和立即操作数的指令.否则,这将是一个汇编时错误(操作数大小不明确).
mov edi, array
puts the memory address of the zeroth array index inedi
. i.e. the label address.mov byte [edi], 3
puts the VALUE 3 into the zeroth index of the array- after
add edi, 3
,edi
now contains the memory address of the 3rd index of the array mov al, [array]
loads the DATA at the zeroth index intoal
.mov al, [array+3]
loads the DATA at the third index intoal
.mov [al], [array]
is invalid because x86 can't encode 2 explicit memory operands, and becauseal
is only 8 bits and can't be used even in a 16-bit addressing mode. Referencing the contents of a memory location. (x86 addressing modes)mov array, 3
is invalid, because you can't say "Hey, I don't like the offset at whicharray
is stored, so I'll call it 3". An immediate can only be a source operand.mov byte [array], 3
puts the value 3 into the zeroth index (first byte) of the array. Thebyte
specifier is needed to avoid ambiguity between byte/word/dword for instructions with memory, immediate operands. That would be an assemble-time error (ambiguous operand size) otherwise.
如果其中任何一项为假,请提及. (编者注:我修复了语法错误/歧义,因此有效的错误实际上是有效的NASM语法.并链接了其他Q&A以获得详细信息)
Please mention if any of these is false. (editor's note: I fixed syntax errors / ambiguities so the valid ones actually are valid NASM syntax. And linked other Q&As for details)
推荐答案
实际上,您的想法是正确的.也就是说,bl将包含5和cl的缓冲区内存地址(实际上,标签缓冲区本身就是内存地址)
Indeed, your thought is correct.That is, bl will contain 5 and cl the memory address of buffer(in fact the label buffer is a memory address itself).
现在,让我解释一下您提到的操作之间的区别:
Now, let me explain the differences between the operations you mentioned:
-
将立即数移动到寄存器中可以使用
mov reg,imm
完成.可能令人困惑的是,例如缓冲区之类的标签本身就是包含地址的立即数.
moving an immediate into a register can be done using
mov reg,imm
.What may be confusing is that labels e.g buffer are immediate values themselves that contain an address.
您不能真正将寄存器移到立即数中,因为立即数是常量,例如
2
或FF1Ah
.您可以做的就是将寄存器移到常量所指向的位置.像mov [const], reg
一样.You cannot really move a register into an immediate, since immediate values are constants, like
2
orFF1Ah
.What you can do is move a register to the place where the constant points to.You can do it likemov [const], reg
.还可以使用
mov reg2,[reg1]
之类的间接寻址,只要reg1指向有效位置,它将reg1指向的值传输到reg2.You can also use indirect addressing like
mov reg2,[reg1]
provided reg1 points to a valid location, and it will transfer the value pointed by reg1 to reg2.因此,
mov cl, buffer
会将缓冲区的地址移动到cl(由于cl只有一个字节长,它可能会或可能不会提供正确的地址),而mov cl, [buffer]
会得到实际值.So,
mov cl, buffer
will move the address of buffer to cl(which may or may not give the correct address, since cl is only one byte long) , whereasmov cl, [buffer]
will get the actual value.- 使用[a]时,指的是指向某处的值.例如,如果a为
F5B1
,则[a]指的是 RAM中的地址F5B1 . - 标签是地址,即
F5B1
之类的值. - 因为寄存器没有地址,所以不必将存储在寄存器中的值称为[reg].实际上,可以将寄存器视为立即值.
- When you use [a], then you refer to the value at the place where a points to.For example, if a is
F5B1
, then [a] refers to the address F5B1 in RAM. - Labels are addresses,i.e values like
F5B1
. - Values stored in registers do not have to be referenced to as [reg] because registers do not have addresses.In fact, registers can be thought of as immediate values.
这篇关于YASM/NASM x86组件中立即数与方括号的基本用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- For example,
- 例如