MIPS ASM 作业 - 数组和 while 循环
首先,让我这么说...我不想让任何人给我答案...我希望有人指出正确的方向!
我在这个程序中有一个整数数组......例如:
numbers:
.word 17
.word -50
.word 1
.word -999
我有一个 while 循环遍历它们并将每个整数打印在新行上。效果很好。 (-999是终止号码,不包含在打印输出中)
我还需要以相反的顺序打印它们。我知道我可以使用一个循环来计算元素的数量,然后再进行另一个循环,该循环从最后一个地址开始并向后移动......但这似乎效率低下。
有没有办法在不先执行循环的情况下找到数组中最后一个元素的地址?如果没有,我可以按照我提到的方式做到这一点,只是想确保我在程序中尽可能高效。
提前致谢!
First, let me say this...I don't want anyone to just give me an answer...I'd like to be pointed in the right direction!
I have an array of integers in this program....for instance:
numbers:
.word 17
.word -50
.word 1
.word -999
I have a while loop that runs through them and prints each one on a new line. That works fine. (-999 is the terminating number, and is not included in the printout)
I also need to print them in the reverse order. I know that I could use a loop to count the number of elements and then have another loop that starts at the last address and goes backwards....this seems to be inefficient, though.
Is there some way to find the address of the last element in an array without doing a loop first? If not, I can do it this way I mentioned, just want to make sure I'm being as efficient as I can in the program.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您被允许执行任何您想要的汇编技巧,您可以执行类似的操作
并使用类似伪代码的代码
换句话说,放置一个标签和数组的末尾并向后走,直到到达数组的开头,使用指针比较而不是寻找终止符(-999)。
或者,为了避免过多更改代码,
向后走直到找到 -999 怎么样?
If you are allowed to do any assembly tricks you want, you could do something like
and use code like this pseudo code
In other words, put a label and the end of the array and walk backwards until you hit the beginning of the array, using a pointer comparison instead of looking for the terminator (-999).
Or, to avoid changing you code too much, how about
and walk backward until you find the -999?