在不使用 SLT 的情况下在 MIPS I 中编写循环
我得到一个数组,我知道它的长度充满了 20 个 4 字节字(大概是整数)。不使用 SLT,而是使用 BEQ/BNE,我将编写一个循环来对所有数组元素求和。我也不使用像“i”这样的变量来迭代数组,而只使用字节偏移量。
我正在尝试加载最后一个元素的字节偏移量 + 4,以判断当前字节偏移量是否等于该值(表明其超出范围),并另外加载该元素的数据并将其求和。我完全迷失了。这是我的尝试:(
数组 A 的基地址已加载到 $s0 中)
addi $s1, $s1, $zero #sum = 0
lw $t1, 84($s0) #trying to load the byte offset of the last element (a[20] + 4)
sll $t2, $s1, 2 #byte offset of the current element
LOOP: beq $t2, $t1, EXIT #go to exit if they're equal, otherwise execute the rest of the code
#load the element of A into $t4 (no idea how to implement)
add $s1, $s1, $t4 #add it to the sum (sum += current element)
sll $t2, $t2, 2 #increment the byte offset
j LOOP
EXIT : #code not indicated here
根据我的作业,研究引导我找到了我不应该使用的指令(SLT、LB、LA 等)。我非常不确定如何继续前进。
I'm given an array that I know the length of that is full of 20 4 byte words (presumably integers). Without using SLT, and instead using BEQ/BNE, I'm to write a loop to sum all of the array elements. I'm also to not use a variable like "i" to iterate through the array, only using byte offsets instead.
I am trying to load the byte offset of the last element + 4, to branch on whether or not the current byte offset equals that (indicating its out of bounds), and additionally load the data of the element and summate it. I'm completely lost on it. This was my attempt at it:
(Base address of array A is already loaded into $s0)
addi $s1, $s1, $zero #sum = 0
lw $t1, 84($s0) #trying to load the byte offset of the last element (a[20] + 4)
sll $t2, $s1, 2 #byte offset of the current element
LOOP: beq $t2, $t1, EXIT #go to exit if they're equal, otherwise execute the rest of the code
#load the element of A into $t4 (no idea how to implement)
add $s1, $s1, $t4 #add it to the sum (sum += current element)
sll $t2, $t2, 2 #increment the byte offset
j LOOP
EXIT : #code not indicated here
Research leads me to instructions I'm not supposed to use (SLT, LB, LA, etc.), as per my assignment. I'm very unsure how to move forward from this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论