⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib0020.html

📁 Memory Management—Algorithms and implementation in C/C++ Introduction Chapter 1 - Memory Manag
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Case Study: MS-DOS</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="LiB0019.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0021.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="ch02"></a>
<div class="section">
<h2 class="first-section-title"><a name="135"></a><a name="ch02lev1sec1"></a>Case Study: MS-DOS</h2><div class="section">
<h3 class="sect3-title">
<a name="136"></a><a name="ch02lev2sec1"></a>DOS Segmentation and Paging</h3>
<p class="first-para">Microsoft's disk operating system (DOS) is a study in minimalism. The operating system's conspicuous lack of advanced features, however, was not completely intentional. Rather, the nature of DOS was a result of the context in which it was constructed. Back in 1980, when Bill Gates offered to provide an operating system for IBM's first PC, the hardware (i.e., the 8088) didn't support anything other than strict real mode addressing. To compound matters, Microsoft was under such strict time-to-market constraints that they out-sourced everything by purchasing a CP/M clone written for the 8088 by a man named Tim Paterson.</p>
<p class="para">Suffice it to say, talking about memory segmentation and paging on DOS is of limited use, primarily because neither of these features exist. DOS operates strictly in real mode, where it is very easy for a program in execution to pillage memory.</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&nbsp;</td><td valign="top" class="admon-body">
<p class="first-para">It is interesting to see how being at the right place at the right time can change history. IBM had originally intended to use the CP/M operating system sold by Digital Research. For whatever reason, the deal fell through and Microsoft was there to catch the ball. Had Bill Gates decided to complete his degree at Harvard, or had Digital Research not bungled its deal with IBM, we would all probably be using Apple computers.</p>
</td>
</tr>
</table>
<a name="137"></a><a name="IDX-47"></a>
<a></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="138"></a><a name="ch02lev2sec2"></a>DOS Memory Map</h3>
<p class="first-para">The DOS operating system consists of three files:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">
<span class="fixed">IO.SYS</span>
</p>
</li>
<li class="listitem">
<p class="first-para">
<span class="fixed">MSDOS.SYS</span>
</p>
</li>
<li class="listitem">
<p class="first-para">
<span class="fixed">COMMAND.COM</span>
</p>
</li>
</ul>
<p class="para">
<span class="fixed">IO.SYS</span> interfaces directly to the computer's hardware. It is written, or at least modified, by the Original Equipment Manufacturer (OEM). All requests for hardware services from user programs must travel through <span class="fixed">MSDOS.SYS</span>, a device-neutral I/O manager, which translates the request into a format that can then be passed to <span class="fixed">IO.SYS.MSDOS.SYS</span> can be thought of as the kernel of DOS (although your definition of "kernel" would have to be pretty loose). <span class="fixed">COMMAND.COM</span> is a command interpreter.</p>
<p class="para">
<span class="fixed">IO.SYS</span> and <span class="fixed">MSDOS.SYS</span> are both core system files. Without these files, there is no DOS. The command interpreter, on the other hand, can be replaced. There were several companies in the 1980s that offered their own, enhanced version of <span class="fixed">COMMAND.COM</span>. How many readers remember Norton Commander?</p>
<p class="para">Before DOS is loaded, the real mode address space of a bare computer resembles that displayed in <a class="internaljump" href="#ch02fig01">Figure 2.1</a>. A bare-bones DOS bootstrap will load all of its components into the region between the BIOS data and video RAM.</p>
<div class="figure">
<a name="139"></a><a name="ch02fig01"></a><span class="figuremediaobject"><a href="images/fig75%5F01%5F0%2Ejpg" NAME="IMG_23" target="_parent"><img src="images/fig75_01.jpg" height="273" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 2.1</span></span>
</div>
<a name="140"></a><a name="IDX-48"></a>
<p class="para">Low memory is populated by BIOS code and the interrupt vector table (IVT). The first 640KB of memory is known as <i class="emphasis">conventional memory</i>. This is where DOS will load itself and run its user programs. There are also several small regions of memory not used by the video hardware or the BIOS. These are called <i class="emphasis">Upper Memory Blocks</i> (UMBs). The region of memory above the 1MB limit is known as <i class="emphasis">extended memory</i>. In the <a class="internaljump" href="#ch02lev2sec3">next section</a>, I will discuss extended memory in more depth. For now, you can assume that DOS is confined to a 1MB space.</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&nbsp;</td><td valign="top" class="admon-body">
<p class="first-para">
<i class="emphasis">Expanded memory</i> is extra storage space that was created by swapping memory below the 1MB mark to special memory chips. Expanded memory is strictly an historical artifact and its use was replaced by extended memory.</p>
</td>
</tr>
</table>
<p class="para">When a DOS machine boots, the BIOS locates a bootable disk and loads its boot sector to <span class="fixed">0000[0]:7C00</span>. The boot sector then inspects its disk for <span class="fixed">IO.SYS</span>. How do I know this? One quick way to verify this without performing a gruesome disassembly is to dump the boot sector and look for character data that is hard coded.</p>
<p class="para">This is how the <span class="fixed">debug</span> utility could be used to perform this type of investigation:</p>
<div class="informalexample">
<pre class="literallayout">
C:\WINDOWS&gt;debug
-l cs:0100 0 0 1
-d cs:0280
158E:0280 C3 B4 02 8B 16 4D 7C B1-06 D2 E6 0A 36 4F 7C 8B .....M|.....6O|.
158E:0290 CA 86 E9 8A 16 24 7C 8A-36 25 7C CD 13 C3 0D 0A .....$|.6%|.....
158E:02A0 4E 6F 6E 2D 53 79 73 74-65 6D 20 64 69 73 6B 20 Non-System disk
158E:02B0 6F 72 20 64 69 73 6B 20-65 72 72 6F 72 0D 0A 52 or disk error..R
158E:02C0 65 70 6C 61 63 65 20 61-6E 64 20 70 72 65 73 73 eplace and press
158E:02D0 20 61 6E 79 20 6B 65 79-20 77 68 65 6E 20 72 65  any key when re
158E:02E0 61 64 79 0D 0A 00 49 4F-20 20 20 20 20 20 53 59 ady...IO      SY
158E:02F0 53 4D 53 44 4F 53 20 20-20 53 59 53 00 00 55 AA SMSDOS   SYS..U.
-q
</pre>
</div>
<p class="para">The first thing I do is load the boot sector into memory at <span class="fixed">CS:0100</span>. Then I just start perusing the memory image until I hit pay dirt. As you can see, the string "<span class="fixed">IO.SYS</span>" is there. This doesn't necessarily prove that DOS looks for <span class="fixed">IO.SYS</span> and loads it (to prove this, you would have to disassemble), but it provides evidence.</p>
<p class="para">Once <span class="fixed">IO.SYS</span> has been loaded, it locates <span class="fixed">MSDOS.SYS</span> and loads it into memory. The operating system, whose core files now exist in memory, then loads <span class="fixed">COMMAND.COM</span>. After this, the <span class="fixed">CONFIG. SYS</span> configuration file will be processed to load device drivers and set system parameters. Finally, <span class="fixed">COMMAND. COM</span> will automatically process a batch file of commands named <span class="fixed">AUTOEXEC.BAT</span>. The <span class="fixed">AUTOEXEC.BAT</span> file is used to set up the operating system's <a name="141"></a><a name="IDX-49"></a>environment (i.e., PATH and TEMP variables) and load Terminate and Stay Resident (TSR) programs.</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&nbsp;</td><td valign="top" class="admon-body">
<p class="first-para">TSR programs are loaded into memory and remain there even after they terminate so that they can be reactivated quickly. The <span class="fixed">doskey</span> program, which supports command line history services, is a good example of a TSR as is the DOS mouse driver. Because TSRs are usually small, they are typically stuck into a UMB if possible.</p>
</td>
</tr>
</table>
<a></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="142"></a><a name="ch02lev2sec3"></a>Memory Usage</h3>
<p class="first-para">The <span class="fixed">mem</span> command can be used to obtain a snapshot of the operating system's memory usage:</p>
<div class="informalexample">
<pre class="literallayout">
Memory Type        Total  =  Used  +   Free
----------------  -------  -------    ------
Conventional         638K      97K      541K
Upper                  0K       0K        0K
Reserved               0K       0K        0K
Extended (XMS)    65,532K  65,532K        0K
----------------  -------  -------    ------
Total memory      66,170K  65,629K      541K

