访问C ++类成员内联汇编 [英] Accessing C++ class member in inline assembly
问题描述
问:如何从非POD类中访问组件中的成员变量
Question: How can I access a member variable in assembly from within a non-POD class?
精化:
我已经写了一些内联汇编code一类的成员函数,但什么躲开我是如何访问类成员变量。我试着在 offsetof 宏观但这是一个非POD类。
I have written some inline assembly code for a class member function but what eludes me is how to access class member variables. I've tried the offsetof macro but this is a non-POD class.
我使用的是目前的解决方案是从全球范围内的指针分配给成员变量,但它是一个混乱的解决方案,我希望有更好的东西,我不知道。
The current solution I'm using is to assign a pointer from global scope to the member variable but it's a messy solution and I was hoping there was something better that I dont know about.
注意:我使用的是G ++编译器。与英特尔的语法ASM一个解决方案将是不错,但我会采取什么。
note: I'm using the G++ compiler. A solution with Intel syntax Asm would be nice but I'll take anything.
例如我想要做什么(Intel语法):
example of what I want to do (intel syntax):
class SomeClass
{
int* var_j;
void set4(void)
{
asm("mov var_j, 4"); // sets pointer SomeClass::var_j to address "4"
}
};
目前hackish的解决方案:
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 :(
}
};
这些都是原油的例子,但我认为他们得到跨越点。
Those are crude examples but I think they get the point across.
推荐答案
这是你所需要的:
__asm__ __volatile__ ("movl $4,%[v]" : [v] "+m" (var_j)) ;
编辑补充:汇编程序接受英特尔的语法,但是编译器不知道它,所以采用Intel语法这一招是不行的(不与G ++ 4.4.0,反正)
Edited to add: The assembler does accept Intel syntax, but the compiler doesn't know it, so this trick won't work using Intel syntax (not with g++ 4.4.0, anyway).
这篇关于访问C ++类成员内联汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!