【问题标题】:C to MIPS conversion. Array load word offset?C 到 MIPS 的转换。数组加载字偏移?
【发布时间】:2017-02-21 01:53:56
【问题描述】:

我有一个需要 MIPS 帮助的 C 代码。 C代码如下。

for (i=0; i<100; i++){
   a[i] = b[i] + c[i] - d[i];
}

我已将其转换为 MIPS,但不知道将什么放入加载字的偏移量中。

addi $t0, $zero, 0                #i = 0

for_loop:
   bgt $t0, 100, for_loop_done     #i <100
   addi $t0, $t0, 1               #i++ or i = i+1


   lw $t4, __($s0)              # load a in t4
   lw $t1, __($s1)              # load b in t1
   lw $t2, __($s2)              # load c in t2
   add $t4, $t2, $t1                 # add b with c and store in a
   lw $t3, __($s3)              # load d in t3
   sub $t4, $t3, $t4                 # sub contents of a from d
   sw $t4, __($s0)              # store contents of t4 into a

   j for_loop                     # go to start of loop

for_loop_done:

我们假设 a,b,c,d 分别在 s0,s1,s2,s3,s4 中。现在代码需要的是我们如何使用 c 代码中不断变化的“i”来抵消加载字和存储字。因为据我所知 load word 只使用静态值。

【问题讨论】:

  • 您可以在每个循环中将 s0,s1,s2...增加一个数组项的大小。或者复制它们并增加它们,这样你就不会丢失原始起始地址。这适用于大多数指令集,包括这个。

标签: c assembly mips


【解决方案1】:

数组以内存中最低的基地址排列,并以 4 个字节的偏移量索引到下一个元素。[图片来自 Harris D. M., Harris S. L. - Digital Design and Computer Architecture, 2nd Edition - 2012]

 for_loop:
         bgt $t0, 100, for_loop_done     #i <100
         addi $t0, $t0, 1               #i++ or i = i+1


         lw $t4, 0($s0)              # load a in t4
         lw $t1, 4($s1)              # load b in t1
         lw $t2, 8($s2)              # load c in t2
         add $t4, $t2, $t1            # add b with c and store in a
         lw $t3, 12($s3)              # load d in t3
         sub $t4, $t3, $t4            # sub contents of a from d
         sw $t4, 0($s0)              # store contents of t4 into a

         j for_loop                   # go to start of loop

for_loop_done:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多