Total under 1 MB     638K      97K      541K

Largest executable program size  541K (553,664 bytes)
Largest free upper memory block    0K       (0 bytes)
</pre>
</div>
<p class="para">The <span class="fixed">mem / D</span> command can also be utilized to get a precise look at where, in conventional memory, DOS has placed its core components:</p>
<div class="informalexample">
<pre class="literallayout">
Conventional Memory Detail:

  Segment       Total         Name         Type
  -------  ----------------   -----------  -------
   00000      1,039    (1K)                Interrupt Vector
   00040        271    (0K)                ROM Communication Area
   00050        527    (1K)                DOS Communication Area
   00070      2,560    (3K)   IO           System Data
                                 CON       System Device Driver
                                 AUX       System Device Driver
                                 PRN       System Device Driver
                                 CLOCK$    System Device Driver
                                 A: - B:   System Device Driver
                                 COM1      System Device Driver
                                 LPT1      System Device Driver
                                 LPT2      System Device Driver
                                 LPT3      System Device Driver
                                 COM2      System Device Driver
                                 COM3      System Device Driver
                                 COM4      System Device Driver
   00110     42,784     (42K) MSDOS        System Data<a name="143"></a><a name="IDX-50"></a>
   00B82     10,832     (11K)  IO          System Data
                192      (0K)              FILES=8
                256      (0K)              FCBS=4
              7,984      (8K)              BUFFERS=15
                448      (0K)              LASTDRIVE=E
              1,856      (2K)              STACKS=9,128
   00E27      4,720      (5K)  COMMAND     Program
   00F4E         80      (0K)  MSDOS       -- Free --
   00F53        272      (0K)  COMMAND     Environment
   00F64        112      (0K)  MEM         Environment
   00F6B     88,992     (87K)  MEM         Program
   02525    501,168    (489K)  MSDOS       -- Free --
