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

📄 6.5.2.htm

📁 建立《编译原理网络课程》的目的不仅使学生掌握构造编译程序的原理和技术
💻 HTM
字号:
<html>

<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>

<body>

<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.1.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.3.htm'"></img></td>
</tr>
</table>
<br><br>

<font class="title2"><b>6.5.2 引用调用</b></font>         

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
引用调用(call-by-reference)也称传地址调用(call-by-address)或传位置调用(call-by-location),是指把实在参数的地址传递给相应的形式参数,或者说调用过程把一个指向实在参数的存储地址的指针传递给被调用过程的相应的形式参数。实现如下: 
</p>
</td></tr></table>


<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
1.如果一个实在参数是一个名字或者是一个具有左-值的表达式,那么这个左-值本身被传递。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
2.然而,如果实在参数是一个表达式,如a+b或2等,它们无左-值,那么计算出表达式的值并放人新的存储单元,然后将此单元的地址传递过去。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
在目标代码中,在被调用过程中对形式参数的一次引用就成为对传递给被调用过程的指针的一个间接引用。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
 (1) <b><font color="#0000FF">program</font> </b>reference(input,output); <br>  
&nbsp;&nbsp;&nbsp;  (2)&nbsp;&nbsp; <b><font color="#0000FF">var</font></b> a,b: integer; <br>  
&nbsp;&nbsp;&nbsp;  (3)&nbsp;&nbsp; <b><font color="#0000FF">procedure</font></b> swap(var x,y: integer); <br>  
&nbsp;&nbsp;&nbsp;  (4) <b>&nbsp;&nbsp;&nbsp; <font color="#0000FF">var</font></b> temp: integer; <br>  
&nbsp;&nbsp;&nbsp;  (5) <b>&nbsp;&nbsp;&nbsp; <font color="#0000FF">begin</font></b> <br> 
&nbsp;&nbsp;&nbsp;  (6) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp:=x;<br> 
&nbsp;&nbsp;&nbsp;  (7) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x:=y;<br> 
&nbsp;&nbsp;&nbsp;  (8) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y:=temp  <br> 
&nbsp;&nbsp;&nbsp;  (9) <b>&nbsp;&nbsp; &nbsp;<font color="#0000FF">end</font></b>; <br> 
&nbsp;&nbsp;&nbsp;  (10) <b>&nbsp;<font color="#0000FF">begin</font></b> <br> 
&nbsp;&nbsp;&nbsp;  (11)&nbsp;&nbsp;&nbsp; a:=1;b:=2; <br>  
&nbsp;&nbsp;&nbsp;  (12) &nbsp;&nbsp;&nbsp;swap(a,b); <br> 
&nbsp;&nbsp;&nbsp;  (13)&nbsp;&nbsp;&nbsp;&nbsp;writeln('a=',a); writeln('b=', b)<br>  
&nbsp;&nbsp;&nbsp;  (14) <b>&nbsp;<font color="#0000FF">end</font></b>. </p> 
<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>图6.24</b>  过程swap的形式参数x和y为变量参数
<p>
</p>
</td></tr></table>
    


<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
<b>例6.6</b>  考虑图6.24中的过程swap。其中形式参数x和y为变量参数。一个带有实在参数i和a[i]的对swap的调用,即swap(i,a[i]),与下列步骤具有同样的作用: 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
1.把i和a[i]的地址(即左-值)复制到被调用过程的活动记录中,或说复制到分别与x和y相对应的存储单元argl和arg2中。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
2.把temp的内容置为arg1所指向的单元中存放的内容。这一步对应于过程swap的定义中的第(6)行的temp:=x。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
3.把由arg1所指向的单元的内容置为arg2所指向的单元的值。这一步对应于swap内的第(7)行上的x:=y。 
</p>
</td></tr></table>
    
<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
4.把由arg2所指向的单元的内容置为temp的值。这一步相当于y=temp。 
</p>
</td></tr></table>

<table><tr><td>&nbsp&nbsp&nbsp&nbsp</td>
<td class="content">
<P>
有相当多的语言使用引用调用。在Pascal中变量参数就是用这种方法传递的。  
</p>
</td></tr></table>

<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.1.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.3.htm'"></img></td>
</tr>
</table>

</BODY>

⌨️ 快捷键说明

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