问题描述
GNU 汇编器在汇编 Intel 语法代码时给出了意外的内存操作数.
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.
以下文本驻留在文件 code.asm 中:
The following text resided in the file code.asm:
.intel_syntax noprefix
.global somecode
somecode:
int 3
mov rax,qword [rcx]
ret
.att_syntax
汇编和反汇编code.asm:
Assembling and disassembling code.asm with:
as code.asm -o code1.obj -64
objdump -Mintel -d code1.obj > code1.asm
code1.asm(带反汇编代码)的内容为:
The content of code1.asm (with the disassembled code) is:
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
我使用的是 GNU 汇编器 (GNU Binutils) 2.25 (`x86_64-pc-cygwin').
I'm using GNU assembler (GNU Binutils) 2.25 (`x86_64-pc-cygwin').
问题:为什么在内存操作数 QWORD PTR [rcx+0x8] 中有一个额外的 qword 偏移量(8 字节)?我期待 mov rax,QWORD PTR [rcx].
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].
我一定是做错了什么.所以我与另一位受人尊敬的汇编程序 Yasm 进行了交叉检查并运行:
I must be doing something wrong. So I cross-checked with another respected assembler Yasm and ran:
yasm -f x64 -o code2.obj --parser=gas code.asm
objdump -Mintel -d code2.obj > code2.asm
code2.asm 的内容为:
The content of code2.asm is:
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
关于内存操作数,这正是我所期望的.我如何指示 GNU 做同样的事情?
With regard to the memory operand, this is what I expected. How can I instruct GNU to do the same?
推荐答案
你需要写mov rax, qword ptr [rcx]
.显然 qword
本身解析为大小,即.8
,所以你的代码汇编为 mov rax, 8[rcx]
.确实,mov rax, qword
也汇编为 mov rax, 8
.
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
.
有趣的是你的交叉检查"如何使用正确的语法:)
It's funny how your "cross check" used the proper syntax :)
这篇关于Gnu 汇编程序给出了意外的内存操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!