assemblyx86-64gnu-assemblerimmediate-operand

x86_64 Cannot add 64 bit value to rax, "operand mismatch on 'add'"


I am trying to assemble some 64-bit code and the assembling fails on the line:

addq $0xffffff7fc0005000, %rax

with the error:

Error operand type mismatch for `add'

The first operand is a 64-bit value and the latter a register which should assemble fine. This instruction is preceded by a .code64 pseudo-op. I am assembling with

x86_64-elf-as test.s -o test.o --64

As for the assembler itself, when called with --version it returns:

GNU assembler (GNU Binutils) 2.32
Copyright (C) 2019 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `x86_64-elf'.

Solution

  • The first operand is a 64bit value and the latter a register which should assemble fine.

    It shouldn't, the 64bit version of add can take an 8bit or 32bit sign-extended immediate, but not a 64bit immediate, which in general is rare in x64. This can be verified by looking in the Intel Software Developer's Manual or various other places, such as this online copy of the ADD lemma.

    The only instruction that can take a full 64-bit immediate (not 32-bit sign-extended) is mov. (Or movabs as GAS likes to call this variant, although with a large numeric constant mov $0x123456789abc, %rcx will pick the necessary encoding, not truncate the number).

    Normally you'd mov a 64-bit immediate into a register and then use that.

    Another option is to put the value in memory and use it in an add with a memory operand.