assembly - Matrix Multiplication in MIPS, Array Looping -
first time on stackoverflow, please forgive me if i'm doing wrong. trying create c code below in mips:
void mm (int c[][], int a[][], int b[][]) { int i, j ,k; (i = 0; != 4; = + 1) (j = 0; j != 4; j = j + 1) (k = 0; k != 4; k = k + 1) c[i][j] = c[i][j] + a[i][k] * b[k][j] }
here's mips code:
.text main: j mm mm: la $a3, array_a # base address array_a loaded $a3 la $a1, array_b # base address array_b loaded $a1 la $a2, array_c # base address array_c loaded $a2 li $t1, 4 # $t1 = 4 (row-size , loop end) li $s0, 0 # = 0; initialize 1st loop loop1: li $s1, 0 # j = 0; restart 2nd loop loop2: li $s2, 0 # k = 0; restart 3rd loop sll $t2, $s0, 2 # $t2 = * 4 (size of row of c) addu $t2, $t2, $s1 # $t2 = * size(row) + j sll $t2, $t2, 2 # $t2 = byte offset of [i][j] addu $t2, $a2, $t2 # $t2 = byte offset of [i][j] lw $t4, 0($t2) # $t4 = 2 bytes of c[i][j] loop3: sll $t0, $s2, 2 # $t0 = k * 4 (size of row of b) addu $t0, $t0, $s1 # $t0 = k * size(row) + j sll $t0, $t0, 2 # $t0 = byte offset off [k][j] addu $t0, $a1, $t0 # $t0 = byte address of b[k][j] lw $t5, 0($t0) # $t5 = 2 bytes of b[k][j] sll $t0, $s0, 2 # $t0 = * 4 (size of row of a) addu $t0, $t0, $s2 # $t0 = * size(row) + k sll $t0, $t0, 2 # $t0 = byte offset of [i][k] addu $t0, $a3, $t0 # $t0 = byte address of a[i][k] lw $t6, 0($t0) # $t6 = 2 bytes of a[i][k] mul $t5, $t6, $t5 # $t5 = a[i][k] * b[k][j] add $t4, $t4, $t5 # $t4 = c[i][j] + a[i][k] * b[k][j] addiu $s2, $s2, 1 # $k = k + 1 bne $s2, $t1, loop3 #if (k != 4) go loop3 sw $t4, 0($a2) # c[i][j] = $t4 #----------test------------- li $v0, 1 lw $a0, ($a2) syscall li $v0, 4 la $a0, new_row syscall #----------test------------- addiu $s1, $s1, 1 # $j = j + 1 addi $a2, $a2, 4 bne $s1, $t1, loop2 # if (j != 4) go loop2 addiu $s0, $s0, 1 # $i = + 1 bne $s0, $t1, loop1 # if (i != 32) go l1 exit: li $v0, 10 #exits syscall .data array_a: .word 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 array_b: .word 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 array_c: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 output_row_string_c: .asciiz "matrix c output row " colon_string: .asciiz ": space_string: .asciiz " " new_row: .asciiz "\n" char_space: .space 2
the output expecting array of 8's. reformat later become 4x4 matrix. however, have placed syscall values of array printing , getting garbage:
8 8 8 8 8 8 8 8 8 1126201457 544503160 536885768 8 8 8 8
i trying make sure right values being multiplied , stored i'm not sure if there problem in data allocation.
thanks help!
remove line addi $a2, $a2, 4
after #### test snippet. changing base address of array_c
think not want.
also, arrays array_a, array_b , array_c have 17 elements instead of 16 (4x4), though not cause of misbehavior.
Comments
Post a Comment