</pre>
</div>
<p class="last-para">As you can see, most of the system binaries are loaded into low memory, where they share space with vital system data structures. The segment addresses provided in the previous listing will need to be multiplied by 16 (i.e., append the implied zero) in order to specify an actual segment.</p>
<a></a>
</div>
<div class="section">
<h3 class="sect3-title">
<a name="144"></a><a name="ch02lev2sec4"></a>Example: A Simple Video Driver</h3>
<p class="first-para">Because DOS offers no memory protection, it is possible to sidestep DOS and the BIOS and write your own video driver.</p>
<p class="para">It so happens that text characters can be sent to the screen by modifying VRAM. For example, VGA color text mode video RAM starts at <span class="fixed">0xB8000</span>. A VGA text screen consists of 80 columns and 25 rows. Each character requires a word of memory, so 4,000 bytes of VRAM are consumed. The low byte of each word stores an ASCII character, and the high byte of each character is an attribute byte. I am going to keep things simple and use a monochrome scheme so the attribute byte is <span class="fixed">0xF</span>.</p>
<p class="para">The following program takes advantage of this fact by clearing the screen and displaying a small message.</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">/* --crtio.c-- */</span>

<span style="background-color:d9d9d9">int offset;</span>

<span style="background-color:d9d9d9">/*</span>

    <span style="background-color:d9d9d9">Have 80x25 screen</span>
    <span style="background-color:d9d9d9">Each screen character in VRAM is described by two bytes:</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -