64 位指令中的 C++ 指针

C++ Pointers in 64bit instructions

我正在编写一个 jit 编译器(仅限 64 位,windows)。我需要通过引用(指针)访问 C++ 程序的变量。

示例:将 32 位整数从内存添加到 32 位寄存器,使用此指令:

opcode "03 /r" : ADD r32, m32. (valid in 64bit mode)

不幸的是,这是要求 32 位位移,但我(只有?)有一个指向 c++ 变量的 64 位指针。

我的问题:有什么方法可以从 64 位 C++ 指针获取 32 位位移?

或者,如果不是,更一般地说,我将如何处理 C++ 变量?

我还尝试将指针值移动到 rax 并使用间接寻址 [rax]。这似乎也不起作用。

我看过一些反汇编(clang),好像是用RIP(relative instruction pointer),比如

mov dword ptr [rip + test], 2358

这看起来很奇怪,因为 rip 在每条指令上都在变化(据我所知)。

任何正确方向的指示将不胜感激。

编辑:通过 [rax] 的间接寻址正在运行!我在 C++ 中有一个错误。 32 位位移寻址仍然不走运。

刚刚在英特尔开发人员手册第 1 卷第 70 页找到答案:

"Generally, displacements and immediates in 64-bit mode are not extended to 64 bits. They are still limited to 32 bits and sign-extended during effective-address calculations. In 64-bit mode, however, support is provided for 64-bit displacement and immediate forms of the MOV instruction."

并且在英特尔指令集参考手册第 42 页中:

"In 64-bit mode, the ModR/M Disp32 (32-bit displacement) encoding is re-defined to be RIP+Disp32 rather than displacement-only. See Table 2-7."

所以disp32只能在64位模式下使用相对于指令指针寻址。