Solved

How to access a C++ object member inside 80386 _asm block?

Posted on 2004-09-09
6
561 Views
Last Modified: 2008-01-09

I am using VC++ v6.0 and unlikely to upgrade soon.

I have an assembly block (Intel 80386) inside of a class method.
I want to access the member variables of the class. I can access
the "this" pointer easily enough.

I am have not yet figured out the syntax for the member offset.
The following code example illustrates what I want to do.  But it
does not compile.

class_name::subroutine()
{
    _asm
    {
        mov     esx,this    ; this compiles okay
        mov     eax,offset class_name::member   ; C2415: improper operand type
        mov     eax,offset member   ; C2400: inline assembler syntax error in 'opcode';
                                                 ;                                     found 'bad token'
        mov     ebx,[esx+eax]       ; Have not got this far
    }
}

The offset operator does not work.  Does anybody know how to
get the offset of a member from within assembler???

Jim S.
0
Comment
Question by:Jim_S
6 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12023819
I don't know how you'd do it in assembler, but it's a pretty easy workaround to calculate it piror to the asm block and then use the local variable in the asm:

    long lm = offsetof(class_name, member);
    __asm
    {
        mov esx, this  ;  Is this a typo?  Or what processor?
        mov eax, lm  ;
        mov ebx,[esx+eax]  ; again, what's esx?
    }
0
 
LVL 1

Accepted Solution

by:
beavis_shenzhen earned 250 total points
ID: 12023922
in vs.net , no need to call offsetof Macro , you could referece to class member freely in asm block, as following:
{
    CDialog dlg;
    __asm mov eax, dlg.m_Handle ;// yes ,compiling OK!
}
I dont have vc6.0 at hand , but I guess the vc 6.0 compiler should support this.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12024064
>> I dont have vc6.0 at hand , but I guess the vc 6.0 compiler should support this.
It does, but unfortunately it does not seem to support dereferencing of pointers in the asm block and the intent is to get a member of the current class instance: 'this->member' and '(*this).member' do not work.

Which of course begs the question:  This is exactly the code that the compiler generates for getting a value of a member variable, so why do it in an asm block at all?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:Jim_S
ID: 12024173
drichards>     long lm = offsetof(class_name, member);

This obviously would work.  But it had already occurred to
me that I could do:

    long local_member = member;

to put a copy of the member into a local variable and access the
data that way.  Another way would be:

    long *ptr_member = &member;

But I was hoping for something that would not rely on the C code.

-----

beavis_shenzhen>     __asm mov eax, dlg.m_Handle ;

The following compiled without error.

        _asm
        {
            mov eax,this.member
        }

Thank you.  I give you the points for this.  Just hope it
executes correctly when I get to that point.

-----

drichards>     mov ebx,[esx+eax]  ; again, what's esx?

You are right.  I meant esi, not esx.  Thank you, this has helped
me get rid of additional syntax errors.  I never liked the fact
experts-exchange requires that I award all points to only one
person.  This comment was helpful.  If I could divide the points,
I would give you some.
0
 
LVL 13

Expert Comment

by:SteH
ID: 12024478
You can divide the points: thats called a split or split points. See
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/help.jsp#hi69
how to do it. If you still want to divide your points you can ask in the community support to reopen this question that you can split the points.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12026320
Jim_S,

You should seriously question why you're doing this in the first place.
You gain nothing by using assembly language, and completely loose your portability.

This can easily be done with no assembly language, and would be much more reliable.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java inner class, for the sole use of parameter passing 21 77
FMX enumerated colours 2 99
How to split this in C++ 4 102
draw a Christmas tree by using a nested loop? 26 62
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question