0

I want to load a 64 bit immediate to %rax and another to %rax. I am using gas. The problem is that gas will happily load a 64bit immediate to %rax or load a 32bit immediate to %rbx but it will refuse to generate machine code to load a 64bit immediate to %rbx. What is going on and how can I get around this?

$ echo "movq 0x100000000,%rax" > function.asm && as function.asm -o function.bin && objdump -D function.bin

function.bin:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <.text>:
   0:   48 a1 00 00 00 00 01    movabs 0x100000000,%rax
   7:   00 00 00 

$ echo "movq 0x100000000,%rbx" > function.asm && as function.asm -o function.bin && objdump -D function.bin
function.asm: Assembler messages:
function.asm:1: Error: operand type mismatch for `movq'

$ echo "movq 0x10000,%rbx" > function.asm && as function.asm -o function.bin && objdump -D function.bin

function.bin:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <.text>:
   0:   48 8b 1c 25 00 00 01    mov    0x10000,%rbx
   7:   00 

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
fakedrake
  • 6,528
  • 8
  • 41
  • 64
  • 10
    You’re not loading an immediate. Those instructions do a memory read. There’s a special encoding just for al/ax/eax/rax to load from an absolute address. – prl Jun 01 '21 at 19:47
  • 2
    `movq 0x100000000,%rbx` -> `movq $0x100000000,%rbx` – mediocrevegetable1 Jun 01 '21 at 19:48
  • What does the dollar sign mean in this context? – fakedrake Jun 01 '21 at 19:56
  • 3
    Dollar sign in AT&T syntax indicates an immediate value. It's also used if you put some expression of constants like `label + 1` or `1 + 1`. – ecm Jun 01 '21 at 19:57
  • 1
    Use Intel-syntax disassembly (`objdump -drwC -Mintel`) to see the `movabs rax, [0x100000000]` load wrote. The `[]` makes it clear it's a memory operand. [Difference between movq and movabsq in x86-64](https://stackoverflow.com/q/40315803) mentions the `moffs` encodings of `movabs` that allow a 64-bit absolute address to load/store the accumulator. https://www.felixcloutier.com/x86/mov – Peter Cordes Jun 01 '21 at 21:05
  • 2
    [Load from a 64-bit address into other register than rax](https://stackoverflow.com/q/19415184) is a better duplicate, showing the form of the instruction you're using with RAX. – Peter Cordes Jun 02 '21 at 01:32

0 Answers0