Assembly Language for Beginners

(Jeff_L) #1

3.18. C++


DD FLAT:??_R0?AVbox@@@8
DD FLAT:??_R3box@@8

??_7box@@6B@ DD FLAT:??_R4box@@6B@ ; box::`vftable'
DD FLAT:?dump@box@@UAEXXZ


_color$ = 8 ; size = 4
_width$ = 12 ; size = 4
_height$ = 16 ; size = 4
_depth$ = 20 ; size = 4
??0box@@QAE@HHHH@Z PROC ; box::box, COMDAT
; _this$ = ecx
push esi
mov esi, ecx
call ??0object@@QAE@XZ ; object::object
mov eax, DWORD PTR _color$[esp]
mov ecx, DWORD PTR _width$[esp]
mov edx, DWORD PTR _height$[esp]
mov DWORD PTR [esi+4], eax
mov eax, DWORD PTR _depth$[esp]
mov DWORD PTR [esi+16], eax
mov DWORD PTR [esi], OFFSET ??_7box@@6B@
mov DWORD PTR [esi+8], ecx
mov DWORD PTR [esi+12], edx
mov eax, esi
pop esi
ret 16
??0box@@QAE@HHHH@Z ENDP ; box::box


Here we see a slightly different memory layout: the first field is a pointer to some tablebox::`vftable'
(the name has been set by the MSVC compiler).


In this table we see a link to a table named
box::`RTTI Complete Object Locator'and also a link
to thebox::dump()method.


These are called virtual methods table andRTTI^29. The table of virtual methods has the addresses of
methods and theRTTItable contains information about types.


By the way, theRTTItables are used while callingdynamic_castandtypeidin C++. You can also see here
the class name as a plain text string.


Thus, a method of the baseobjectclass may call the virtual methodobject::dump(), which in turn will call
a method of an inherited class, since that information is present right in the object’s structure.


Some additional CPU time is needed for doing look-ups in these tables and finding the right virtual method
address, thus virtual methods are widely considered as slightly slower than common methods.


In GCC-generated code theRTTItables are constructed slightly differently.


3.18.2 ostream.


Let’s start again with a “hello world” example, but now we are going to useostream:


#include


int main()
{
std::cout << "Hello, world!\n";
}


Almost any C++ textbook tells us that the<<operation can be defined (overloaded) for other types. That
is what is done inostream. We see thatoperator<<is called forostream:


Listing 3.98: MSVC 2012 (reduced listing)

$SG37112 DB 'Hello, world!', 0aH, 00H


(^29) Run-Time Type Information

Free download pdf