上的操作数不匹配

上的操作数不匹配

本文介绍了x86_64 无法将 64 位值添加到 rax,“'add' 上的操作数不匹配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试组装一些 64 位代码,但组装失败了:

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

addq $0xffffff7fc0005000, %rax

错误:

`add' 的错误操作数类型不匹配

第一个操作数是一个 64 位值,后者是一个应该可以正确组装的寄存器.该指令前面是 .code64 伪操作.我正在组装

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

至于汇编器本身,当使用 --version 调用时,它返回:

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'.

推荐答案

不应该,add 的 64 位版本可以使用 8 位或 32 位符号扩展立即数,但不能使用 64 位立即数,这在 x64 中通常很少见.这可以通过查看英特尔软件开发人员手册或其他各种地方来验证,例如 此在线副本添加引理.

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.

唯一可以获取完整 64 位立即数(不是 32 位符号扩展)的指令是 mov.(或 movabs 因为 GAS 喜欢调用这个变体,尽管有一个大的数字常量 mov $0x123456789abc, %rcx 将选择必要的编码,而不是截断数字).

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).

通常你会 mov 将 64 位立即数放入寄存器中,然后使用它.

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

另一种选择是将值放入内存中,然后在带有内存操作数的 add 中使用它.

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

这篇关于x86_64 无法将 64 位值添加到 rax,“'add' 上的操作数不匹配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:14