我正在尝试在MIPS中使用sw
来访问数组中的一个点。练习是获取一个字符串,然后在字符串中每次出现字母时递增数字列表。
.data
pos: .word 0
letter: .word 65
index: .word 0
buffer: .space 1024
array: .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
prompt: .asciiz "Enter a string: "
pdng1: .asciiz ": " #used to make our output look nice
pdng2: .asciiz ", "
.text
...
lw $t0, pos #position in the string
la $t1, array #holds the array itself
la $t2, buffer #holds the buffe
sw $t3, letter
loopcp: bgt $t3, 90, looplw #if t is greater than 90, then it is lower case
lw $t3, letter #save letter
subi $t4, $t3, 65 #$t4 now holds the index in the array that we want to access
#### ERROR ON LINE BELOW ####
sw $t4($t1), index
addi index, index, 1 #take what's at the offset we calculated and increment it
addi $t0, $t0, 1 #increment the position in the string
blt $t0, 256, loopcp
我尝试使用 addi
直接转到数组中的地址,但这也没有用...... 我一直在查找文档,但我找不到任何对我正在做的事情有帮助的东西。
编辑:我收到一个错误,内容如下:
Error in C:UsersrobertDownloadsLab7 line 37 column 8: "(": operand is of incorrect type
理想情况下,它应该将内存地址存储在数组 (0-25) 中,其中存储了一个值,该值跟踪从语句加载的字母的频率:
lw $t3, letter #save letter
在 MIPS 中寻址到数组:
假设您在 $s0
中具有数组开头的地址,并且您有要在 $s1
中访问的索引。数组元素的大小为 4。要访问array[index]
,您需要:
sll $t0, $s1, 2 #byte-addressing, needs index*sizeof(element)
add $t0, $t0, $s0 #calculate array+index*sizeof(element)
lw/sw $s2, 0($t0) #load to- or store from register $s2
您显然需要额外的临时寄存器$t0
。
要在 array[index]
处递增该值,您可以这样做(假设$t0
持有&array[index]
,计算如上所示:
lw $t1, 0($t0)
addi $t1, $t1, 1
sw $t1, 0($t0)