NASM中的equ和db有什么区别? [英] What's the difference between equ and db in NASM?
问题描述
len: equ 2
len: db 2
它们是否相同,产生可以代替2
使用的标签?如果不是,那么每种声明形式的优点或缺点是什么?它们可以互换使用吗?
第一个是equate
,类似于C:
#define len 2
因为它实际上并没有在最终代码中分配任何空间,它只是将len
符号设置为等于2.然后,当您稍后在源代码中使用len
时,它是相同的就像您使用常量2
.
第二个是define byte
,类似于C:
int len = 2;
它确实分配了空间,即在内存中分配了一个字节,在其中存储了一个2
,并将len
设置为该字节的地址.>
以下是一些伪汇编程序代码,它们显示了区别:
line addr code label instruction
---- ---- -------- ----- -----------
1 0000 org 1234
2 1234 elen equ 2
3 1234 02 dlen db 2
4 1235 44 02 00 mov ax elen
5 1238 44 34 12 mov ax dlen
第1行只是将程序集地址设置为1234
,以便于解释发生的情况.
在第2行中,没有代码生成,汇编器仅将elen
的值2
加载到符号表中.由于未生成任何代码,因此地址不会更改.
然后,当您在第4行使用它时,它将把该值加载到寄存器中.
第3行表明db
是不同的,它实际上分配了一些空间(一个字节)并将值存储在该空间中.然后,它将dlen
加载到符号表中,但为其提供该地址1234
的值,而不是常量值2
.
稍后在第5行上使用dlen
时,您将获得地址,必须将该地址取消引用才能获取实际值2
.
len: equ 2
len: db 2
Are they the same, producing a label that can be used instead of 2
? If not, then what is the advantage or disadvantage of each declaration form? Can they be used interchangeably?
The first is equate
, similar to C's:
#define len 2
in that it doesn't actually allocate any space in the final code, it simply sets the len
symbol to be equal to 2. Then, when you use len
later on in your source code, it's the same as if you're using the constant 2
.
The second is define byte
, similar to C's:
int len = 2;
It does actually allocate space, one byte in memory, stores a 2
there, and sets len
to be the address of that byte.
Here's some psuedo-assembler code that shows the distinction:
line addr code label instruction
---- ---- -------- ----- -----------
1 0000 org 1234
2 1234 elen equ 2
3 1234 02 dlen db 2
4 1235 44 02 00 mov ax elen
5 1238 44 34 12 mov ax dlen
Line 1 simply sets the assembly address to be 1234
, to make it easier to explain what's happening.
In line 2, no code is generated, the assembler simply loads elen
into the symbol table with the value 2
. Since no code has been generated, the address does not change.
Then, when you use it on line 4, it loads that value into the register.
Line 3 shows that db
is different, it actually allocates some space (one byte) and stores the value in that space. It then loads dlen
into the symbol table but gives it the value of that address 1234
rather than the constant value 2
.
When you later use dlen
on line 5, you get the address, which you would have to dereference to get the actual value 2
.
这篇关于NASM中的equ和db有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!