📄 5.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>
<big><big><big>程序范例</big></big></big></b><strong>
</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> </strong><a href="4.htm"><img border="0" src="../images/prev.gif" width="60" height="45"></a>
<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">
swap(int v[],int k)</font></strong></p>
<p><strong><font color="#000000" size="4"> {</font></strong></p>
<p><strong><font color="#000000" size="4">
int temp; </font> </strong></p>
<p><strong><font color="#000000" size="4">
temp=v[k];</font> </strong></p>
<p><strong><font color="#000000" size="4">
v[k]=v[k+1];</font></strong></p>
<p><strong><font color="#000000" size="4">
v[k+1]=temp;</font></strong></p>
<p><strong><font color="#000000" size="4"> }</font></strong></p>
<p><strong><font color="#000000" size="4">这是一个在C语言中常见的交换过程。将一个C程序转化为汇编语言一般是三步曲:</font></strong></p>
<p><strong><font color="#000000" size="4"> 1. <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"> 2. <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"> 3. <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">. <img border="0" src="../images/sub.gif" width="26" height="27">
寄存器的分配。</font></strong></p>
<p><strong><font color="#000000" size="4"> 根据我们的通常约定,一般用寄存器$4,$5,$6,$7来保存程序调用中的参数。在该程序中,只有两个参数v和k,所以我们把v和k分别放</font></strong></p>
<p><strong><font color="#000000" size="4"> 了寄存器$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">. <img border="0" src="../images/sub.gif" width="26" height="27">
由于存储器是以字节编址的,所以数K在内存中的地址是4的倍数。理解这一点对我们理解
汇编语言很重要。</font></strong></p>
<p><strong><font color="#000000" size="4"> 该程序的程序主体代码应该是:</font></strong></p>
<p><strong><font color="#000000" size="4">
muli $2,$5,4
#register $2=k*4</font> </strong></p>
<p><strong><font color="#000000" size="4">
add $2,$4,$2
#register $2=v+(k*4)</font></strong></p>
<p><strong><font color="#000000" size="4">
lw $15,0($2)
#register $2 has the address of v[k]</font></strong></p>
<p><strong><font color="#000000" size="4">
lw $16,4($2)
#reg $15(temp)=v[k]</font></strong></p>
<p><strong><font color="#000000" size="4">
sw $16,0($2)
#v[k]=register $16</font></strong></p>
<p><strong><font color="#000000" size="4">
sw $15,4($2)
#v[k+1}=register $15(temp)</font></strong></p>
<p><strong><font color="#000000" size="4"><a name="3.">3.</a> <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"> 因为在程序中我们要改变$2,$15,$16的值,所以我们必须要保存它们的原值。寄存器$29是堆栈指针,我们需要开辟3*4或者是12个</font></strong></p>
<p><strong><font color="#000000" size="4"> 字节来保<a href="#3.">护</a>它们。</font></strong></p>
<p><strong><font color="#000000" size="4">
addi $29,$29,-12</font></strong></p>
<p><strong><font color="#000000" size="4"> 现在我们可以保存旧值:</font></strong></p>
<p><strong><font color="#000000" size="4">
sw $2,0($29)
#save $2 on stack
</font> </strong></p>
<p><strong><font color="#000000" size="4">
sw $15,4($29)
#save $15 on stack</font></strong></p>
<p><strong><font color="#000000" size="4">
sw $16,8($29)
#save $16 on stack</font></strong></p>
<p><strong><font color="#000000" size="4">
最后,我们可以从堆栈中取出数据,并且使堆栈调整到原状。由于<em>swap</em>不再调用其他的子程序,所以我们跳过保存并把返回地址存在
$31.在程序的末尾我们加上一条<em>jr </em>$31返回。</font></strong></p>
<p><strong><font color="#000000" size="4"> <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
addi $29,$29,-12</font> </strong><p><strong><font color="#000000" size="4">
sw $2,0($29)</font> </strong></p>
<p><strong><font color="#000000" size="4"> sw
$2,4($29)</font></strong></p>
<p><strong><font color="#000000" size="4"> sw
$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">
muli $2,$5,4</font></strong><p><strong><font color="#000000" size="4">
add $2,$4,$2</font></strong></p>
<p><strong><font color="#000000" size="4"> lw
$15,0($2)</font></strong></p>
<p><strong><font color="#000000" size="4"> lw
$16,4($2)</font></strong></p>
<p><strong><font color="#000000" size="4"> sw
$16,0($2)</font></strong></p>
<p><strong><font color="#000000" size="4"> sw
$15,4($2) </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">
lw $2,0($29)</font></strong><p><strong><font color="#000000" size="4">
lw $15,4($29)</font></strong></p>
<p><strong><font color="#000000" size="4"> lw
$16,8($29)</font></strong></p>
<p><strong><font color="#000000" size="4"> addi
$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">
jr $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"> </font> </strong></p>
<p><strong><font color="#000000"> </font> </strong></p>
<p><font color="#000000"> </font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -