Gnu汇编程序给出了意外的内存操作数

时间:2021-02-24 03:14:21

The GNU assembler gives an unexpected memory operand when assembling Intel syntax code.

在组装英特尔语法代码时,GNU汇编程序会提供意外的内存操作数。

I have reduced my bug to one single lonely line of code, and for the last three days I've tried anything to understand why the GNU assembler yields something that I cannot understand. I know this must (or should) be trivial, but I'm at a loss.

我已经将我的错误减少到一个单独的代码行,并且在过去的三天里,我已经尝试过任何事情来理解为什么GNU汇编器会产生一些我无法理解的东西。我知道这必须(或应该)是微不足道的,但我不知所措。

The following text resided in the file code.asm:

以下文本位于文件code.asm中:

.intel_syntax noprefix
.global somecode
somecode: 
    int 3
    mov        rax,qword [rcx]
    ret
.att_syntax

Assembling and disassembling code.asm with:

使用以下代码组装和反汇编code.asm:

as code.asm -o code1.obj -64 
objdump -Mintel -d code1.obj > code1.asm

The content of code1.asm (with the disassembled code) is:

code1.asm(包含反汇编代码)的内容是:

code1.obj:     file format pe-x86-64
Disassembly of section .text:
0000000000000000 <somecode>:
   0:   cc                      int3 
   1:   48 8b 41 08             mov    rax,QWORD PTR [rcx+0x8]
   5:   c3                      ret    

I'm using GNU assembler (GNU Binutils) 2.25 (`x86_64-pc-cygwin').

我正在使用GNU汇编程序(GNU Binutils)2.25(`x86_64-pc-cygwin')。

Question: Why is there an extra one qword offset (8bytes) in the memory operand QWORD PTR [rcx+0x8]? I expect mov rax,QWORD PTR [rcx].

问:为什么内存操作数QWORD PTR [rcx + 0x8]中有一个额外的qword偏移量(8字节)?我期待mov rax,QWORD PTR [rcx]。

I must be doing something wrong. So I cross-checked with another respected assembler Yasm and ran:

我一定做错了什么。所以我和另一位受人尊敬的汇编Yasm交叉核对并跑了:

yasm -f x64 -o code2.obj --parser=gas code.asm
objdump -Mintel -d code2.obj > code2.asm

The content of code2.asm is:

code2.asm的内容是:

code2.obj:     file format pe-x86-64
Disassembly of section .text:
0000000000000000 <somecode>:
   0:   cd 03                   int    0x3
   2:   48 8b 01                mov    rax,QWORD PTR [rcx]
   5:   c3                      ret

With regard to the memory operand, this is what I expected. How can I instruct GNU to do the same?

关于内存操作数,这是我的预期。我如何指示GNU做同样的事情?

1 个解决方案

#1


You need to write mov rax, qword ptr [rcx]. Apparently qword by itself resolves to the size, ie. 8, so your code assembled as mov rax, 8[rcx]. Indeed, mov rax, qword also assembles as mov rax, 8.

你需要写mov rax,qword ptr [rcx]。显然qword本身解析为大小,即。 8,所以你的代码组装为mov rax,8 [rcx]。确实,mov rax,qword也组装为mov rax,8。

It's funny how your "cross check" used the proper syntax :)

你的“交叉检查”使用正确的语法:)这很有趣:)

#1


You need to write mov rax, qword ptr [rcx]. Apparently qword by itself resolves to the size, ie. 8, so your code assembled as mov rax, 8[rcx]. Indeed, mov rax, qword also assembles as mov rax, 8.

你需要写mov rax,qword ptr [rcx]。显然qword本身解析为大小,即。 8,所以你的代码组装为mov rax,8 [rcx]。确实,mov rax,qword也组装为mov rax,8。

It's funny how your "cross check" used the proper syntax :)

你的“交叉检查”使用正确的语法:)这很有趣:)