嵌套for循环中的数组,MIPS程序集

时间:2017-11-01 07:17:07

标签: arrays loops assembly mips

我获得了以下C代码以在MIPS程序集中实现。

for(i=0; i<16, i++){
  for(i=0; j<16, j++){
    C[i][j] = A[i][j] = B[j][i]
  }
}

数组已经为我们初始化了,我们只需要处理内存。

这是我如何制作嵌套循环。

  First:
    bge $t1, $t0, Exit
        Second:
            bge $t2, $t0, Continue
    #do work here.
            addi $t2, $t2, 1
            j Second
    Continue:
    addi $t1, $t1, 1
    j First
   Exit:

加载计数器:

addi $t0, $t0, 16

move $t1, $zero
move $t2, $zero

la $t3, A
la $t4, B
la $t5, C

A [i] [j]的逻辑使用公式基数+字长*(行*最大尺寸+ Col)

    sllv $t6, $t0, $t1 #Row, shift 16 by current counter -> 32 -> 64..
    addu $t6, $t6, $t2 #Column, add column counter.
    sll $t6, $t6, 2 #Shift entire count by word length.
    addu $t6, $t6, $t3 #Add A base address.
    lw $t7, ($t6) #Load word from that address.

完整代码:

    addi $t0, $t0, 16

    move $t1, $zero
    move $t2, $zero

    la $t3, A
    la $t4, B
    la $t5, C

First:
    bge $t1, $t0, Exit
        Second:
            bge $t2, $t0, Continue

            ###

            #Zero out counters first.
            move $t6, $zero
            move $t7, $zero
            move $t8, $zero
            move $t9, $zero

            sllv $t6, $t0, $t1 #Row, shift 16 by current counter -> 32 -> 64..
            addu $t6, $t6, $t2 #Column, add column counter.
            sll $t6, $t6, 2 #Shift entire count by word length.
            addu $t6, $t6, $t3 #Add A base address.
            lw $t7, ($t6) #Load word from that address.

            sllv $t7, $t0, $t2 #Row, shift 16 by current counter -> 32 -> 64..
            addu $t7, $t7, $t1 #Column, add column counter.
            sll $t7, $t7, 2 #Shift entire count by word length.
            addu $t7, $t7, $t4 #Add B base address.
            lw $t8, ($t7) #Load word from that address.

            addu $t9, $t7, $t8 #add A and B results.

            addu $t7, $t6, $t5 #add C base address, reuses $t7, copies $t6 from *A* array.

            sw $t9, 0($t7)  #store above result to C.

            ###

            addi $t2, $t2, 1
        j Second
    Continue:
    addi $t1, $t1, 1
    j First
Exit:

我收到了错误的地址错误,但我似乎无法弄清楚出了什么问题。

1 个答案:

答案 0 :(得分:1)

至少有三个错误:

  • 您正在覆盖$t6,其中A和C的偏移量应为A
  • 的基址
  • 您正在覆盖$t7,其中应包含A [i] [j]的内容,地址为B [j] [i]
  • 您错误地计算了行偏移量。您应该将行移动4次(有效地将行乘以16),而不是移动16次行。

您可以更改

sllv $t6, $t0, $t1 #Row, shift 16 by current counter -> 32 -> 64..
   ...
addu $t6, $t6, $t3 #Add A base address.
lw $t7, ($t6) #Load word from that address.
   ...
sllv $t7, $t0, $t2 #Row, shift 16 by current counter -> 32 -> 64..
   ...
addu $t9, $t7, $t8 #add A and B results

sll $t6, $t1, 4   # Row
   ...
addu $t7, $t6, $t3 #Add A base address.
lw $t9, ($t7) #Load word from that address.
   ...
sll $t7, $t2, 4  # Row
   ...
addu $t9, $t9, $t8 #add A and B results.