MIPS循环遍历数组 [英] MIPS looping through an array

查看:739
本文介绍了MIPS循环遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MIPS的新手



我正在尝试开发一个程序,该程序从十字数组中获取输入,将其递增2,存储结果返回寄存器$ v0。哪个将保持递增的值。当程序从数组中遇到值0时程序应该停止。



数组的前9个元素将从1到9开始,第十个元素是一个0.



另一个寄存器也将用于保存所有递增值的总和。



我也不能使用任何伪代码。



这是我到目前为止的代码



。数据

数组:

.word 1

.word 2

.word 3

.word 4

.word 5

.word 6

.word 7

.word 8

.word 9

.word 0

.text

lui $ a0,0x1001 #load address 0x1001000将数组放入寄存器$ a0

jal increment #call程序

增量:

lw $ a2,0($ a0)#load从寄存器$ a0到$ a2的单词

beq $ a2,$ 0,否则#if word加载为0 goto else

addi $ a2,$ a2,2 #add 2
sw $ a2,0($ a0)#store结果从$ a2回到$ a0

addi $ a0,$ a0,1 #add 1 to $ a0 to point到数组中的下一个值

添加$ v0,$ a2,$ 0 #add从$ a2到$ v0的结果

addi $ v0,$ 0,1

系统调用

jr $ a0

j增量#jump返回程序

else:

添加$ v1,$ a0,$ 0 #once找到0,将最终金额从$ a0存入$ v1

addi $ v1,$ 0,1 $

系统调用



我觉得逻辑是正确的但代码的语义不足



代码可能看起来有点乱,另外一个我也不确定的是索引,我应该使用另一个寄存器来保存数组的0索引?



如果你无法修复它,那么你可以给我一些指示吗?谢谢

I'm a novice to MIPS

I'm trying to develop a program which takes an input from a ten word array, increments it by two, stores the result back into a register $v0. Which will hold the incremented value. The program should stop when it encounters a value of 0 from the array.

The first 9 elements of the array will start from 1 up to 9, with the tenth being a 0.

Another register will also be used to hold the sum of all incremented values.

I can also not make use of any pseudo code.

This is the code i have so far

.data
array:
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
.text
lui $a0, 0x1001 #load address 0x1001000 of the array into register $a0
jal increment #call the procedure
increment:
lw $a2, 0($a0) #load word from register $a0 into $a2
beq $a2, $zero, else #if word loaded is 0 goto else
addi $a2, $a2, 2 #add two
sw $a2, 0($a0) #store result from $a2 back into $a0
addi $a0, $a0, 1 #add 1 to $a0 to point to the next value in the array
add $v0, $a2, $zero #add the result from $a2 into $v0
addi $v0, $zero, 1
syscall
jr $a0
j increment #jump back to the procedure
else:
add $v1, $a0, $zero #once the 0 is found, store the final sum from $a0 into $v1
addi $v1, $zero, 1
syscall

I feel like the logic is correct but the semantics of the code are lacking

The code may seem like a bit of a mess, another area I'm also unsure about is the index, i'm supposed to use another register to hold the 0 based index of the array?

If you can't fix it, then could you give me some pointers? Thanks

推荐答案

v0。哪个将保持递增的值。当程序从数组中遇到值0时程序应该停止。



数组的前9个元素将从1到9开始,第十个元素是一个0.



另一个寄存器也将用于保存所有递增值的总和。



我也不能使用任何伪代码。



这是我到目前为止的代码



。数据

数组:

.word 1

.word 2

.word 3

.word 4

.word 5

.word 6

.word 7

.word 8

.word 9

.word 0

.text

lui
v0. Which will hold the incremented value. The program should stop when it encounters a value of 0 from the array.

The first 9 elements of the array will start from 1 up to 9, with the tenth being a 0.

Another register will also be used to hold the sum of all incremented values.

I can also not make use of any pseudo code.

This is the code i have so far

.data
array:
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
.text
lui


a0,0x1001 #load地址0x1001000数组到寄存器
a0, 0x1001 #load address 0x1001000 of the array into register


a0

jal increment #call程序

增量:

lw
a0
jal increment #call the procedure
increment:
lw


这篇关于MIPS循环遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