Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

assembly - 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'.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...