C 到 MIPS 的转换。数组加载字偏移量?

标签 c assembly mips

我有一个 C 代码需要 MIPS 方面的帮助。 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”来抵消加载字和存储字。因为据我所知加载词只使用静态值。

最佳答案

数组按内存中的最低基地址排列,并通过 4 字节的偏移索引到下一个元素。[图片来自 Harris D.M.、Harris S.L. - 数字设计和计算机体系结构,第 2 版 - 2012]

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:

关于C 到 MIPS 的转换。数组加载字偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357326/

相关文章:

c - 如何使用 pthreads 访问本地存储

c - 井字棋 - 转弯 C

c++ - 使用内联汇编修改数组元素

assembly - mips 类型 R、I 和 J、RS、RT 和 RD 字段长度

gcc - 基于 MIPS 的 codesourcery 工具链的编译参数?

c - 为什么 Isdigit 函数不能正常工作?

c - 在c中提取存档文件

c++ - assembly 打印 float

assembly - 分析没有源代码的编译代码?

mips - "trap"在 MIPS 术语中是什么意思?