Pregunta

I want to write a 64 bit integer to a particular memory location.

sample C++ code would look like this:

extern char* base;
extern uint64_t data;
((uint64_t *)base)[1] = data;

Now, here is my attempt to write the above as inline assembly:

uint64_t addr = (uint64_t)base + 8; 
asm volatile (
    "movq %0, (%1)\n\t"
    :: "r" (data), "r"(addr) : "memory"
    );

The above works in a small test program but in my application, I suspect that something here is off.

Do I need to specify any output operands or any other constraints in the above?

Thanks!

¿Fue útil?

Solución

Just say:

asm("mov %1, %0\n\t" : "=m"(*(uint64_t*)(base + 8)) : "r"(data) : "memory");

The tricky thing is that when using the "m" constraint, you're (possibly counterintuitively so) not giving an address but instead what in C looks like the "value" of the variable you want to change.
That's why, in this case, the weird pointer-cast-dereference. The compiler for this makes sure to put the address of the operand in.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top