Accessing class members from inline assembly in Visual C++

On the off chance that you have something that really does need assembly (see Bo's answer), there is an article here on accessing C or C++ data in inline assembly blocks.

In your case we get:

void Graph::PutPixel(DWORD x, DWORD y, DWORD c)
{
    __asm 
    {
        mov ecx,this
        mov Eax, y
        mov Ebx, [ecx]Graph._width //alias ecx to type 'Graph' and access member
        mul Ebx
        add Eax, x
        shl Eax, 2
        add Eax, [ecx]._buffer //access member with bound alias
        mov Edi, Eax
        mov Eax, c
        stosd
    }
}

Why use assembly in the first place?!

Isn't this

void Graph::PutPixel(DWORD x, DWORD y, DWORD c)
{
    __asm 
    {
        Mov Eax, y
        Mov Ebx, _width
        Mul Ebx
        Add Eax, x
        Shl Eax, 2 // Multiply by four
        Add Eax, _buffer
        Mov Edi, Eax
        Mov Eax, c
        StosD
    }
}

the same as

DWORD* ptr = ((y * _width) + x) + _buffer;
*ptr = c;

Just storing c at a computed memory address.


Or, even simpler

_buffer[y * _width + x] = c;

Now we're talking!