用偏移保存单词

时间:2015-04-26 21:03:48

标签: assembly mips

我试图在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:\Users\robert\Downloads\Lab7 line 37 column 8: "(": operand is of incorrect type

理想情况下,它应该将内存地址存储在一个数组(0-25)中,其中存储一个值,该值跟踪从语句中加载的字母的频率:

      lw    $t3, letter #save letter

1 个答案:

答案 0 :(得分:1)

在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)