【问题标题】:Converting double for loops from C to Mips and Addition in Arrays将双 for 循环从 C 转换为 Mips 和数组中的加法
【发布时间】:2015-10-18 20:34:12
【问题描述】:

我正在研究关于课程计算机体系结构的问题,我应该在其中将一些 C 代码转换为 Mips。

 int A[52], B[52];
    2 for (j = 1; j < 52 ; j++)
    3 for (i = j; i < 52; i++) {
    4 B[i] = B[i-1] + A[j] ;
    5 }

我的翻译如下。我认为 addi 等于 li 但我不知道 li 的真正含义,所以我使用 addi 让我添加一个常量。假设数组 A 保存在 $s0 中,B 保存在 S$1

   addi $t1, $0,  1   //setting j to 1
          addi   $t2 , $t1 , 0  //setting i (which is always equal to j) 
        addi     $t6, $t1 , -1  //this will be used for writing B[i-1]
            addi $t3,  $0, 50  //making a temp value 50 to end the loop.
        Loop:
        beq       $t3, $t1,end     //when j becomes 50 end the loop.
            beq  $t3, $t2,end     //when i becomes 50 end the loop.
        lw    $t4 , $s0          //loading A 
        lw  $t5, $s1              //loading B
         add 4$t1($t5),  4$t6($St5),4$t2($t4) //This is the part I think I am doing it //wrong.I want to write 4($t5)for first element 8 for second an so on but I dont //know how to do it.Is this a legit way?

sv  $t5, $s1               //storing B
addi   $t1, $t1, 1         //increment j by one.
addi   $t2,$t2,1          //increment i by one 
j            loop:
end

我的问题如下。是否有任何宇宙我可以添加这样的数组,如果没有我怎么能。另外我无法检查我的答案,因为这本书没有解决方案。我做其他操作的方式是正确的?

【问题讨论】:

    标签: arrays assembly dynamic mips


    【解决方案1】:

    我会发布答案,因为我认为我解决了问题,如果有人和我有同样的问题,这会很有帮助。

    loop
    lw $t4 ,4($s1)  //First element
    lw $t5, 0($s0)   //Zeroth element
    add $t6, $tf $t4  
    sw $t6, 4($s1)   //store
    addi $s0 $s0  4  //Increment by 1
    addi $s1 $s1 4 //Increment by 1 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2021-01-09
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多