【问题标题】:Why does this compiler output by GCC error when translated into NASM?为什么这个编译器在翻译成 NASM 时会输出 GCC 错误?
【发布时间】:2018-11-16 14:28:33
【问题描述】:

我正在玩弄 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

我对汇编并不超级熟悉,但这似乎非常奇怪。有人可以向我解释一下这里到底发生了什么吗?

【问题讨论】:

  • 从 GCC 输出翻译/插入 NASM 时是否有其他输出有效?
  • 根据我的经验,它通常有,但不是 100% 的时间。我只是好奇是什么使 this 示例不起作用。
  • 我更新了问题名称和标签,以澄清我问的是失败的代码翻译,而不是简单的插入输出到输入。

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


【解决方案1】:

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

nasm -f elf64 source.asm

或在 Windows 上:

nasm -f win64 source.asm

【讨论】:

  • 超级很奇怪......我验证了这一点,事实就是如此。
猜你喜欢
  • 2021-11-28
  • 2014-03-24
  • 2012-01-28
  • 2023-02-01
  • 2012-02-08
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多