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

📄 lib0029.html

📁 Memory Management—Algorithms and implementation in C/C++ Introduction Chapter 1 - Memory Manag
💻 HTML
📖 第 1 页 / 共 5 页
字号:
; likeCob.asm-------------------------

.MODEL small, c
.STACK

;working storage----------------------
.DATA
assets DW 0H
debt   DW 0H
net    DW 0H

;procedure division-------------------
.CODE
. STARTUP

MOV AX,07H<a name="371"></a><a name="IDX-176"></a>
MOV [assets],AX

MOV AX,03H
MOV [debt],AX

MOV DX, [assets]
MOV CX, [debt]
SUB DX, CX

ADD DX, '0'

MOV AH, 0EH
MOV AL, DL
INT 10H

.EXIT
END
</pre>
</div>
<p class="para">This programming approach might not seem so bad. In fact, at first glance, it may appear like an effective way to organize an application. Do not be fooled by such na&iuml;ve first impressions. With only a single section of working storage to provide read/write memory, a program can become very difficult to read if you want to do anything even remotely complicated.</p>
<p class="para">Consider the following COBOL program that takes a list of values and prints out the average of those values and the maximum value in the list. You should be able to see how COBOL's limitations make simple array manipulation nowhere near as straightforward as it would be in C.</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">000012 IDENTIFICATION DIVISION.</span>
<span style="background-color:d9d9d9">000020 PROGRAM-ID. STATISTICS.</span>
<span style="background-color:d9d9d9">000021*------------------------------------------------</span>
<span style="background-color:d9d9d9">000022 DATA DIVISION.</span>
<span style="background-color:d9d9d9">000023 WORKING-STORAGE SECTION.</span>
<span style="background-color:d9d9d9">000024 01 AVERAGE  PIC 9(2) VALUE 0.</span>
<span style="background-color:d9d9d9">000025 01 ARRAY-SIZE PIC 9(1) VALUE 5.</span>
<span style="background-color:d9d9d9">000027 01 ARRAY.</span>
<span style="background-color:d9d9d9">000028 03 ARRAY-ELM  PIC 9(2) OCCURS 5 TIMES.</span>
<span style="background-color:d9d9d9">000029 01 COUNT-INDEX PIC 9(1) VALUE 1.</span>
<span style="background-color:d9d9d9">000030 01 ARRAY-MAX  PIC  9(1) VALUE 0.</span>
<span style="background-color:d9d9d9">000031*------------------------------------------------</span>
<span style="background-color:d9d9d9">000032 PROCEDURE DIVISION.</span>
<span style="background-color:d9d9d9">000033 SOURCECODE SECTION.</span>
<span style="background-color:d9d9d9">000034 MAIN.</span>
<span style="background-color:d9d9d9">000035     PERFORM INIT-ARRAY.</span>
<span style="background-color:d9d9d9">000040     PERFORM COMPUTE-AVERAGE.</span>
<span style="background-color:d9d9d9">000041     PERFORM GET-MAX.</span>
<span style="background-color:d9d9d9">000050      STOP RUN.</span><a name="372"></a><a name="IDX-177"></a>
<span style="background-color:d9d9d9">000051*-------------------------------------------------</span>
<span style="background-color:d9d9d9">000052 SUBROUTINES SECTION.</span>
<span style="background-color:d9d9d9">000053 INIT-ARRAY.</span>
<span style="background-color:d9d9d9">000054     MOVE 5 TO ARRAY-ELM (1).</span>
<span style="background-color:d9d9d9">000055     MOVE 6 TO ARRAY-ELM (2).</span>
<span style="background-color:d9d9d9">000056     MOVE 3 TO ARRAY-ELM (3).</span>
<span style="background-color:d9d9d9">000057     MOVE 7 TO ARRAY-ELM (4).</span>
<span style="background-color:d9d9d9">000058     MOVE 4 TO ARRAY-ELM (5).</span>
<span style="background-color:d9d9d9">000060 COMPUTE-AVERAGE.</span>
<span style="background-color:d9d9d9">000062     PERFORM COMPUTE-AVERAGE-SUM ARRAY-SIZE TIMES.</span>
<span style="background-color:d9d9d9">000064     DIVIDE ARRAY-SIZE INTO AVERAGE.</span>
<span style="background-color:d9d9d9">000065     DISPLAY "average is: " AVERAGE.</span>
<span style="background-color:d9d9d9">000070 COMPUTE-AVERAGE-SUM.</span>
<span style="background-color:d9d9d9">000071     ADD ARRAY-ELM(COUNT-INDEX) TO AVERAGE.</span>
<span style="background-color:d9d9d9">000081     ADD 1 TO COUNT-INDEX.</span>
<span style="background-color:d9d9d9">000091 GET-MAX.</span>
<span style="background-color:d9d9d9">000101     MOVE 1 TO COUNT-INDEX.</span>
<span style="background-color:d9d9d9">000102     MOVE ARRAY-ELM(1) TO ARRAY-MAX.</span>
<span style="background-color:d9d9d9">000111     PERFORM GET-MAX-EXAMINE-CURRENT ARRAY-SIZE</span>
           <span style="background-color:d9d9d9">TIMES.</span>
