Archived

This topic is now archived and is closed to further replies.

Quick inline ASM question

This topic is 6014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible for a class'' member function to access one of its own member variables in inline ASM? I know you can access another object''s members just like thing.number, but for it to access its own members, it has to use the this pointer, and the arrow and dereference operators don''t seem to work in ASM. It''s really not a big deal, just a little optimization that I tried to make just for the hell of it, but now it''s got me curious^^
-Deku-chan DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unless I''m totally off in my C++, class methods don''t need the *this pointer to access member variables; they can access them by name alone (with the exception of static variables?) So if your class foo has member variable bar and member function getBar() like so:


class foo
{
public:
int getBar( void ) { return bar; }

private:
int bar;
};


You notice that getBar simply refers to bar as "bar", not *this.bar. Since the C++ and inline assembly visibilities seem to be the same, I think your question is answered.

Unless, of course, you''ve already tried this. In that case, try *this.bar rather than this->bar (which I suspect you''re using). If that dpoesn''t work, well - I''ll shut up now.

Share this post


Link to post
Share on other sites
Yup, tried all those. Tried [this].bar too, since [] is sort of ASM''s version of *. Then that didn''t work so I tried (foo)[this].bar, but ASM doesn''t know how to cast things either, so that didn''t work...
I could calculate the offset into the class, and use like [this]+5 (or whatever the offset is), but that seems error-prone, so I posted here instead. Oh well, what I was planning to use it for was pretty much pointless, it''d just be nice to know I could use it in other places.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites
I just started Win32 Asm, and when I do program in it, I do the whole thing in assembly, so I wouldn''t know how to interface with C++ syntax.Anyways back to the point, post that question at http://www.hiroshimator.com/asmcommunity/messageboard/ Since its an Asm board some one there should know.

Share this post


Link to post
Share on other sites
First you need to picture this:

class BIGCLASS
{
int one; // at offset 0
int two; // offset 4
short three; // offset 8

void CallIt(void); // I believe just a point to a function
// so a dword then
};

so you then need to access these through asm, remember that the ebx/esi/edi registers are used for addressing data...NOT OTHER DATA SUCH AS THIS.

So:

void BIGCLASS::CallIt(void)
{
__asm
{
mov ebx, this
mov dword ptr [ebx], 1
mov dword ptr [ebx+4], 2
mov word ptr [ebx+8], 3
}
}

...I did compile this and output the values of the variables in the class and it did work..


Hoped this helped some.

--Andrew

Share this post


Link to post
Share on other sites
Assembler015''s suggestion works for his particular example, but it should be noted that in general it''s not safe.

If you have a class that''s just like a standard C struct as far as data goes (member functions don''t effect this) then it''s fine. But as soon as you start defining virtual functions and dealing with inheritance the idea probably will not work.

When you have virtual functions, the class internally manages a virtual function pointer table, which might or might not (I''m not sure) be stored at the start of the class instance in memory (at offset 0).

Therefore, if you have a class with virtual functions and attempt to access the first byte or dword or whatever, you may be accessing data that belongs to the VTable instead of your class member data.

Share this post


Link to post
Share on other sites
That is true...I think that if you keep all your variables in one spot in the class it will always work though..like this->

class BIGCLASS
{
int func1 (int x);
virtual func2 (int y);

int x; //data TOGETHER
float y;
double z; //DATA end block

void func3 (void);
};

that should ALWAYS work when accessing variables...I think----I''m not the biggest fan of C++ so I don''t try these thing out enough....in C that is true---the data is kept in the same place. It would seem kind of awkward for C++ to change the data alignment in the middle of a program...but whatever.

--Andrew

Share this post


Link to post
Share on other sites
Well, the class does use virtual functions, but I think the table would be stored somewhere else, since it would only need one copy, not one for each object. I could just create an object of the class, get the addres of that, and subtract that from the address of the variable I''m after if just calculating the offset myself doesn''t work though.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites
thats some wierd syntax for masm32....declaring a variable should go like this:

avariable DW 0

...that other way isn''t intel syntax....and it''s not like masm6.15/6.14/6.0/5.0/--->

I don''t really care...thats just wierd

Share this post


Link to post
Share on other sites
Thanks, it worked^^ Unfortunately, I changed what I was planning to use it for, but there is another place where I just made a copy of a variable, so I can save a very small amount of time, and make it look nicer now^^



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Share this post


Link to post
Share on other sites