Assembly 64bit - movl, movq. Interchanging is okay?
You can use 32 bit registers and instructions in 64 bit mode, just like you can use 16 or 8 bit too. One thing to keep in mind is that 32 bit instructions will automatically zero the top 32 bits of the respective 64 bit registers, while 16 or 8 bit instructions don't:
movabsq $0xffffffffffffffff, %rax
movb $0, %al # rax = 0xffffffffffffff00
movw $0, %ax # rax = 0xffffffffffff0000
movl $0, %eax # rax = 0x0000000000000000
One reason to use 32 bit instructions is that they are shorter machine code.
Comments
-
Larry almost 2 years
Context:
Learning GAS assembly on 64 bit linux. Many tutorials are for 32-bit assembly.
Difficult to bushwhack through x86_64 assembly.
Question:
When I compile a c program with gcc, I still see some
%eax
andmovl
.I mostly play with
int32_t
.But I thought that one had to use the 64 bits instructions on
x86_64
(likerax
,rbx
and so on).I don't understand very well. Shouldn't they be
%rax
andmovq
? In my assembly programs, I should usemovq
, right ? even for 32 bits integers ? I am a lost beginner.Thanks