<span style="background-color:d9d9d9">000112     DISPLAY "max is: " ARRAY-MAX.</span>
<span style="background-color:d9d9d9">000121 GET-MAX-EXAMINE-CURRENT.</span>
<span style="background-color:d9d9d9">000131     IF ARRAY-ELM(COUNT-INDEX) &gt; ARRAY-MAX</span>
<span style="background-color:d9d9d9">000132        MOVE ARRAY-ELM(COUNT-INDEX) TO ARRAY-MAX</span>
<span style="background-color:d9d9d9">000141     END-IF</span>
<span style="background-color:d9d9d9">000151     ADD 1 TO COUNT-INDEX.</span>
</pre>
</div>
<p class="para">When this source code (<span class="fixed">statistics. cob</span>) is compiled and linked into an executable, it will produce the following output when run:</p>
<div class="informalexample">
<pre class="literallayout">
average is: 05
max is: 7
</pre>
</div>
<p class="last-para">The absence of the stack and heap may be a good thing from the view of a sysadmin, who does not have to worry about memory leaks or buffer overflow exploits, but from the perspective of a programmer, COBOL's spartan memory arrangement is a curse. Very large COBOL applications can quickly become impossible to maintain or even understand. As a veteran Y2K COBOL programmer, I can attest to this fact. Some of the programs I looked at were so large, complicated, and crucial to business operations, that I was often scared to touch anything.</p>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Case Study: </span>FORTRAN</span><a name="373"></a><a></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<p class="first-para">FORTRAN has the distinction of being considered one of the first compiled computer languages. The development of FORTRAN began in 1954 and was initiated by a team of engineers at IBM led by John Backus. FORTRAN originally stood for "IBM Mathematical <a name="374"></a><a name="IDX-178"></a>FORmula TRANslation system." Within 10 years, every hardware manufacturer in creation was shipping their computers with a FORTRAN compiler. Naturally, each vendor had to tweak FORTRAN so that they could call their compiler "value added." To help reign in chaos, a standards committee stepped in. In 1966, the first draft of the FORTRAN standard was released by the ASA (a predecessor to ANSI). This version of FORTRAN is known as FORTRAN 66. FORTRAN was the first high-level language to be specified by a standards committee.</p>
<p class="para">
<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">Any computer science student who has ever studied compiler theory will recognize the name Backus. This is because John Backus helped invent a notation called <i class="emphasis">Backus-Naur Form</i> (BNF), which is used to specify the context-free grammar of a programming language.</p>
</td>
</tr>
</table>
</p>
<p class="para">In 1977, the ANSI committee in charge of FORTRAN released a revised standard. It added several new features to the language including the <span class="fixed">CHARACTER</span> data type and flow-control constructs like IF-ELSE blocks (i.e., <span class="fixed">IF</span>... <span class="fixed">THEN</span>... <span class="fixed">ELSE</span>... <span class="fixed">ENDIF</span>). FORTRAN 77 is also known as F77.</p>
<p class="para">In 1990 and 1995, ANSI released new standards for FORTRAN. FORTRAN 90 (F90) was a major revision. FORTRAN 95 (F95) merely added a few extensions. F90, as specified in ANSI X3.198-1992, supplemented F77 with new features like dynamic memory allocation (via <span class="fixed">ALLOCATE</span> and <span class="fixed">DEALLOCATE</span>) and a stack to support recursion. However, because of the time lag between the F77 and the F90 standard, other languages were able to win popularity, which pushed FORTAN into the backwaters of history.</p>
<p class="para">For the sake of illustration, I will be looking at FORTRAN 77. F77 occupies the next level of sophistication above COBOL 85 in terms of language features, and this will make it a good stepping-stone.</p>
<p class="para">
<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">A Control Data veteran once confided in me that, in his day, FORTRAN programmers looked down on COBOL programmers. This was because FORTRAN is geared toward analytic programs that perform sophisticated numerical computation, instead of the mundane dollars-and-cents math that is a core component of COBOL programs. FORTRAN programmers were scientists in white coats, and COBOL programmers were corporate schlubs who sat in cubes.</p>
</td>
</tr>
</table>
</p>
<p class="para">F77, from an organizational standpoint, actually provides much better procedure modularity when compared to COBOL 85. Specifically, an F77 program consists of:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">A single <span class="fixed">PROGRAM</span> procedure</p>
</li>
<li class="listitem">
<p class="first-para">Zero or more external procedures</p>
</li>
</ul>
<a name="375"></a><a name="IDX-179"></a>
<p class="para">An external procedure can be a function or a subroutine. A <i class="emphasis">function</i> is a procedure that can possess multiple arguments but returns only a single output value via its name identifier. A subroutine is invoked by a <span class="fixed">CALL</span> statement and can accept an arbitrary number of input and output parameters.</p>
<p class="para">Here is a simple program to help illustrate these concepts:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">*-- metrics.F ---------------------------------------</span>
      <span style="background-color:d9d9d9">PROGRAM METRICS</span>
      <span style="background-color:d9d9d9">INTEGER ARRAY(5)</span>
      <span style="background-color:d9d9d9">INTEGER MAX</span>
      <span style="background-color:d9d9d9">ARRAY(1)=4</span>
      <span style="background-color:d9d9d9">ARRAY(2)=10</span>
      <span style="background-color:d9d9d9">ARRAY(3)=26</span>
      <span style="background-color:d9d9d9">ARRAY(4)=8</span>
      <span style="background-color:d9d9d9">ARRAY(5)=3</span>
      <span style="background-color:d9d9d9">MAX=0</span>
      <span style="background-color:d9d9d9">WRITE(*,*) "val= ", GETAVG(ARRAY,5)</span>
      <span style="background-color:d9d9d9">CALL GETMAX(ARRAY,5,MAX)</span>
      <span style="background-color:d9d9d9">WRITE(*,*) "max= ", MAX</span>
      <span style="background-color:d9d9d9">END</span>
<span style="background-color:d9d9d9">*----------------------------------------------------</span>
      <span style="background-color:d9d9d9">REAL FUNCTION GETAVG(ARR,NELM)</span>
      <span style="background-color:d9d9d9">INTEGER ARR(NELM)</span>
      <span style="background-color:d9d9d9">INTEGER INDEX</span>
      <span style="background-color:d9d9d9">REAL SUM</span>
      <span style="background-color:d9d9d9">SUM=0</span>
      <span style="background-color:d9d9d9">DO 10,INDEX = 1,NELM</span>
      <span style="background-color:d9d9d9">SUM=SUM+ARR(INDEX)</span>
<span style="background-color:d9d9d9">10    CONTINUE</span>
      <span style="background-color:d9d9d9">GETAVG = SUM/NELM</span>
      <span style="background-color:d9d9d9">END</span>
<span style="background-color:d9d9d9">*----------------------------------------------------</span>

⌨️ 快捷键说明

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