Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "mov eax, 0x1" always be used instead of "mov rax, 0x1"?

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?

like image 287
Tyilo Avatar asked Aug 06 '12 04:08

Tyilo


1 Answers

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.

like image 190
Alexey Frunze Avatar answered Sep 20 '22 14:09

Alexey Frunze