简体   繁体   English

0和dword 0有什么区别?

[英]What's the difference between 0 and dword 0?

As the question states, What's the difference between (for example) mov eax, 0 and mov eax, dword 0 ? 正如问题所述,(例如) mov eax, 0mov eax, dword 0之间有什么区别?

I've been using cmp statements, and I can't catch the difference. 我一直在使用cmp语句,我无法发现差异。 Is one an address and the other a numerical value? 一个是地址而另一个是数值吗?

As stated, no difference for the MOV instruction. 如上所述,MOV指令没有区别。 For CMP, you would have difference between 对于CMP,你会有区别

CMP memory address of a qword,a 32 bit immediate 一个qword的CMP内存地址,一个32位立即数

is different from 不同于

CMP memory address of a qword,an 8 bit immediate 一个qword的CMP存储器地址,一个8位立即数

as the comparison will be done after a sign extension, and hence, with a smaller dimension immediate number, expecially when its a negative number, some caution is advised. 因为比较将在符号扩展之后进行,因此,使用较小的立即数,特别是当它为负数时,建议采取一些谨慎措施。

Have fun programming... 玩得开心编程......

It does not make a difference with a register since the name of the register already tells the assembler "how big" the data item (in this case, 0 ) is: 它与寄存器没有区别,因为寄存器的名称已经告诉汇编程序“有多大”数据项(在本例中为0 ):

mov   eax, dword 0    ; move a 4-byte 0 into eax
mov   eax, 0          ; move a 0 into eax (eax is 4 bytes, so move 4 bytes)

However, there are cases where you might want to be able to specify how large the value is since there's a choice. 但是,在某些情况下,您可能希望能够指定值的大小,因为可以选择。 In x86 32-bit assembly, the following would push a 4-byte 0 value on the stack: 在x86 32位汇编中,以下内容将在堆栈上推送一个4字节的0值:

push   0             ; push 4-byte 0 onto the stack

If you wanted to push a 2-byte 0 value, you would use: 如果你想推一个2字节的0值,你会使用:

push   word 0

If you wanted to be explicit so it's clear for the 4-byte immediate push, you could use the dword : 如果你想要明确,那么4字节立即推送是明确的,你可以使用dword

push   dword 0

If you want to move an immediate value to memory, the size specifier becomes necessary because the assembler doesn't know how large the data item is otherwise. 如果要将立即值移动到内存中,则必须使用大小说明符,因为汇编程序不知道数据项的大小。 For example, consider the following code in nasm assembly: 例如,请考虑nasm程序集中的以下代码:

        section  .bss
num     resb     4

        ...
        section  .text
        ...
        mov    [num], 0

This will generate an error: 这将产生一个错误:

 error: operation size not specified

So you need to specify the size, say, 4-bytes: 所以你需要指定大小,比如4字节:

        mov    [num], dword 0

They are exactly the same thing, it's just assembler syntax. 它们完全相同,它只是汇编语法。

As a side note, xor eax, eax is generally preferred since it generates a two byte instruction which is much smaller. 作为旁注, xor eax, eax通常是首选,因为它产生的双字节指令要小得多。

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

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