assembly - 为什么这个编译器在翻译成NASM时会输出GCC错误?

标签 assembly compiler-errors nasm x86-64 code-translation

我正在研究 GCC 汇编输出,尝试使用快速整数平均值。这是我最初使用的 C 代码:

unsigned int average (unsigned int x, unsigned int y) {
    return (x&y)+((x^y)>>1);
}

这是它发出的程序集(使用 Intel 语法):

average:
  mov edx, edi
  and edi, esi
  xor edx, esi
  shr edx
  lea eax, [rdx+rdi]
  ret

当我将其翻译为 NASM 时:

average:
    mov edx, edi
    and edi, esi
    xor edx, esi
    shr edx, 1
    lea eax, [rdx+rdi]
    ret

它提示这个错误,与 lea 一致:

<source>:6: error: impossible combination of address sizes
<source>:6: error: invalid effective address

我对汇编不太熟悉,但这看起来非常奇怪。有人可以向我解释一下这是怎么回事吗?

最佳答案

该错误消息具有误导性。导致此错误的原因是 nasm 尝试将您的代码汇编为 16 位或 32 位代码,这两种代码都不支持 64 位寄存器。要解决此问题,请使用可使其汇编 64 位代码的选项调用 nasm,例如在 Linux 上:

nasm -f elf64 source.asm

或在 Windows 上:

nasm -f win64 source.asm

关于assembly - 为什么这个编译器在翻译成NASM时会输出GCC错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50728256/

相关文章:

assembly - 如何将存储在扩展寄存器(eax)中的值加载到浮点堆栈的st0中?

assembly - ASM:MASM,NASM,FASM?

memory-management - 在程序集中访问 malloc 内存

c - Mac OS X x86-64 中指令指针的寻址

c++ - C++错误: expected ',' or ';' before '{' token when their is

c++ - 编译在 g++ 4.7.1 中达到虚拟内存限制?

scala - Scala `Application does not take parameters`编译错误

linux - 无法打印在 NASM 堆栈上推送的换行符

assembly - MARS MIPS 汇编程序给出错误 : Extended (pseudo) instruction or format not permitted

c++ - 如何使用 asm Hook C++ 函数