Reputation: 3361
I was given the following C code to implement in MIPS assembly.
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):
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.
Upvotes: 0
Views: 677
Reputation: 22585
There are at least three errors:
$t6
which should have the offset of A and C with the base address of A$t7
which should hold the content of A[i][j] with the address of B[j][i]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.
Upvotes: 1