C ++ class member access in an inline assembly

Question: How can I access a member variable in an assembly from a non-POD class?


Development:

I wrote some built-in assembler code for a member function of a class, but it eluded me how to access the member variables of the class. I tried the offsetof macro , but this is not a POD class.

The current solution I'm using is to assign a pointer from the global scope to a member variable, but this is a messy solution, and I was hoping there was something better that I don’t know about.

note: I am using the g ++ compiler. A solution with Intel Asm syntax would be nice, but I'll take something.

example of what I want to do (syntax):

class SomeClass
{
  int* var_j;
  void set4(void)
  {
    asm("mov var_j, 4"); // sets pointer SomeClass::var_j to address "4"
  }
};

current hacker solution:

int* global_j;
class SomeClass
{
  int* var_j;
  void set4(void)
  {
    asm("mov global_j, 4"); // sets pointer global_j to address "4"
    var_j = global_j;       // copy it back to member variable :(
  }
};

, , .

+1
2

, :

__asm__ __volatile__ ("movl $4,%[v]" : [v] "+m" (var_j)) ;

: Intel, , Intel ( g++ 4.4.0, ).

+6
class SomeClass
{
  int* var_j;
  void set4(void)
  {
    __asm__ __volatile__("movl $4, (%0,%1)"
:
: "r"(this), "r"((char*)&var_j-(char*)this)
:
);
  }
};

, :

    __asm__ __volatile__("movl $4, %1(%0)"
:
: "r"(this), "i"((char*)&var_j-(char*)this)
:
);

, var_j wrt. this , - , , . ( g++, .)

__volatile__. , , volatile, .

+3

All Articles