www.digitalmars.com         C & C++   DMDScript  

c++ - Inline Assembly and class members

reply Rob Yull <Rob_member pathlink.com> writes:
Working on a Mutex class.  I have the following code which compiles successfully
in Visual Studio 2005

__asm	mov ebx, [this];
__asm	mov eax, ebx;
__asm	add eax, [m_intLocked];
__asm	LockTest:
__asm	bts dword ptr [eax], 0;
__asm	jc	LockTest;
__asm	mov eax, intProcessID;
__asm	mov eax, [eax]
__asm	mov [ebx][m_intProcessID], eax;

The class that the this pointer refers too has 2 unsigned int member variables,
m_intLocked & m_intProcessID.  I tried just changing the Microsoft specific
__asm keyword to the asm used by Digital Mars.  When I try to compile I get the
following errors:

}
^
./Threads/Multi/Mutex/Policies/PMutex_x86.h(103) : Error: undefined label
'm_intLocked'
--- errorlevel 1

If I comment out that line it then falls to error on the m_intProcessID line.  I
also tried

add eax, m_intLocked[ebx];

but got the same thing.

What is the proper way to access these class member variables in Digital Mars?
And more specifically how to read their memory addresses?
Feb 08 2006
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Rob Yull wrote:
 What is the proper way to access these class member variables in Digital Mars?
 And more specifically how to read their memory addresses?
 
I don't know much about assembly, but this seems to work as expected: #include <iostream> class X { public: void F(int Val); int MyInt; }; void X::F(int Val) { asm { mov ecx, [this] mov eax, Val mov [ecx].MyInt, eax } } int main() { X MyX; MyX.F(12); std::cout << MyX.MyInt << std::endl; MyX.F(1212); std::cout << MyX.MyInt << std::endl; } Also read: http://www.digitalmars.com/ctg/ctgInlineAsm.html -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Feb 08 2006
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Bertel Brander wrote:
 Rob Yull wrote:
 What is the proper way to access these class member variables in 
 Digital Mars?
 And more specifically how to read their memory addresses?
I don't know much about assembly, but this seems to work as expected:
[Snip] But watch out, this compiles/assebles as well. It only work by incident: #include <iostream> class X { public: void F1(int Val); void F2(int Val); int MyInt1; int MyInt2; }; class Y { public: void F1(int Val); void F2(int Val); int MyInt3; int MyInt4; }; void X::F1(int Val) { asm { mov ecx, [this] mov eax, Val mov [ecx].MyInt3, eax // Note MyInt3 is not a member of X } } void X::F2(int Val) { asm { mov ecx, [this] mov eax, Val mov [ecx].MyInt2, eax } } int main() { X MyX; MyX.F1(12); std::cout << MyX.MyInt1 << std::endl; MyX.F1(1212); std::cout << MyX.MyInt1 << std::endl; MyX.F2(123); std::cout << MyX.MyInt2 << std::endl; MyX.F2(123123); std::cout << MyX.MyInt2 << std::endl; } -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Feb 08 2006
parent Bertel Brander <bertel post4.tele.dk> writes:
Bertel Brander wrote:
 Bertel Brander wrote:
 Rob Yull wrote:
 What is the proper way to access these class member variables in 
 Digital Mars?
 And more specifically how to read their memory addresses?
I don't know much about assembly, but this seems to work as expected:
[Snip] But watch out, this compiles/assebles as well. It only work by incident: mov [ecx].MyInt3, eax // Note MyInt3 is not a member of X
More answering myself, You could make a habit of using the full name: mov [ecx] X.MyInt3, eax // Note MyInt3 is not a member of X Then the compiler will tell you: ff.cpp(26) : Error: 'MyInt3' is not a member of struct 'X' That will also solve the problem with multible class's having the same member, which might result in the error: ff.cpp(27) : Error: 'MyInt1' is a member of 'Y' and 'X' -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Feb 09 2006