0

Assuming I have a memory location that I want to copy data to, and I have that address in a pointer, Is it possible to copy data at that location via MOV instruction and inline assembly.Or basically how would I copy data to a memory location via inline assembly.

In the code snippet I have provided below, I am just copying my value the the eax register then copying from eax register to some out value, whereas i would like to copy it to some memory address.

In short what is in the eax register I would like to store in the address pointed by outpointer is it possible or is there a better way to do it in inline assembly?

#include <stdio.h>
#include <iostream>

using namespace std;

int func3(int parm)
{


    int out =0;
    // int *outpointer =&out;
    //what is in the eax register I would like to store 
    //in the address pointed  by outpointer

    asm("mov  %1 ,%%eax\n\t"
        "mov %%eax, %0": "=r"(out) : "r"(parm));


    // cout<<outpointer;
    return out;
}

int main(int argc, char const *argv[])
{
    int x{5};
    int y{0};


    y = func3(x);

    cout<<y;


    return 0;
} 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Weez Khan
  • 137
  • 1
  • 8
  • 1
    I don't quite understand what you are trying to do, so I'm not sure if this helps. You can move the value in `parm` to the memory for `out` by doing `asm("mov %1, %0": "=m"(out) : "r"(parm));`. But compilers tend to be smart, and they don't allocate memory for variables unless they must. There's no particular need for memory to be reserved here, so (assuming you haven't disabled optimization), it could easily decide to use a register to hold `out`. To allow for either possibility, you could do `asm("mov %1, %0": "=mr"(out) : "r"(parm));`. There's cooler alternatives, but that's a start. – David Wohlferd Mar 20 '20 at 22:01
  • `"=m"(*outpointer)` will get the compiler to substitute an addressing mode into your template operand. – Peter Cordes Mar 21 '20 at 03:08

0 Answers0