请问X64环境中MOV指令使用64位立即数寻址,为什么总是会被GCC截断为32位

gxf_49 2009-04-08 11:31:21
代码很简单

movq 0x1234567890abcdef, %rax

经gcc(gas)编译后,objdump。发现代码段发生操作数低32位截断。即立即数只剩下

90 78 56 34 12

但是如果mov一个立即数到rax,操作却没有任何问题:

movq $0x1234567890abcdef, %rax

实在是很不明了其中原由。在Intel和AMD的文档上都没有看到不支持64位立即数寻址一说。非常怀疑操作码有问题。

请各位指教啊!
...全文
777 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxf_49 2009-04-08
  • 打赏
  • 举报
回复
那需要使用64位立即数寻址的时候,应该怎样写指令呢?
jxc25 2009-04-08
  • 打赏
  • 举报
回复
就象[bp]立即变成[bp+0]一样.
8位运算是标准运算
x86是现行标准
gxf_49 2009-04-08
  • 打赏
  • 举报
回复
感谢大家,问题解决了,在网上找到一些关于64bit addressing的描述,

http://x86asm.net/articles/what-i-dislike-about-gas/index.html

x86-64 addressing

Adding support for AMD64 architecture caused great troubles in all assemblers. AMD64 provides RIP-relative addressing. With this addressing, you can make position-independent code very easily. This addressing is what you want 99.9% times when writing 64-bit code. Using absolute addresses is still possible, but pretty much limited to 4GB address space, and they require relocations. The only exceptions are mov instructions (opcodes A0h-A3h), where absolute 64-bit addressing is enabled.

Some assemblers (YASM, GAS) still use absolute addresses by default in 64-bit mode, and they require explicit notation for RIP-relative addressing. This makes writing position-independent code pain.

mov variable(%rip), %eax

Other assemblers (FASM, MASM) use RIP-relative addressing by default. MASM doesn't provide any way to use absolute addresing, and FASM does offer way to use it, in rare cases when it is needed.

As a minor problem, GAS doesn't provide some exotic 64-bit addressing modes. These are not really needed, but would be nice for sake of completness of assembler.

总结而言,可以这样描述:
1. 由于PIC Code在64位Linux下总是存在的,所以64位系统下,汇编器(当然配合编译器)总是会默认产生rip-relative寻址的mov指令,而非64bit absolute寻址模式的指令。
2. 从指令集而言(AMD64),64bit的absolute寻址只有少数mov(OPCODE A0h-A3h)支持。
3. GAS没有提供64bit absolute寻址模式,即不会产生opcode为A0h-A3h的mov指令。 任何不理会该条的尝试都会导致GAS将oprand中的64bit地址截断为低32bit。
4. 想要使用64bit absolute寻址,当前的选择可能只能是换一个汇编器,譬如FASM。

再次谢谢大家!
gxf_49 2009-04-08
  • 打赏
  • 举报
回复
我用的就是生成64bit binary的gcc,还试验过gas和nasm。Assembler在text段的操作数位置始终都指保留了32bit的数据,然后运算的时候使用符号扩展到64位。
难道非得用手写binary不成?
jxc25 2009-04-08
  • 打赏
  • 举报
回复
x64没有的64位偏移
偏移有8位(标准)
有32位(现行)
有16位(已淘汰)
有64位(未生产)
dalixux 2009-04-08
  • 打赏
  • 举报
回复
找GCC的64位版本

21,500

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