MIPS - getting array values

房东的猫 提交于 2019-12-22 12:29:31

问题


Ok, so I have an array stored in memory and I want to essentially create a variable "i" and get the array value at index i. How do I do this in MIPS? Thanks in advance! Here is my code.

.data
array: .word 0:100

.text
li $t0, 5 #this is my representation of "i"

la $t2, array

lw $t1, i($t2) #this is where i am messed up.

回答1:


You should add the base and index together, and remember to scale by 4 for the word size. Something like this:

li $t0, 5          # this is my representation of "i"
la $t2, array
sll $t1, $t0, 2    # scale by 4
addu $t1, $t1, $t2 # add offset and base together
lw $t1, ($t1)      # fetch the data

You can only use the i($t2) style if i is an immediate constant that fits into 16 bits.




回答2:


To add with the previous answer, if you array happens to be located in the first 64k of RAM, you can also do this :

lw $t1, array($t1)


来源:https://stackoverflow.com/questions/28491018/mips-getting-array-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!