使用数组的 Mips x32
我正在为我的一项作业编写此代码,我需要一个大小为 128 的数组,以便
drops: .space 128
我可以在 Drops 中加载该特定位置并为其存储数字 0-8...
例如。 ...假设随机数是 32,我正在进行循环的第一次迭代 它将在数组的第 32 个位置存储 0 如果我在数组的第二次迭代中...它将在随机数中存储 1,例如 92..spot
这是我的代码:
我首先将数组中的所有内容都设置为 -1,以便我可以测试是否有某些内容 正如你所见,
storeArray:
la $t6, drops
la $t1, 0 #counter
loopStoreRandom:
move $a0, $s5 # send x
jal getDrop
move $t2, $v0 #t2 has a random number
add $t6, $t6, $t2 #random + the whole ---wrong
lb $t3, ($t6)
bne $t3, -1, loopStoreRandom
addi $t1, $t1, 1
beq $t1, 128, exit
j loopStoreRandom
我希望有一些东西我可以像 sb $t1, $t2($t6)
但我不能
im writing this code for one of my assignments and i need to have an array of size 128 which i do by
drops: .space 128
so that i can load that specific spot in drops and store a number 0-8 to it.....
for example.... say the random number was 32 and i was on the first iteration of the loop
it would store 0 in the 32nd spot of the array
if i was in the 2 iteration of the array... it would store 1 in the random number eg 92..spot
here is my code:
I first made everything in my array -1 so that i can test to see if something was in it...
storeArray:
la $t6, drops
la $t1, 0 #counter
loopStoreRandom:
move $a0, $s5 # send x
jal getDrop
move $t2, $v0 #t2 has a random number
add $t6, $t6, $t2 #random + the whole ---wrong
lb $t3, ($t6)
bne $t3, -1, loopStoreRandom
addi $t1, $t1, 1
beq $t1, 128, exit
j loopStoreRandom
so as you see i wish there was something that i could to just be like sb $t1, $t2($t6)
but i can't
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会在每个循环中恢复 $t6 ,因此指针在第一个循环后会关闭。
移动
到循环内部。
Your're not restoring $t6 each loop, so the pointer becomes off after the first loop.
Move
inside the loop.
假设 v0 从 getDrop 返回,则移动指令将在 getDrop 执行之前执行,因为它将作为 jal 延迟槽的一部分执行。所以 T2 不会包含 getDrop 的返回值。
应该是:
Assuming v0 is returned from getDrop, the move instruction will be executed BEORE getDrop is executed as it will execute as part of jal's delay slot. So T2 is not going to contain the returned value of getDrop.
should be: