News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Question on the C++ operator ->

Started by djtraumwelt, May 06, 2005, 01:11:57 PM

Previous topic - Next topic

djtraumwelt

i have some books about programming and they have all example codes in c++ and there is sometimes this -> in the c++ codes. what does this -> mean and how can i do it in programming in masm32?



I changed the title so it was more easily understood.

hutch--

Tedd

foo = blah->stuff

works as..

mov eax,blah
mov edx,[eax].stuff
mov foo,edx


----------------
This requires that the relevant structure pointed to by blah is known, and thus the offset of 'stuff' will be known.
('assume' directives left out of example)
No snowflake in an avalanche feels responsible.

drhowarddrfine

...so blah is an object and stuff is a member of that object, per Tedd's example.

hitchhikr

Quote
so blah is an object

blah is a pointer.

djtraumwelt

that's a little complicated. can you convert this c++ code for me in assembler code?
lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);

mnemonic

If you have a struct called foo with a member called bar and a pointer to the foo struct called pfoo the the two following expressions are the same:
pfoo->bar
(*pfoo).bar

It is not really complicated to convert the expression if you really know assembler.
So think about it a bit. It will let you gain more experience´if you convert it for yourself.
At lest show up with some code even if you couldn´t get it to work. At least try it.
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

djtraumwelt

maybe i'm a little stupid but i really dont understand! :'(

Tedd

Quote from: hitchhikr on May 06, 2005, 03:21:06 PM
Quote
so blah is an object

blah is a pointer.

blah is a pointer to an object. But in C++ when you say 'an object' you almost always really mean the pointer to the object.



dj
lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
lpDD is your pointer to the DirectDraw interface.
CreateSurface is the function you want to call, but in this case you need to know the offset of the function's pointer in the interface to be able to get it and then call the function.
So..

mov ecx,lpDD                 ;ecx is the pointer to the interface
assume ecx:ptr DirectDraw    ;(or whatever it is)
mov eax,[ecx].CreateSurface  ;eax is now the pointer to the CreateSurface function in the interface pointed to by ecx
push 0                       ;}
push OFFSET lpDDSPrimary     ;} -- push parameters (reverse order)
push OFFSET ddsd             ;}
call eax                     ;now call the actual function!
assume ecx:nothing

{Note: ecx is usually used to hold the interface pointer and 'may' be used internally by the function called - ie. it may not work if you use a different register; I'm not sure of the exact rules -- or sometimes you have to push the object pointer as an 'extra' parameter}

I suggest you look at the COM-in-ASM stuff, as well as finding some directx include files for asm :wink
No snowflake in an avalanche feels responsible.

hitchhikr

-> is a C operator and not specific to C++.

So, if you have troubles with such syntax, buy a book about C or browse the web to obtain informations/tutorials about this language (C++ ones usually assume that the reader already knows C), download a C compiler from the web and see how it translates the code in asm. Also directdraw doesn't necessary require C++ even if it was designed with this language in mind.


mov ecx,lpDD                 ;ecx is the pointer to the interface
assume ecx:ptr DirectDraw    ;(or whatever it is)
mov eax,[ecx].CreateSurface  ;eax is now the pointer to the CreateSurface function in the interface pointed to by ecx
push 0                       ;}
push OFFSET lpDDSPrimary     ;} -- push parameters (reverse order)
push OFFSET ddsd             ;}
call eax                     ;now call the actual function!
assume ecx:nothing


This won't work because directx requires the calling interface as first argument (this is constant in the dx com interface).

thomasantony

Quote from: Tedd on May 06, 2005, 05:22:59 PM
Quote from: hitchhikr on May 06, 2005, 03:21:06 PM
Quote
so blah is an object

blah is a pointer.

blah is a pointer to an object. But in C++ when you say 'an object' you almost always really mean the pointer to the object.



dj
lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
lpDD is your pointer to the DirectDraw interface.
CreateSurface is the function you want to call, but in this case you need to know the offset of the function's pointer in the interface to be able to get it and then call the function.
So..

mov ecx,lpDD                 ;ecx is the pointer to the interface
assume ecx:ptr DirectDraw    ;(or whatever it is)
mov eax,[ecx].CreateSurface  ;eax is now the pointer to the CreateSurface function in the interface pointed to by ecx
push 0                       ;}
push OFFSET lpDDSPrimary     ;} -- push parameters (reverse order)
push OFFSET ddsd             ;}
call eax                     ;now call the actual function!
assume ecx:nothing

{Note: ecx is usually used to hold the interface pointer and 'may' be used internally by the function called - ie. it may not work if you use a different register; I'm not sure of the exact rules -- or sometimes you have to push the object pointer as an 'extra' parameter}

I suggest you look at the COM-in-ASM stuff, as well as finding some directx include files for asm :wink
Hi,
    You don't ahve to use different registers. You can do what you are doing using one register.

mov eax,lpDD                 ;ecx is the pointer to the interface
assume eax:ptr DirectDraw    ;(or whatever it is)
mov eax,[eax].CreateSurface  ;
assume eax:nothing
push 0                       ;}
push OFFSET lpDDSPrimary     ;} -- push parameters (reverse order)
push OFFSET ddsd             ;}
call eax                     ;now call the actual function!

No need to use ecx. You can even get rid of the two ASSUMEs. Do

mov eax,(DirectDraw ptr[eax]).CreateSurface


Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

BogdanOntanu

#10

xor eax,eax
push eax
push offset lp_surface
push offset ddsd_all

mov eax,[lpDD] ; main DD Object
        push     eax
mov eax,[eax]
call [eax + DDO_CreateSurface] ; CreateSurface


ok corected

As you might notice the lpDD must be the last thing pushed ie it is the first (unseen) parameter of all COM method interface calls.
You should have a valid DDSurface_Descriptor in ddsd_all and of course lpDD before calling.


The problem here is that C++ chooses to hide the lpDD-> in order ot "protect" you from yourself and to help you do not make mistakes.
In the process it is also hidding information... a standard procedure for HLL languages.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Tedd

Thomas, you 'can' do it like that, but I just typed out the code as I went :P Also, I think it's easier to follow (as should be the point of an example) and possibly more efficient due to register stalls :bg

Thanks boggy, though I did guess at that as an alternative :toothy
Quote..sometimes you have to push the object pointer as an 'extra' parameter..
(-- even though your example is wrong :bdg "mov eax,[eax]" when eax=0 ?? BAADDDD!!)

So... my example, corrected

mov ecx,lpDD                 ;ecx is the pointer to the interface
assume ecx:ptr DirectDraw    ;(or whatever it is)
mov eax,[ecx].CreateSurface  ;eax is now the pointer to the CreateSurface function in the interface pointed to by ecx
push 0                       ;}
push OFFSET lpDDSPrimary     ;} -- push parameters (reverse order)
push OFFSET ddsd             ;}
push ecx                     ;push the object pointer!
call eax                     ;now call the actual function!
assume ecx:nothing

No snowflake in an avalanche feels responsible.