📄 6.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="../chapter4/arctile.jpg" bgcolor="#FFFFFF" text="#000000" link="#3399FF" vlink="#666666" alink="#FF9900">
<p align="center"><font color="#000000"><strong> </strong><b><font size="6">3.6 </font></b><strong>
<big><big><big>数组与指针</big></big></big>
</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>
<a href="5.htm"><img border="0" src="../images/prev.gif" width="60" height="45"></a>
<a href="../chapter4/chapter4.htm"><img border="0" src="../images/forw.gif" width="60" height="45"></a></font></p>
<p><strong><font color="#000000"> 对程序员来说,最重要的莫过于掌握指针的用法了。</font></strong></p>
<p><strong><font color="#000000">让我们先来看下面两个程序:</font></strong></p>
<p><strong><font color="#000000"> <img border="0" src="../images/sub.gif" width="26" height="27"> <a href="#程序1">程序1</a>
<img border="0" src="../images/sub.gif" width="26" height="27">
<a href="#程序2">程序2</a></font></strong></p>
<p><strong><font color="#000000">clear1(int array[], int size)
clear2(int *array[0]; int size)</font></strong></p>
<p><strong><font color="#000000">{
{</font></strong></p>
<p><strong><font color="#000000"> int i;
int *p; </font> </strong></p>
<p><strong><font color="#000000"> for (i=0;i<size;i=i+1)
for(p=&array[0];p<&array[size];p=p+1)</font></strong></p>
<p><strong><font color="#000000"> array[i]=0;
*p=0;</font></strong></p>
<p><strong><font color="#000000">}
}</font></strong></p>
<p><font color="#000000"> </font></p>
<p><strong><font color="#000000">先分析<a name="程序1">程序1</a>,根据我们的约定,一般用$4,$5,$6,$7来保存子程序的参数。所以我们假设两个参数<em>array</em>和<em>size</em>被保存在寄存器$4,$5中,<em>i</em>被保存在寄存器$2中。</font></strong></p>
<p><strong><font color="#000000">1. <img border="0" src="../images/sub.gif" width="26" height="27">
初始化<em>i</em>,<em>for</em>
循环的第一部分</font></strong></p>
<p><strong><font color="#000000"> move $2,$0
# i=0 (register $0=0)</font></strong></p>
<p><strong><font color="#000000">2. <img border="0" src="../images/sub.gif" width="26" height="27">
为将<em>array[i]</em>赋为0,我们要做三步工作</font></strong></p>
<p><strong><font color="#000000"> loop1 muli
$14,$2,4
# $14=i*4</font> </strong></p>
<p><strong><font color="#000000">
add $3,#4,$14
# $3=address of array[i]</font></strong></p>
<p><strong><font color="#000000">
sw $0,0($3)
# array[i]=0</font></strong></p>
<p><strong><font color="#000000">3. <img border="0" src="../images/sub.gif" width="26" height="27">
将<em>i</em>自加1,即<em>i=i+1</em>;</font></strong></p>
<p><strong><font color="#000000"> addi $2,$2,1
# i=i+1</font></strong></p>
<p><strong><font color="#000000">4. <img border="0" src="../images/sub.gif" width="26" height="27">
程序判断<em>i</em>是否还是小于<em>size</em>,如果是的话,再开始下一轮循环。</font></strong></p>
<p><strong><font color="#000000"> slt $6,$2,$5</font></strong></p>
<p><strong><font color="#000000"> bne $6,$0,loop1</font></strong></p>
<p><strong><font color="#000000">总结如下:</font> </strong></p>
<p><strong><font color="#000000">
move $2,$0
# i=0</font></strong></p>
<p><strong><font color="#000000"> loop1 :
muli $14,$2,4
# $14=i*4</font></strong></p>
<p><strong><font color="#000000">
add $3,$4,$14
# $3=address of array[i]</font></strong></p>
<p><strong><font color="#000000">
sw $0,0($3)
# array[i]=0</font> </strong></p>
<p><strong><font color="#000000">
addi $2,$2,1
# i=i+1</font></strong></p>
<p><strong><font color="#000000">
slt $6,$2,$5
# $6=1(i<size)</font></strong></p>
<p><strong><font color="#000000">
bne $6,$0,loop1
# if(i<size) go to loop1</font></strong></p>
<p><font color="#000000"> </font></p>
<p><strong><font color="#000000">分析<a name="程序2">程序2</a>:</font></strong></p>
<p><strong><font color="#000000">第一步也是用$4,$5保存两个参数<em>array</em>
和<em>size</em>,把<em>p</em>分派到寄存器$2中。</font></strong></p>
<p><strong><font color="#000000">1. <img border="0" src="../images/sub.gif" width="26" height="27"> 将指针p指向数组的第一个元素<em>array[0]</em></font></strong></p>
<p><strong><font color="#000000"> move
$2,$4
# p=address of array[0]</font></strong></p>
<p><strong><font color="#000000">2. <img border="0" src="../images/sub.gif" width="26" height="27"> 将指针<em>p</em>赋为<em>0 </em> </font>
</strong></p>
<p><strong><font color="#000000"> loop2
sw $0,0($2)
# Memory[0]=0 </font> </strong></p>
<p><strong><font color="#000000">3. <img border="0" src="../images/sub.gif" width="26" height="27"> 将指针指向下一个字</font></strong></p>
<p><strong><font color="#000000"> addi
$2,$2,4
# p=p+4</font></strong></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -