Can “mov eax, 0x1” always be used instead of “mov

2019-06-22 06:27发布

问题:

When assembling this code with nasm:

BITS 64
mov eax, 0x1
mov rax, 0x1

I get this output:

b8 01 00 00 00 b8 01 00 00 00

which is the opcode for mov eax, 0x1 repeated twice.

Does this mean that mov rax, 0x1 can always be replaced by mov eax, 0x1 or is it just in this case?

If this is correct wouldn't it be better to use than:

xor rax, rax
inc rax

as that becomes 6 bytes when assembled while mov eax, 0x1 is only 5 bytes?

回答1:

Always. Most (if not all) 32-bit MOVs and ALU operations clear bits 32 through 63 of the destination register operand. See the CPU manual for details.