1

for a C++ project I'm currently working on I have the following situation: adr1 is an address in memory that will be randomly selected from a mapped memory for every run of my program. Now I want to write to this address using the MOVNTI (https://www.felixcloutier.com/x86/movnti) instruction. However, I'm pretty new to inline assembly and do not know which constraints and which data-type to use for this. My current code:

asm volatile(
"movntiq %%rax, %[in1] \n"
:
: [in1] "m" (adr1)
);

I tried using the "i" and "c" constraints for the input variable but the compiler does not write the assembly code I want to have. What I want to have written in the end is this (let's say adr1 = 0x12345678):

movntiq %%rax, 0x12345678

However, the compiler currently creates this:

movntiq %%rax, -8(%rbp)

How do I have to set the constraints and variable type of adr1 to achieve my goal?

OHithere
  • 41
  • 1
  • `What I want ...is ... movntiq %%rax, 0x12345678` - then the address would need to be known at compile-time, no? – 500 - Internal Server Error Sep 10 '21 at 11:44
  • 1
    Any reason you're not using the `_mm_stream_si64` intrinsic like a normal person? Inline asm is almost always worse. https://gcc.gnu.org/wiki/DontUseInlineAsm unless you have a good reason. – Peter Cordes Sep 10 '21 at 12:20
  • If you want to store to the memory *pointed-to* by `adr1`, rather than to `adr1` itself, use `*adr1` to reference that C/C++ object. i.e. `"=m"(*adr1)` as an output operand, letting the compiler fill in the addressing mode for it. That's the point of an `"m"` or `"=m"` constraint. – Peter Cordes Sep 10 '21 at 12:22
  • Haven't found an exact duplicate about what `"=m"` does, but [Why is this pointer dereference ignored in this inline assembly statement?](https://stackoverflow.com/q/52936681) shows how `"m"` expands, and explains why. – Peter Cordes Sep 10 '21 at 13:10
  • Update: [GNU C inline asm "m" constraint with a pointer: address vs. pointed-to value?](https://stackoverflow.com/q/7139305) is an exact duplicate: using the pointer value instead of the pointed-to value. – Peter Cordes Sep 10 '21 at 13:17
  • IMHO, place your assembly instructions into a separate assembly language file and pass it to the linker. A lot easier than getting inline assembly working (again, that's just my opinion). – Thomas Matthews Sep 10 '21 at 14:42
  • @PeterCordes I want to use inline assembly because I am working with a lot of write-operations that I don't want the compiler to optimize (research about Rowhammer attack). Thanks for the link, I'll check out the related thread. – OHithere Sep 10 '21 at 19:49
  • 1
    You should try the intrinsic and see if your compiler does dead store elimination on that intrinsic or not. If you follow it with `asm("" ::: "memory")` for GNU C (a compiler memory barrier), it shouldn't be able to. – Peter Cordes Sep 10 '21 at 21:58

0 Answers0