Assembly Language for Beginners

(nextflipdebug2) #1

3.18. C++


_main PROC
push OFFSET $SG37112
push OFFSET ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?⤦
Ç$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits >
add esp, 8
xor eax, eax
ret 0
_main ENDP


Let’s modify the example:


#include


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


Andagain, frommanyC++textbooksweknowthattheresultofeachoperator<<inostreamisforwarded
to the next one. Indeed:


Listing 3.99: MSVC 2012

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


_main PROC
push OFFSET $SG37113 ; 'Hello, '
push OFFSET ?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?⤦
Ç$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits >
add esp, 8


push OFFSET $SG37112 ; 'world!'
push eax ; result of previous function execution
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?⤦
Ç$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char> >
add esp, 8

xor eax, eax
ret 0
_main ENDP


If we would renameoperator<<method name tof(), that code will looks like:


f(f(std::cout, "Hello, "), "world!");


GCC generates almost the same code as MSVC.


3.18.3 References.


In C++, references are pointers (3.21 on page 611) as well, but they are calledsafe, because it is harder
to make a mistake while dealing with them (C++11 8.3.2).


For example, reference must always be pointing to an object of the corresponding type and cannot be
NULL [Marshall Cline,C++ FAQ8.6].


Even more than that, references cannot be changed, it is impossible to point them to another object
(reseat) [Marshall Cline,C++ FAQ8.5].


If we are going to try to change the example with pointers (3.21 on page 611) to use references instead
...


void f2 (int x, int y, int & sum, int & product)
{
sum=x+y;
product=x*y;
};

Free download pdf