简体   繁体   English

嵌套数组用于循环,MIPS组装

[英]Arrays in nested for loops, MIPS assembly

I was given the following C code to implement in MIPS assembly. 我获得了以下C代码,以在MIPS汇编中实现。

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

The arrays are already initialized for us, we only have to deal with the memory. 数组已经为我们初始化了,我们只需要处理内存即可。

Here's how i made my nested loop. 这是我制作嵌套循环的方式。

  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:

Loading the counters: 加载计数器:

addi $t0, $t0, 16

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

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

And logic for A[i][j] using the formula Base + Word Length * (Row * Max size + Col) : 以及A [i] [j]的逻辑,使用公式Base + Word Length *(行*最大大小+ 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.

Full code: 完整代码:

    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:

I'm getting a bad address error but I can't seem to figure out what is wrong. 我收到一个错误的地址错误,但似乎无法弄清楚什么地方出了错。

There are at least three errors: 至少存在三个错误:

  • You are overwriting $t6 which should have the offset of A and C with the base address of A 您正在覆盖$t6 ,它应该具有A和C的偏移量以及A的基地址
  • You are overwriting $t7 which should hold the content of A[i][j] with the address of B[j][i] 您正在覆盖$t7 ,该地址应包含地址B [j] [i]中的A [i] [j]
  • You are miscalculating the row offset. 您正在错误地计算行偏移量。 Instead of shifting 16 row times, you should shift row 4 times (which effectively multiplies row by 16) 而不是移动16行时间,您应该将行移动4次(有效地将行乘以16)

You may change 你可能会改变

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

with

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM