我可以在 x86 中编码 64 位负立即数吗? [英] Can I encode a 64-bit negative immediate in x86?
问题描述
我正在尝试自学 x86,但我遇到了需要立即返回负数的情况:
I'm trying to teach myself x86, and I've run across the need to return an immediate, negative number:
.text
.align 4,0x90
.globl _scheme_entry
_scheme_entry:
movl $-42, %eax
ret
当我打印这个函数的返回值时 (printf("%" PRIdPTR "
", scheme_entry())
,我得到一个无意义的数字:
When I print the return-value of this function (printf("%" PRIdPTR "
", scheme_entry())
, I get a nonsense number:
$ ./neg
4294967254
我猜这是因为它是一个 32 位负数,00000000FFFFFFFF,而不是一个 64 位负数,FFFFFFFFFFFFFFFF.
I'm guessing this is because it's a 32-bit negative, 00000000FFFFFFFF, instead of a 64-bit negative, FFFFFFFFFFFFFFFF.
如何在汇编函数中直接存储一个常量 64 位值?我是否必须将其编码为两个单独的指令?
How can I store a constant 64-bit value directly in an assembler function? Do I have to encode it as two separate instructions?
推荐答案
32 位指令的 32 位立即数(如 movl
)按原样使用,写入 EAX 将 RAX 的高 32 位归零.您没有胡说八道,您得到的是 2^32 - 42
,这正是您所期望的,因为 x86 使用 2 的有符号整数补码.
32-bit immediates for 32-bit instructions (like movl
) are used as-is, and writing EAX zeros the upper 32 bits of RAX. You're not getting nonsense, you're getting 2^32 - 42
, which is exactly what you should expect, because x86 uses 2's complement signed integers.
具有 64 位操作数大小的指令的 32 位立即数被符号扩展为 64 位.
32-bit immediates for instructions with 64-bit operand-size are sign-extended to 64 bits.
使用 mov $-42, %rax
返回负 64 位值,而不是略低于 2^32 的正 64 位值.(使用 %rax
作为目标意味着 64 位操作数大小,即 movq
.)
Use mov $-42, %rax
to return a negative 64-bit value, instead of a positive 64-bit value just slightly below 2^32. (Using %rax
as the destination implies a 64-bit operand size, i.e. movq
.)
使用调试器查看寄存器中的值.
Use a debugger to see values in registers.
这篇关于我可以在 x86 中编码 64 位负立即数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!