Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 579
Torgny Avatar asked Oct 16 '13 22:10

Torgny


People also ask

Is RAX a 64-bit register?

rax is the 64-bit, "long" size register. It was added in 2003 during the transition to 64-bit processors. eax is the 32-bit, "int" size register. It was added in 1985 during the transition to 32-bit processors with the 80386 CPU.

What is the RAX register used for?

The RAX register is used for return values in functions regardless of whether you're working with Objective-C or Swift.

What is MOV RAX?

In AT&T syntax, the instruction: mov (%rax), %eax # AT&T syntax. or, equivalently in Intel syntax: mov eax, DWORD PTR [rax] ; Intel syntax. dereferences the memory address stored in rax , reads a 32-bit value from that memory address, and stores it in the eax register.


1 Answers

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

like image 123
phuclv Avatar answered Oct 19 '22 03:10

phuclv