Reputation: 159
I'm writing a jit compiler (64bit only, windows). I need to access variables of the c++ program, by reference (pointer).
Example: Adding a 32bit integer from memory to a 32bit register, using this instruction:
opcode "03 /r" : ADD r32, m32. (valid in 64bit mode)
unfortunately this is asking for a 32bit displacement, but I (only?) have a 64bit pointer to the c++ variable.
My question: is there some way to get a 32bit displacement from a 64bit c++ pointer?
Or if not, more generally, how would I adress c++ variables?
I also tried to mov the pointer value to rax and use indirect adressing [rax]. Which doesn't seem to work either.
I've looked at some dis-assembly (clang), and it seems to use RIP (relative instruction pointer), such as
mov dword ptr [rip + test], 2358
Which seems odd, as rip changes on every instruction (as far as I know).
Any pointers in the right direction would be greatly appreciated.
Edit: Indirect adressing through, [rax], is working now! I had a bug in the c++. Still no luck with 32bit displacement adressing.
Upvotes: 2
Views: 751
Reputation: 159
Just found the answer in the Intel Developer Manual vol 1 page 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."
And in the Intel Instruction Set Reference Manual page 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."
So disp32 can be used in 64bit mode only for relativ to instruction pointer adressing.
Upvotes: 1