Load from a 64-bit address into other register than rax

On x64, loading from a 64-bit absolute address (that is, dereferencing a 64-bit immediate) can be done by

movabs addr64, %rax

However, when the destination register is any other than rax the assembler gives an error message saying operand size mismatch for movabs. What am I missing?


From the MOV instruction document you can see that you can move a 64-bit immediate to any registers, but loads/stores involving a 64-bit immediate absolute address can only work with Areg

Opcode Instruction Description
A0 MOV AL,moffs8* Move byte at (seg:offset) to AL.
REX.W + A0 MOV AL,moffs8* Move byte at (offset) to AL.
A1 MOV AX,moffs16* Move word at (seg:offset) to AX.
A1 MOV EAX,moffs32* Move doubleword at (seg:offset) to EAX.
REX.W + A1 MOV RAX,moffs64* Move quadword at (offset) to RAX.
A2 MOV moffs8,AL Move AL to (seg:offset).
REX.W + A2 MOV moffs8***,AL Move AL to (offset).
A3 MOV moffs16*,AX Move AX to (seg:offset).
A3 MOV moffs32*,EAX Move EAX to (seg:offset).
REX.W + A3 MOV moffs64*,RAX Move RAX to (offset).

As you can see there's no ModR/M byte to encode the register number. Since this is less commonly used than moving a 64-bit immediate to a register it won't be a problem. If really needed it can be done in 2 instructions

MOVABS is the GAS opcode name for the MOV opcode forms MOV Areg, [Offs64] and MOV [Offs64], Areg. In Yasm's NASM syntax mode, you can get this form by saying MOV AX, [qword xxx]. Yasm's GAS syntax mode accepts MOVABS (for GAS compatibility). Note this form is only valid with Areg (AL/AX/EAX/RAX) as the source/destination register.

http://cvs.tortall.net/pipermail/yasm-devel/2006-March/000579.html


For any register other than %rax one can replace it by two instructions:

48 bb f0 de bc 9a 78 56 34 12   mov    $0x123456789abcdef0,%rbx
48 8b 1b                        mov    (%rbx),%rbx

This is longer than the single one,

48 a1 f0 de bc 9a 78 56 34 12   mov    0x123456789abcdef0,%rax

hence you'd quite probably prefer movabs (the latter) if you can use it.