📄 lib0027.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Compiler-Based Allocation</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.books24x7.css">
</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0026.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0028.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
<div class="chapter">
<a name="ch03"></a>
<div class="section">
<h2 class="first-section-title"><a name="296"></a><a name="ch03lev1sec2"></a>Compiler-Based Allocation</h2><p class="first-para">User applications typically have their address space divided into four types of regions:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">
<a class="internaljump" href="#ch03lev2sec2">Code section</a>
</p>
</li>
<li class="listitem">
<p class="first-para">
<a class="internaljump" href="#ch03lev2sec1">Data section</a>
</p>
</li>
<li class="listitem">
<p class="first-para">
<a class="internaljump" href="#ch03lev2sec3">Stack</a>
</p>
</li>
<li class="listitem">
<p class="first-para">Heap</p>
</li>
</ul>
<p class="para">An application may have more than one section of a particular type (see <a class="internaljump" href="#ch03fig02">Figure 3.2</a>). For example, an application may have multiple code sections and stacks.</p>
<div class="figure">
<a name="297"></a><a name="ch03fig02"></a><span class="figuremediaobject"><a href="images/fig157%5F01%5F0%2Ejpg" NAME="IMG_51" target="_parent"><img src="images/fig157_01.jpg" height="268" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 3.2</span></span>
</div>
<p class="para">Sometimes an application may have only a single section that is a hybrid of different types (see <a class="internaljump" href="#ch03fig03">Figure 3.3</a>). For example, DOS <span class="fixed">.COM</span> executables, loaded into memory, consist of a single section. Data, as long as it is not executed, can be interspersed with code. The stack pointer register (<span class="fixed">SP</span>) is set to the end of the executable's image so that the last few bytes serve as an informal stack.</p>
<div class="figure">
<a name="298"></a><a name="ch03fig03"></a><span class="figuremediaobject"><a href="images/fig158%5F01%5F0%2Ejpg" NAME="IMG_52" target="_parent"><img src="images/fig158_01.jpg" height="331" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 3.3</span></span>
</div>
<a name="299"></a><a name="IDX-130"></a>
<p class="para">A <span class="fixed">.COM</span> file has no official boundaries. If the stack overflows into the heap, or if the program is so large that the stack spills into the code, then you are out of luck. Here is a small <span class="fixed">.COM</span> file program so that you can see what I am talking about:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">; --smallCom.asm--</span>
<span style="background-color:d9d9d9">.386</span>
<span style="background-color:d9d9d9">mycode SEGMENT USE16</span>
<span style="background-color:d9d9d9">ASSUME CS:mycode,DS:mycode,SS:mycode,ES:mycode</span>
<span style="background-color:d9d9d9">ORG 100H</span>
<span style="background-color:d9d9d9">;code region----------------------------------</span>
<span style="background-color:d9d9d9">entry:</span>
<span style="background-color:d9d9d9">PUSH DS</span>
<span style="background-color:d9d9d9">MOV AH,0H</span>
<span style="background-color:d9d9d9">PUSH AX</span>
<span style="background-color:d9d9d9">MOV [oldstack],SP</span>
<span style="background-color:d9d9d9">MOV SP,OFFSET stktop</span>
<span style="background-color:d9d9d9">MOV AX,OFFSET string1</span>
<span style="background-color:d9d9d9">PUSH AX</span>
<span style="background-color:d9d9d9">CALL printStr</span>
<span style="background-color:d9d9d9">MOV AX,OFFSET string2</span>
<span style="background-color:d9d9d9">PUSH AX</span>
<span style="background-color:d9d9d9">CALL printStr</span>
<span style="background-color:d9d9d9">MOV SP,[oldstack]</span>
<span style="background-color:d9d9d9">RETF</span>
<span style="background-color:d9d9d9">;data region----------------------------------</span>
<span style="background-color:d9d9d9">oldstack D W ?</span><a name="300"></a><a name="IDX-131"></a>
<span style="background-color:d9d9d9">string1 DB "the hot side stays hot--"</span>
<span style="background-color:d9d9d9">end1 DB '$'</span>
<span style="background-color:d9d9d9">string2 DB "the cool side stays cool"</span>
<span style="background-color:d9d9d9">end2 DB '$'</span>
<span style="background-color:d9d9d9">;stack region---------------------------------</span>
<span style="background-color:d9d9d9">stkbody DB 31 dup ('01')</span>
<span style="background-color:d9d9d9">stktop DB 01H</span>
<span style="background-color:d9d9d9">;code region----------------------------------</span>
<span style="background-color:d9d9d9">printStr:</span>
<span style="background-color:d9d9d9">PUSH BP</span>
<span style="background-color:d9d9d9">MOV BP,SP</span>
<span style="background-color:d9d9d9">MOV AH,09H</span>
<span style="background-color:d9d9d9">MOV DX,[BP+4]</span>
<span style="background-color:d9d9d9">INT 21H</span>
<span style="background-color:d9d9d9">POP BP</span>
<span style="background-color:d9d9d9">RET</span>
<span style="background-color:d9d9d9">mycode ENDS</span>
<span style="background-color:d9d9d9">END entry</span>
</pre>
</div>
<p class="para">Here is the build command with MASM: <span class="fixed">C:\MASM\ SRC> ML /AT smallCom.asm</span>
</p>
<p class="para">When you run this application, the following message is printed to the screen:</p>
<div class="informalexample">
<pre class="literallayout">
C:\MASM\SRC>smallCom
the hot side stays hot--the cool side stays cool
</pre>
</div>
<p class="para">As you can see, I have placed data, not to mention a whole entire stack, dead in the middle of executable code. There are really very few rules in the case of a <span class="fixed">.COM</span> binary. Most current executable formats, like the ELF file format or the PE file format, have more strict and established rules with regard to program section arrangement.</p>
<table border="0" cellspacing="0" cellpadding="0" class="note">
<tr>
<td valign="top" class="admon-check"></td><td valign="top" class="admon-title">Note </td><td valign="top" class="admon-body">
<p class="first-para">Regardless of how many or what type of sections a program has, the general rule is that the stack grows down from a high address and the heap grows up from a low address.</p>
</td>
</tr>
</table>
<div class="qandaset">
<table border="0" cellpadding="0">
<tr class="qandaentry">
<td class="td" valign="top" width="2%">
<p class="first-para">
<a name="LiB12"><b>1. </b></a>
</p>
</td><td class="td" valign="top" width="90%">
<p class="first-para">What does all of this memory partitioning have to do with development tools?</p>
</td><td class="td" valign="top" width="8%">
<p class="first-para">
<a href="#LiB11"><img src="images/question.gif" height="24" width="24" alt="A compiler is a development tool that acts as a translator. It consumes human-readable source code, moves the source through its compound digestive track, and then emits native machine instructions (or some other type of bytecode). A compiler also determines how the emitted binary values will be organized to provide the four types of memory sections described previously. In general, compilers control how memory is arranged, allocated, accessed, and updated in every section, except the heap. Managing the heap is the domain of user libraries and virtual machines. " border="0"></a>
</p>
</td>
</tr>
</table>
<p class="bold">Answers</p>
<table border="0" cellpadding="0">
<tr class="qandaentry-answer">
<td class="td" valign="top" width="2%">
<p class="first-para">
<a class="internaljump" name="answer.nr-qandaentry.E309676A-AB09-4BAE-A4F2-76DB3CCE89A7" href="#LiB12"><b>1.</b></a> </p>
</td><td class="td" valign="top" width="90%">
<p class="first-para">A compiler is a development tool that acts as a translator. It consumes human-readable source code, moves the source through its compound digestive track, and then emits native machine instructions (or some other type of bytecode). A <a name="301"></a><a name="IDX-132"></a>compiler also determines how the emitted binary values will be organized to provide the four types of memory sections described previously. <i class="emphasis">In general, compilers control how memory is arranged, allocated, accessed, and updated in every section, except the heap.</i> Managing the heap is the domain of user libraries and virtual machines.</p>
</td>
</tr>
</table>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="302"></a><a name="ch03lev2sec1"></a>Data Section</h3>
<p class="first-para">The data section of an application traditionally supplies what is known as static memory. As I mentioned earlier, static memory regions are fixed in size and exist for the duration of an application's life span.</p>
<p class="para">Given these two characteristics, most compilers will construct data sections to serve as storage for global data. For example, consider the following C program:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">#include<string.h></span>
<span style="background-color:d9d9d9">struct employee</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">char firstname[32];</span>
<span style="background-color:d9d9d9">char lastname[32];</span>
<span style="background-color:d9d9d9">unsigned char age;</span>
<span style="background-color:d9d9d9">unsigned int salary;</span>
<span style="background-color:d9d9d9">};</span>
<span style="background-color:d9d9d9">struct employee architect = {"Gil","Bates",45,100000};</span>
<span style="background-color:d9d9d9">struct employee ceo = {"Reed","Almer",42,95000};</span>
<span style="background-color:d9d9d9">struct employee drone;</span>
<span style="background-color:d9d9d9">void main()</span>
<span style="background-color:d9d9d9">{</span>
<span style="background-color:d9d9d9">strcpy(drone.firstname,"bill");</span>
<span style="background-color:d9d9d9">strcpy(drone.lastname,"blunden");</span>
<span style="background-color:d9d9d9">drone.age=35;</span>
<span style="background-color:d9d9d9">drone.salary=(int) (3.5);</span>
<span style="background-color:d9d9d9">return;</span>
<span style="background-color:d9d9d9">}</span>
</pre>
</div>
<p class="para">If we look at a listing file, it is clear that the global variables above have been isolated in their own reserved program section called <span class="fixed">_DATA</span>. This section will have a fixed size and exist from the time the program starts until the time that it exits.</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">.386P</span>
<span style="background-color:d9d9d9">.model FLAT</span>
<span style="background-color:d9d9d9">PUBLIC _architect</span>
<span style="background-color:d9d9d9">PUBLIC _ceo</span><a name="303"></a><a name="IDX-133"></a>
<span style="background-color:d9d9d9">_DATA SEGMENT</span>
<span style="background-color:d9d9d9">COMM _drone:BYTE:048H</span>
<span style="background-color:d9d9d9">_architect DB 'Gil', 00H</span>
<span style="background-color:d9d9d9">ORG $+28</span>
<span style="background-color:d9d9d9">DB 'Bates', 00H</span>
<span style="background-color:d9d9d9">ORG $+26</span>
<span style="background-color:d9d9d9">DB 02dH</span>
<span style="background-color:d9d9d9">ORG $+3</span>
<span style="background-color:d9d9d9">DD 0186aOH</span>
<span style="background-color:d9d9d9">_ceo DB 'Reed', 00H</span>
<span style="background-color:d9d9d9">ORG $+27</span>
<span style="background-color:d9d9d9">DB 'Almer', 00H</span>
<span style="background-color:d9d9d9">ORG $+26</span>
<span style="background-color:d9d9d9">DB 02aH</span>
<span style="background-color:d9d9d9">ORG $+3</span>
<span style="background-color:d9d9d9">DD 017318H</span>
<span style="background-color:d9d9d9">_DATA ENDS</span>
<span style="background-color:d9d9d9">PUBLIC _main</span>
<span style="background-color:d9d9d9">EXTRN _strcpy:NEAR</span>
<span style="background-color:d9d9d9">_DATA SEGMENT</span>
<span style="background-color:d9d9d9">$SG117 DB 'bill', 00H</span>
<span style="background-color:d9d9d9">ORG $+3</span>
<span style="background-color:d9d9d9">$SG118 DB 'blunden', 00H</span>
<span style="background-color:d9d9d9">_DATA ENDS</span>
<span style="background-color:d9d9d9">_TEXT SEGMENT</span>
<span style="background-color:d9d9d9">_main PROC NEAR</span>
<span style="background-color:d9d9d9">; 16 : {</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -