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

📄 5.htm

📁 浙江大学计算机学院计算机组成教材《计算机组成与设计》(潘学增)的课堂教学课件。
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>程序范例 </title> 
 
 
<meta name="Microsoft Theme" content="arcs 011"> 
<meta name="Microsoft Border" content="none"> 
</head> 
 
<body background="arctile.jpg" bgcolor="#FFFFFF" text="#000000" link="#3399FF" vlink="#666666" alink="#FF9900"> 
 
<p align="center"><font color="#000000"><b><font size="6">3.5</font>&nbsp;&nbsp;&nbsp;&nbsp;    
<big><big><big>程序范例</big></big></big></b><strong>     
&nbsp;</strong></font></p>     
     
<p align="right"><font color="#000000"><a href="index.htm"><img border="0" src="../images/back.gif" align="left" width="60" height="45"></a><strong>&nbsp;&nbsp;&nbsp;&nbsp;</strong><a href="4.htm"><img border="0" src="../images/prev.gif" width="60" height="45"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     
<a href="6.htm"><img border="0" src="../images/forw.gif" width="60" height="45"></a></font></p>    
    
<p><strong><font color="#000000" size="4">为了加深我们对指令的理解,在本节让我们来看一个程序范例,其C程序代码如下:</font></strong></p>   
   
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
swap(int v[],int k)</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
int temp; &nbsp;&nbsp;&nbsp;</font> </strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
temp=v[k];</font> </strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
v[k]=v[k+1];</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
v[k+1]=temp;</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</font></strong></p>     
     
<p><strong><font color="#000000" size="4">这是一个在C语言中常见的交换过程。将一个C程序转化为汇编语言一般是三步曲:</font></strong></p>  
  
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1.&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27"> <a href="#1">为程序变量分配寄存器</a>;</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27"> <a href="#2">程序主体的代码</a>,  
即汇编编程;</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27"> <a href="#3.">在程序调用过程中保护过程中用到的寄存器</a>。</font></strong></p>    
    
<p><strong><font color="#000000" size="4">下面我们分三步来讨论这个程序:</font></strong></p>  
  
<p><strong><a name="1"><font size="4" color="#000000">1</font></a><font size="4" color="#000000">.&nbsp;&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27">  
寄存器的分配。</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp; 根据我们的通常约定,一般用寄存器$4,$5,$6,$7来保存程序调用中的参数。在该程序中,只有两个参数v和k,所以我们把v和k分别放</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp; 了寄存器$4,$5中。另外仅有的一个变量<em>temp</em>被保存在$15中。</font></strong></p>     
     
<p><strong><a name="2"><font size="4" color="#000000">2</font></a><font size="4" color="#000000">. &nbsp;<img border="0" src="../images/sub.gif" width="26" height="27">&nbsp;  
由于存储器是以字节编址的,所以数K在内存中的地址是4的倍数。理解这一点对我们理解  
汇编语言很重要。</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp; 该程序的程序主体代码应该是:</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
muli &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $2,$5,4      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
#register &nbsp; $2=k*4</font> </strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
add &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $2,$4,$2      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
#register &nbsp; $2=v+(k*4)</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
lw &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $15,0($2)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
#register &nbsp; $2 has the address of v[k]</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
lw &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $16,4($2)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
#reg $15(temp)=v[k]</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
sw &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $16,0($2)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
#v[k]=register $16</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
sw &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $15,4($2)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
#v[k+1}=register $15(temp)</font></strong></p>     
     
<p><strong><font color="#000000" size="4"><a name="3.">3.</a>&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27">    
 在程序调用过程中保<a href="#3.">护</a>寄存器。</font></strong></p>   
   
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp; 因为在程序中我们要改变$2,$15,$16的值,所以我们必须要保存它们的原值。寄存器$29是堆栈指针,我们需要开辟3*4或者是12个</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp; 字节来保<a href="#3.">护</a>它们。</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
addi &nbsp;&nbsp; $29,$29,-12</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp; 现在我们可以保存旧值:</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
sw &nbsp;&nbsp;&nbsp;&nbsp; $2,0($29)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #save $2 on stack      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font> </strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
sw &nbsp;&nbsp;&nbsp;&nbsp; $15,4($29)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #save $15 on stack</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
sw &nbsp;&nbsp;&nbsp;&nbsp; $16,8($29)      
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #save $16 on stack</font></strong></p>     
     
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;      
最后,我们可以从堆栈中取出数据,并且使堆栈调整到原状。由于<em>swap</em>不再调用其他的子程序,所以我们跳过保存并把返回地址存在&nbsp;  
$31.在程序的末尾我们加上一条<em>jr </em>$31返回。</font></strong></p>    
    
<p><strong><font color="#000000" size="4">&nbsp;&nbsp;<img border="0" src="../images/sub.gif" width="26" height="27">   
总结该程序,我们得到如下完整的一章表:</font></strong></p>    
    
<table border="1" width="100%" height="447" bordercolordark="#CC9966" bordercolorlight="#FFCC66">    
  <tr>    
    <td width="100%" colspan="2" height="16" bgcolor="#008000"><p align="center"><strong><font color="#000000" size="4">Saving      
    registers</font></strong></td>     
  </tr>     
  <tr>     
    <td width="50%" height="121"><strong><font color="#000000" size="4">swap &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    addi&nbsp; $29,$29,-12</font> </strong><p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    sw &nbsp;&nbsp;&nbsp; $2,0($29)</font> </strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw      
    &nbsp;&nbsp;&nbsp; $2,4($29)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw      
    &nbsp;&nbsp;&nbsp; $2,8($29)</font></strong></td>     
    <td width="50%" height="121"><strong><font color="#000000" size="4"># make room on stack for 3*registers</font></strong><p><strong><font color="#000000" size="4">#      
    save $2 on stack</font> </strong></p>     
    <p><strong><font color="#000000" size="4"># save $15 on stack</font> </strong></p>     
    <p><strong><font color="#000000" size="4"># save $16 on stack</font></strong></td>     
  </tr>     
  <tr>     
    <td width="100%" colspan="2" height="16" bgcolor="#008000"><p align="center"><strong><font color="#000000" size="4">Procedure      
    body</font></strong></td>     
  </tr>     
  <tr>     
    <td width="50%" height="191"><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    muli &nbsp; $2,$5,4</font></strong><p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    add &nbsp;&nbsp; $2,$4,$2</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lw      
    &nbsp;&nbsp;&nbsp; $15,0($2)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lw      
    &nbsp;&nbsp;&nbsp; $16,4($2)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw      
    &nbsp;&nbsp;&nbsp; $16,0($2)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw      
    &nbsp;&nbsp;&nbsp; $15,4($2)&nbsp;&nbsp;</font> </strong></td>     
    <td width="50%" height="191"><strong><font color="#000000" size="4"># reg$2=k*4</font></strong><p><strong><font color="#000000" size="4"># reg$2=v+(k*4)</font></strong></p>     
    <p><strong><font color="#000000" size="4"># reg$2 has the address of v[k]</font></strong></p>     
    <p><strong><font color="#000000" size="4"># reg$15 (temp) =v[k]</font></strong></p>     
    <p><strong><font color="#000000" size="4"># reg$16=v[k+1]</font></strong></p>     
    <p><strong><font color="#000000" size="4"># refers to next element of v</font></strong></p>     
    <p><strong><font color="#000000" size="4"># v[k+1]=reg $15 (temp)</font> </strong></td>     
  </tr>     
  <tr>     
    <td width="100%" height="7" colspan="2" bgcolor="#008000"><p align="center"><strong><font color="#000000" size="4">Restoring      
    registers</font></strong></td>     
  </tr>     
  <tr>     
    <td width="50%" height="16"><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    lw &nbsp;&nbsp;&nbsp; $2,0($29)</font></strong><p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    lw &nbsp;&nbsp;&nbsp; $15,4($29)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lw      
    &nbsp;&nbsp;&nbsp; $16,8($29)</font></strong></p>     
    <p><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addi      
    &nbsp; $29,$29,12</font></strong></td>     
    <td width="50%" height="16"><strong><font color="#000000" size="4"># restore $2 from stack</font> </strong><p><strong><font color="#000000" size="4"># restore      
    $15 from stack</font> </strong></p>     
    <p><strong><font color="#000000" size="4"># restore $16 from stack</font> </strong></p>     
    <p><strong><font color="#000000" size="4"># restore stack pointer</font></strong></td>     
  </tr>     
  <tr>     
    <td width="100%" height="16" colspan="2" bgcolor="#008000"><p align="center"><strong><font color="#000000" size="4">Procedure      
    return</font></strong></td>     
  </tr>     
  <tr>     
    <td width="50%" height="16"><strong><font color="#000000" size="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      
    jr &nbsp;&nbsp;&nbsp; $31</font></strong></td>     
    <td width="50%" height="16"><strong><font color="#000000" size="4"># return to calling routine</font> </strong></td>     
  </tr>     
</table>     
     
<p><strong><font color="#000000">&nbsp;&nbsp;</font> </strong></p>   
   
<p><strong><font color="#000000">&nbsp;&nbsp;</font> </strong></p>   
   
<p><font color="#000000"> </font></p>
</body>
</html>

⌨️ 快捷键说明

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