📄 atl under the hood - part 4.mht
字号:
to set the stack frame. </P><PRE lang=3Dasm> <SPAN =
class=3Dcpp-keyword>push</SPAN> ebp
<SPAN class=3Dcpp-keyword>mov</SPAN> ebp, esp
<SPAN class=3Dcpp-keyword>sub</SPAN> esp, <SPAN =
class=3Dcpp-literal>12</SPAN> <SPAN class=3Dcpp-comment>; =
0000000cH</SPAN>
</PRE>
<P>This code is called prolog code. And in the same way the code =
inserted=20
at the end of function is called Epilog code. In the same program =
the=20
Epilog code generated by the compiler is </P><PRE lang=3Dasm> =
<SPAN class=3Dcpp-keyword>mov</SPAN> esp, ebp
<SPAN class=3Dcpp-keyword>pop</SPAN> ebp
<SPAN class=3Dcpp-keyword>ret</SPAN> <SPAN class=3Dcpp-literal>0</SPAN>
</PRE>
<P>Now take a look at the function with naked attribute =
</P><B>Program=20
64</B> <PRE><SPAN class=3Dcpp-keyword>extern</SPAN> <SPAN =
class=3Dcpp-string>"C"</SPAN> <SPAN class=3Dcpp-keyword>void</SPAN> =
_declspec(naked) fun() {
<SPAN class=3Dcpp-keyword>_asm</SPAN> ret
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
fun();
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>The code of the function fun, which is generated by the =
compiler, is=20
something like this. </P><PRE lang=3Dasm> _asm <SPAN =
class=3Dcpp-keyword>ret</SPAN>
</PRE>
<P>Means there is no prolog and epilog code in this function. In =
fact,=20
there are rules of naked function, i.e. you can't declare =
automatic=20
variable in naked function, because for this compiler have to =
generate the=20
code for you and in naked function compiler wont generate any code =
for=20
you. In fact you have to write the ret statement yourself =
otherwise=20
program will be crash. You even can't write return statement in =
the naked=20
function. Why? Because when you return something from the =
function, then=20
compiler puts its value in <CODE>eax</CODE> register. So it means =
compiler=20
have to generate the code for your return statement. Let's take a =
look at=20
this simple program to understand the working of return value from =
the=20
function. </P><B>Program 64</B> <PRE><SPAN =
class=3Dcpp-preprocessor>#include <cstdio></SPAN>
<SPAN class=3Dcpp-keyword>extern</SPAN> <SPAN =
class=3Dcpp-string>"C"</SPAN> <SPAN class=3Dcpp-keyword>int</SPAN> =
sum(<SPAN class=3Dcpp-keyword>int</SPAN> a, <SPAN =
class=3Dcpp-keyword>int</SPAN> b) {
<SPAN class=3Dcpp-keyword>return</SPAN> a + b;
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
<SPAN class=3Dcpp-keyword>int</SPAN> iRetVal;
sum(<SPAN class=3Dcpp-literal>3</SPAN>, <SPAN =
class=3Dcpp-literal>7</SPAN>);
<SPAN class=3Dcpp-keyword>_asm</SPAN> mov iRetVal, eax
printf(<SPAN class=3Dcpp-string>"%d\n"</SPAN>, iRetVal);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>The output of this program is "10". Here we haven't directly =
use the=20
return value of the function, instead of this we copy the value of =
<CODE>eax</CODE> in the variable just after calling the function. =
</P>
<P>Now write our whole function naked with prolog and epilog code =
which=20
return the value of two variables after return it. </P><B>Program =
65</B> <PRE><SPAN class=3Dcpp-preprocessor>#include =
<cstdio></SPAN>
<SPAN class=3Dcpp-keyword>extern</SPAN> <SPAN =
class=3Dcpp-string>"C"</SPAN> <SPAN class=3Dcpp-keyword>int</SPAN> =
_declspec(naked) sum(<SPAN class=3Dcpp-keyword>int</SPAN> a, <SPAN =
class=3Dcpp-keyword>int</SPAN> b) {
<SPAN class=3Dcpp-comment>// prolog code</SPAN>
<SPAN class=3Dcpp-keyword>_asm</SPAN> push ebp
<SPAN class=3Dcpp-keyword>_asm</SPAN> mov ebp, esp
<SPAN class=3Dcpp-comment>// code for add two variables and =
return</SPAN>
<SPAN class=3Dcpp-keyword>_asm</SPAN> mov eax, dword ptr [ebp + <SPAN =
class=3Dcpp-literal>8</SPAN>]
<SPAN class=3Dcpp-keyword>_asm</SPAN> add eax, dword ptr [ebp + <SPAN =
class=3Dcpp-literal>12</SPAN>]
=09
<SPAN class=3Dcpp-comment>// epilog code</SPAN>
<SPAN class=3Dcpp-keyword>_asm</SPAN> pop ebp
<SPAN class=3Dcpp-keyword>_asm</SPAN> ret
}
<SPAN class=3Dcpp-keyword>int</SPAN> main() {
<SPAN class=3Dcpp-keyword>int</SPAN> iRetVal;
sum(<SPAN class=3Dcpp-literal>3</SPAN>, <SPAN =
class=3Dcpp-literal>7</SPAN>);
<SPAN class=3Dcpp-keyword>_asm</SPAN> mov iRetVal, eax
printf(<SPAN class=3Dcpp-string>"%d\n"</SPAN>, iRetVal);
<SPAN class=3Dcpp-keyword>return</SPAN> <SPAN =
class=3Dcpp-literal>0</SPAN>;
}
</PRE>
<P>The output of this program is "10" i.e. the sum of two =
parameter 3 and=20
7. </P>
<P>This attributed is used in ATLBASE.H file to implement the =
member of=20
<CODE>_QIThunk</CODE> structure. This is structure is used to =
debug=20
reference counting the ATL program when =
<CODE>_ATL_DEBUG_INTERFACES</CODE>=20
are defined. </P>
<P>I hope to explore some other mysterious of ATL in next article. =
</P><!-- Article Ends --></DIV>
<H2>About Zeeshan Amjad</H2>
<TABLE width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD class=3DsmallText noWrap><BR></TD>
<TD class=3DsmallText width=3D"100%">Software Engineer at =
SoftPak=20
Financial Systems<BR><A=20
=
href=3D"http://www.softpak.com/">http://www.softpak.com/</A><BR><BR>And=20
sometimes teaching Object Oriented programming and C++ in =
University=20
of Karachi as a Visiting Faculty Member.<BR><A=20
href=3D"http://www.csku.edu.pk/">http://www.csku.edu.pk/</A>
<P class=3DsmallText>Click <A=20
=
href=3D"http://www.codeproject.com/script/profile/whos_who.asp?vt=3Darts&=
amp;id=3D5890">here</A>=20
to view Zeeshan Amjad's online =
profile.</P></TD></TR></TBODY></TABLE><BR>
<H2>Other popular ATL articles:</H2>
<UL>
<LI><A=20
=
href=3D"http://www.codeproject.com/atl/shellfoldertree.asp">ShellFolderTr=
ee</A><BR><SPAN=20
class=3Dsmalltext>Mimicking and extending the shell=92s =
folder-tree control=20
functionality</SPAN>
<LI><A href=3D"http://www.codeproject.com/atl/atlserver.asp">ATL =
Server -=20
Web Application/Web Service</A><BR><SPAN class=3Dsmalltext>Web=20
Application/Web Service development using ATL Server =
classes</SPAN>
<LI><A =
href=3D"http://www.codeproject.com/atl/newinatl7.asp">What's new in=20
ATL7</A><BR><SPAN class=3Dsmalltext>Overview of new classes in =
ATL7</SPAN>
<LI><A=20
=
href=3D"http://www.codeproject.com/atl/ietoolbartutorial.asp">Internet=20
Explorer Toolbar (Deskband) Tutorial</A><BR><SPAN =
class=3Dsmalltext>A=20
tutorial on Using RBDeskband and CWindowImpl ATL Object Wizards =
to=20
create an Internet Explorer(IE) Toolbar.</SPAN></LI></UL>
<FORM action=3D/script/rating/code/app/insert_vote.asp =
method=3Dpost><INPUT=20
type=3Dhidden value=3Datl_underthehood_4/atl6/5/2002 =
name=3Dvote_name> <INPUT=20
type=3Dhidden value=3D/atl/atl_underthehood_4.asp name=3Dgoal>=20
<TABLE cellSpacing=3D0 cellPadding=3D1 width=3D"100%" =
bgColor=3D#ff9900=20
border=3D0><TBODY>
<TR>
<TD width=3D"100%">
<TABLE cellSpacing=3D0 cellPadding=3D4 width=3D"100%" =
bgColor=3D#fbedbb=20
border=3D0>
<TBODY>
<TR>
<TD class=3Dsmalltext vAlign=3Dcenter>[<A=20
=
href=3D"http://www.codeproject.com/atl/atl_underthehood_4.asp#__top">Top<=
/A>]</TD>
<TD vAlign=3Dcenter noWrap align=3Dright><I><B>Rate this =
Article=20
for us!</B></I> =
<I>Poor</I><INPUT=20
type=3Dradio value=3D1 name=3Drate><INPUT type=3Dradio =
value=3D2=20
name=3Drate><INPUT type=3Dradio value=3D3 =
name=3Drate><INPUT=20
type=3Dradio value=3D4 name=3Drate><INPUT type=3Dradio =
value=3D5=20
name=3Drate><I>Excellent</I> <INPUT =
class=3DFormButton type=3Dsubmit value=3DVote>=20
=
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD vAlign=3Dtop>
<CENTER><A=20
=
href=3D"http://www.codeproject.com/script/admentor/admentorredir.asp?id=3D=
544&way=3Dban"=20
target=3D_blank><IMG height=3D60 alt=3D""=20
=
src=3D"http://www.codeproject.com/script/admentor/images/SQUARE%20LOGO4.g=
if"=20
width=3D120 border=3D0></A><BR><FONT size=3D1><A=20
href=3D"http://www.codeproject.com/info/MediaKit/">Premium=20
Sponsor</A></FONT></CENTER><BR></TD>
<TD vAlign=3Dtop noWrap align=3Dmiddle><SPAN id=3DAdBanner5><A =
=
href=3D"http://www.codeproject.com/script/admentor/admentorredir.asp?id=3D=
204&way=3Dban"=20
target=3D_blank><IMG height=3D60=20
alt=3D"Memory Validator - Faster leak detection, better =
analysis (Special Launch Offer! $299, Save $100)"=20
=
src=3D"http://www.codeproject.com/script/admentor/images/SoftwareValidato=
r.gif"=20
width=3D468=20
border=3D0></A></SPAN></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE=
><A=20
name=3D__comments></A>
<SCRIPT language=3DJavaScript>
function MsgVoteForm(UserID, MsgID)
{
document.write("Rate this message: ");
for (var i=3D1; i<=3D5;i++)
document.write("<a href=3D'#xx" + MsgID.toString() + "xx' =
title=3D'give this message a vote of " + i.toString() + "' =
onclick=3D'RateMsg(" + UserID.toString() + ", " + MsgID.toString() + ", =
" + i.toString() + ")'><b>" + i.toString() + "</b></a> ");
document.write(" (out of 5)");
}
function RateMsg(userid, msgid, score)=20
{=20
=
open("/script/comments/vote.asp?js=3D1&user=3D"+userid.toString()+"&msg=3D=
"+msgid.toString()+"&score=3D"+score.toString() =
,"helpful","toolbar=3Dno,resizable=3Dyes,scrollbars=3Dyes,directories=3Dn=
o,menubar=3Dno,width=3D300,height=3D200");
return false;
}
</SCRIPT>
<SCRIPT language=3DJavaScript>
var Selected =3D "";
var AdTime =3D new Date();
document.writeln("<iframe name=3DAdFrame width=3D0 height=3D0 =
frameborder=3D0></IFRAME>");
function SwitchMessage(elm)
{
var now =3D new Date();
if (Number(now.getTime() - AdTime.getTime()) > Number(5000))
{
AdTime =3D now;
var loc =3D window.frames["AdFrame"];
if (loc) loc =3D loc.document;
if (loc) loc =3D loc.location;
if (loc) loc.replace('/script/comments/fetchAd.asp?cat=3D2');
}
=09
var elmref;
if (Selected !=3D "")
{
elmref =3D eval("document.getElementById('" + Selected + "_h1')");
if (elmref) elmref.style.display =3D 'none';
elmref =3D eval("document.getElementById('" + Selected + "_h0')");
if (elmref) elmref.bgColor =3D '#FEF9E7';
}
if (Selected !=3D elm.name)=20
{
Selected =3D elm.name;
elmref =3D eval("document.getElementById('" + Selected + "_h1')");
if (elmref)=20
{
if (elmref.style.display=3D=3D'none') elmref.style.display=3D'';
else elmref.style.display =3D 'none';
}
elmref =3D eval("document.getElementById('" + Selected + "_h0')");
if (elmref)=20
elmref.bgColor =3D '#99CCFF';
}
else
Selected=3D"";
=09
window.location =3D "#xx" + Selected + "xx";
return false;
}
</SCRIPT>
<SCRIPT language=3Djavascript event=3Donclick for=3DDynMessLink>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -