📄 atl under the hood - part 4.mht
字号:
=
src=3D"http://www.codeproject.com/script/images/support_icon.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
href=3D"http://www.codeproject.com/store/"><B>MSDN =
Univ. from=20
$1950</B></A></TD></TR>
<TR vAlign=3Dtop>
<TD></TD>
<TD></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/print.gif" =
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/atl/atl_underthehood_4.asp?print=3Dtru=
e"=20
target=3D_print>Print version</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/link.gif" =
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/add_bookmark.asp?t=3D0&=
amp;ct=3D%2Fatl%2Fatl%5Funderthehood%5F4%2Easp&guid=3Datl%5Fundertheh=
ood%5F4%2Fatl6%2F5%2F2002">Bookmark=20
this</A></TD></TR></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
src=3D"http://www.codeproject.com/images/mail.gif" =
width=3D16></TD>
<TD class=3DSmallText><A=20
=
href=3D"http://www.codeproject.com/script/recommend/form.asp?guid=3Datl%5=
Funderthehood%5F4%2Fatl6%2F5%2F2002">Send=20
to a friend</A></TD></TR>
<TR vAlign=3Dtop>
<TD></TD>
<TD></TD></TR>
<TR vAlign=3Dtop>
<TD></TD>
<TD class=3DInfoBarHeader>My Profile</TD></TR>
<TR>
<TD bgColor=3Dblack colSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif"=20
width=3D1></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/profile_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/modify.asp?ct=3D/atl/at=
l_underthehood_4.asp">My=20
Settings</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/submit_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/articles/list_articles.asp?user=
id=3D273403">My=20
Articles</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/bookmarks.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/bookmark_list.asp">My=20
Bookmarks</A></TD></TR>
<TR vAlign=3Dtop>
<TD><IMG height=3D16=20
=
src=3D"http://www.codeproject.com/images/logoff_small.gif"=20
width=3D16></TD>
<TD class=3DsmallText><A=20
=
href=3D"http://www.codeproject.com/script/profile/logoff.asp?ct=3D/atl/at=
l_underthehood_4.asp">Sign=20
out</A></TD></TR>
<TR>
<TD bgColor=3Dblack colSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif"=20
width=3D1></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD> </TD></TR>
<TR>
<TD>
<CENTER><A=20
=
href=3D"http://ad.doubleclick.net/jump/N3263.codeproject.com/B1287526;abr=
=3D!ie4;abr=3D!ie5;sz=3D120x600;ord=3D62182350?"><IMG=20
height=3D600 alt=3D"Click Here"=20
=
src=3D"http://ad.doubleclick.net/ad/N3263.codeproject.com/B1287526;abr=3D=
!ie4;abr=3D!ie5;sz=3D120x600;ord=3D62182350?"=20
width=3D120 =
border=3D0></A><BR></CENTER><BR></TD></TR></TBODY></TABLE></TD>
<TD width=3D5 rowSpan=3D2><IMG height=3D1=20
src=3D"http://www.codeproject.com/images/space.gif" width=3D5> =
</TD>
<TD align=3Dright width=3D"100%" bgColor=3Dwhite>
<TABLE width=3D"100%">
<TBODY>
<TR>
<TD class=3DsmallText vAlign=3Dtop noWrap></TD>
<TD noWrap align=3Dright><A name=3D__top></A>
<TABLE>
<TBODY>
<TR>
<TD class=3DsmallText align=3Dright>24 members have =
rated this=20
article. Result:</TD>
<TD>
<TABLE cellSpacing=3D0 cellPadding=3D0 border=3D2>
<TBODY>
<TR>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D20 border=3D0></TD>
<TD><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/red.gif"=20
width=3D16 border=3D0><IMG height=3D5=20
=
src=3D"http://www.codeproject.com/script/images/white.gif"=20
width=3D4 =
border=3D0></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=3DsmallText align=3Dright colSpan=3D2><A=20
title=3D"Calculated as rating x Log10(# votes)"=20
=
href=3D"http://www.codeproject.com/script/articles/top_articles.asp?st=3D=
2">Popularity:=20
6.61</A>. Rating: <B>4.79</B> out of=20
5.</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD vAlign=3Dtop width=3D"100%">
<DIV id=3Dcontentdiv><!-- Article Starts --><!-- Add the rest of =
your HTML here -->
<H2>Introduction</H2>
<P>Till now we haven't discuss anything about assembly language. =
But we=20
can't avoid it so long if we really want to know what is going on =
under=20
the hood of ATL. Because ATL use some low level technique as well =
as some=20
inline assembly language to make it as small and as fast as =
possible. I=20
assume that reader already have basic knowledge of assembly =
language so I=20
will only concentrate on my topic and not try to write another =
tutorial of=20
assembly language. If you don't know enough assembly language then =
I=20
recommends taking a look at Matt Pietrek's Article "Under The =
Hood" in Feb=20
1998 issue of Microsoft System Journal, it gives you enough =
information=20
about the assembly language.=20
<P>To start out tour take a look at this simple program=20
<P><B>Program 55</B> </P><PRE><SPAN =
class=3Dcpp-keyword>void</SPAN> fun(<SPAN =
class=3Dcpp-keyword>int</SPAN>, <SPAN class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
fun(<SPAN class=3Dcpp-literal>5</SPAN>, <SPAN =
class=3Dcpp-literal>10</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>Now compile it on command line with command line compiler cl.exe.=20
Compile it with -FAs switch. E.g. if this program name is prog55 =
then=20
compile it this way=20
<P></P><PRE lang=3Dtext>Cl -FAs prog55.cpp
</PRE>
<P>This will generate a file with the same name but .asm extension =
contains the assembly language code of the following program. Now =
take a=20
look at generated output file. Lets discuss the calling of =
function first.=20
The assembly code to call this function is something like this. =
</P><PRE lang=3Dasm> <SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>10</SPAN> <SPAN class=3Dcpp-comment>; =
0000000aH</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>5</SPAN>
<SPAN class=3Dcpp-keyword>call</SPAN> ?fun@@YAXHH@Z <SPAN =
class=3Dcpp-comment>; fun</SPAN>
</PRE>
<P>The parameters of function are pushed on the stack from right =
to left=20
order and then call function. But the name of function is little =
bit=20
different then our given function name. This is because C++ =
compiler=20
decorates the name of function to perform function overloading. =
Let's=20
change a program little bit and overload the function to take a =
look at=20
the behavior of the code. </P><B>Program 56</B> <PRE><SPAN =
class=3Dcpp-keyword>void</SPAN> fun(<SPAN =
class=3Dcpp-keyword>int</SPAN>, <SPAN class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>void</SPAN> fun(<SPAN =
class=3Dcpp-keyword>int</SPAN>, <SPAN class=3Dcpp-keyword>int</SPAN>, =
<SPAN class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
fun(<SPAN class=3Dcpp-literal>5</SPAN>, <SPAN =
class=3Dcpp-literal>10</SPAN>);
fun(<SPAN class=3Dcpp-literal>5</SPAN>, <SPAN =
class=3Dcpp-literal>10</SPAN>, <SPAN class=3Dcpp-literal>15</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>Now the assembly languages of calling both of the functions are =
something like this </P><PRE lang=3Dasm> <SPAN =
class=3Dcpp-keyword>push</SPAN> <SPAN class=3Dcpp-literal>10</SPAN> =
<SPAN class=3Dcpp-comment>; 0000000aH</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>5</SPAN>
<SPAN class=3Dcpp-keyword>call</SPAN> ?fun@@YAXHH@Z <SPAN =
class=3Dcpp-comment>; fun</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>15</SPAN> <SPAN class=3Dcpp-comment>; =
0000000fH</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>10</SPAN> <SPAN class=3Dcpp-comment>; =
0000000aH</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>5</SPAN>
<SPAN class=3Dcpp-keyword>call</SPAN> ?fun@@YAXHHH@Z <SPAN =
class=3Dcpp-comment>; fun</SPAN>
</PRE>
<P>Take a look at the name of the function, we write both function =
with=20
the same name but compiler decorates these functions itself to do =
function=20
overloading. </P>
<P>If you don't want to decorate the function name then you can =
use extern=20
"C" with function. Let's see little bit change in the program.=20
</P><B>Program 57</B> <PRE><SPAN class=3Dcpp-keyword>extern</SPAN> =
<SPAN class=3Dcpp-string>"C"</SPAN> <SPAN =
class=3Dcpp-keyword>void</SPAN> fun(<SPAN =
class=3Dcpp-keyword>int</SPAN>, <SPAN class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
fun(<SPAN class=3Dcpp-literal>5</SPAN>, <SPAN =
class=3Dcpp-literal>10</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>The assembly language code of this function is </P><PRE =
lang=3Dasm> <SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>10</SPAN> <SPAN class=3Dcpp-comment>; =
0000000aH</SPAN>
<SPAN class=3Dcpp-keyword>push</SPAN> <SPAN =
class=3Dcpp-literal>5</SPAN>
<SPAN class=3Dcpp-keyword>call</SPAN> _fun
</PRE>
<P>This means that now you can't overload the function with C =
linkage.=20
Take a look at the following program </P><B>Program 58</B> =
<PRE><SPAN class=3Dcpp-keyword>extern</SPAN> <SPAN =
class=3Dcpp-string>"C"</SPAN> <SPAN class=3Dcpp-keyword>void</SPAN> =
fun(<SPAN class=3Dcpp-keyword>int</SPAN>, <SPAN =
class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>extern</SPAN> <SPAN =
class=3Dcpp-string>"C"</SPAN> <SPAN class=3Dcpp-keyword>void</SPAN> =
fun(<SPAN class=3Dcpp-keyword>int</SPAN>, <SPAN =
class=3Dcpp-keyword>int</SPAN>, <SPAN class=3Dcpp-keyword>int</SPAN>) {
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
fun(<SPAN class=3Dcpp-literal>5</SPAN>, <SPAN =
class=3Dcpp-literal>10</SPAN>);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>This program give compilation error because function =
overloading is not=20
supported in C language and you are going to make the two function =
with=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -