📄 6.htm
字号:
<p><strong><font color="#000000">4. <img border="0" src="../images/sub.gif" width="26" height="27"> 得到数组的最后一个元素<em>array[size]</em>的地址值:</font></strong></p>
<p><strong><font color="#000000"> muli
$14,$5,4
# $14=size*4
</font>
</strong></p>
<p><strong><font color="#000000"> add
$3,$4,$14
# $3=address of array[size] </font> </strong></p>
<p><strong><font color="#000000">5. <img border="0" src="../images/sub.gif" width="26" height="27"> 判断并跳转</font></strong></p>
<p><strong><font color="#000000"> slt
$6,$2,$3
# $6=(p<array[size])</font></strong></p>
<p><strong><font color="#000000"> bne
$6,$0,loop2
# if(p<array[size]) go to loop2</font></strong></p>
<p><strong><font color="#000000"><img border="0" src="../images/sub.gif" width="26" height="27">总结如下:</font></strong></p>
<p><strong><font color="#000000">
move
$2,$4
# p=address of array[0]</font></strong></p>
<p><strong><font color="#000000"> loop2 :
sw $0,0($2)
# Memory[p]=0</font></strong></p>
<p><strong><font color="#000000">
addi
$2,$2,4
# p=p+4</font></strong></p>
<p><strong><font color="#000000">
muli
$14,$5,4
# $14=size*4</font></strong></p>
<p><strong><font color="#000000"> add
$3,$4,$14
# $3=address of array[size]</font></strong></p>
<p><strong><font color="#000000">
slt
$6,$2,$3
# $6=(p<array[size])</font></strong></p>
<p><strong><font color="#000000">
bne
$6,$0,loop2
# if (p<array[size]) go to loop2</font></strong></p>
<p><strong><font color="#000000">我们注意到在每一个循环中,数组的末地址是不变的,所以程序二又可以写成下面的代码:</font></strong></p>
<p><strong><font color="#000000"> move
$2,$4
# p=address of array[0]</font></strong></p>
<p><strong><font color="#000000">
muli
$14,$5,4
# $14=size*4</font></strong></p>
<p><strong><font color="#000000">
add
$3,$4,$14
# $3=address of array[size]</font></strong></p>
<p><strong><font color="#000000"> loop2:
sw $0,0($2)
# Memory[p]=0 </font></strong></p>
<p><strong><font color="#000000">
addi
$2,$2,4
# p=p+4 </font> </strong></p>
<p><strong><font color="#000000">
slt
$6,$2,$3
# $6=(p<array[size])</font></strong></p>
<p><strong><font color="#000000">
bne
$6,$0,loop2
# if (p<array[size]) go to loop2</font></strong></p>
<p><strong><font color="#000000">让我们来比较两个程序的代码:</font></strong></p>
<p><strong><font color="#000000"> </font> </strong></p>
<p><strong><font color="#000000">
move $2,$0
move $2,$4
</font>
</strong></p>
<p><font color="#000000"><strong>loop1</strong></font>:<font color="#000000"><strong>
muli $14,$2,4
muli $14,$5,4
</strong></font>
</p>
<p><strong><font color="#000000">
add $3,$4,$14
add $3,$4,$14
</font>
</strong></p>
<p><strong><font color="#000000">
addi $2,$2,1
loop2: sw $0,0($2)
</font>
</strong></p>
<p><strong><font color="#000000">
sw $0,0($3)
addi $2,$2,4</font></strong></p>
<p><font color="#000000"><strong> </strong></font>
<font color="#000000"><strong>slt $6,$2,$5
slt $6,$2,$3</strong></font></p>
<p><strong><font color="#000000"> bne $6,$0,loop1
bne $6,$0,loop2</font></strong></p>
<p><strong><font color="#000000">现在我们从对两个程序的分析中,发现指针给我们的程序运行带来的不可多得的优化,使程序的循环变得更为简洁,所以在一般的现代的编译器中会将程序1优化成程序2。
</font> </strong></p>
<p><font color="#000000"> </font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -