0

I have been trying to complete this program for the last 13 hours. I have tried everything and nothing seems to work. For some reason it works when I take input from the user for 4x4 matrices and multiply them. It gives the wrong answer when I try to compute the product of two 8x8 matrices I have already initialized.

Initialized Matrices;

array1: .byte 1,3,4,7,8,8,9,4,3,2,3,32,3,4,5,6,7,8,9,8,6,4,5,4,4,3,5,6,7,7,8,7,5,4,3,4,5,6,5,4,5,4,5,6,7,8,8,9,5,3,3,2,4,6,7,8,3,2,2,1,3,4,5,6,7,7,5,4,3,4,5,4,4,3,3,4,3,3,4,3,3,34,5,5,6,7,7,8,6,5,4,4,3,3,5,6,7 array2: .byte 0,4,4,7,6,6,4,4,4,7,4,7,4,4,7,6,7,6,4,6,6,4,0,4,4,4,7,6,7,7,6,7,7,4,7,4,7,6,7,4,7,4,6,6,7,6,6,4,7,4,4,7,4,6,7,7,4,7,7,4,4,4,7,6,7,7,7,4,4,4,7,4,44,4,4,4,7,4,4,4,4,44,7,7,6,7,7,6,7,7,4,4,4,4,7,6,7

Code to multiply them;

la $t2, array3
la $t3, array2
la $a1, array1
li $s1, 0
li $a2, 8
L1: # First Row of Product Matrix complete. Start second row of product matrix
li $s2, 0
L2:
li $s3, 0
sll $t1, $s1, 3
addu $t1, $s2, $t1
addu $t1, $t2, $t1
lbu $s4, ($t1)
L3:
sll $t4, $s3, 3
addu $t4, $t4, $s2
addu $t4, $t4, $t3
lbu, $t5, ($t4)
sll $t6, $s1, 3
addu $t6, $t6, $s3
addu $t6, $t6, $a1
lbu $t7, ($t6)
mul $t5, $t7, $t5
addu $s4, $s4, $t5
addiu $s3, $s3, 1
bne $s3, $a2, L3
sb $s4, ($t1)   
addiu $s2, $s2, 1
bne $s2, $a2, L2
addiu $s1, $s1, 1
bne $s1, $a2, L1 # First row of product matrix is complete.

addiu $a0, $zero, 10
li $v0,11
syscall
la $s7, Productmatrixmessage
move $a0, $s7
li $v0, 4
syscall
addiu $a0, $zero, 10
li $v0,11
syscall
la $t2, array3
li $t6, 8
li $s4, 1
li $t4, 65
li $t5, 0
li $t7, 1
la $a0, Outputrowmessage
li $v0, 4
syscall
li $a0, 1
li $v0, 1
syscall
la $a0, colon
li $v0, 4
syscall

outputproductmatrix:
beq $s4, $t4, Productmatrixend
la $a0, space
li $v0, 4
syscall
lbu $a0, ($t2)
li $v0, 1
syscall
addiu $t2, $t2, 1
addiu $t5, $t5, 1
addiu $s4, $s4, 1
beq $t5, $t6, newline
j outputproductmatrix

newline:
beq $s4, $t4, Productmatrixend
addi $t7, $t7, 1
li $t5, 0
addiu $a0, $zero, 10
li $v0,11
syscall
la $a0, Outputrowmessage
li $v0, 4
syscall
move $a0, $t7
li $v0, 1
syscall
la $a0, colon
li $v0, 4
syscall
j outputproductmatrix

Productmatrixend:
David Wohlferd
  • 7,110
  • 2
  • 29
  • 56
  • I don't speak mips assembler, so I'm not going to be much help. That said, you might want to update this code with more comments. A wall of uncommented assembler code like this is hard to read. Plus, sometimes you can find your own problems this way. Also, your 8x8 matrices both have more than 64 elements? – David Wohlferd Jun 29 '17 at 08:24
  • Yes they have 64 elements each. Its extremely frustrating because it makes no sense that 4x4 gives the right answer and 8x8 gives the wrong answer. The only difference is that in 4x4 I took input from the user and in 8x8 i have already initialized the matrices. I changed the appropriate parameters for 8x8. – Ahsen Waqar Jun 29 '17 at 09:08
  • You might want to count again. I see 97 elements in array1. – David Wohlferd Jun 29 '17 at 10:22
  • That doesn't really matter, it just has to be>=64. Found the error though, apparently RAM can only store a max value of 8 bits which means a max of 256 in 1 cell. Instead of showing an error, it made the value zero whenever it increased more than 256. Assembly is very frustrating. – Ahsen Waqar Jun 29 '17 at 10:25
  • 2
    It's not assembly's fault that you used the wrong data type. The same would have happened in C for example. – Jester Jun 29 '17 at 11:43
  • 1
    Assembly is language of the machine. Trust me, you don't want the machine to try to fix your mistakes. It sort of works with high level languages, where the occasional backfire means just restarting a service or something, but would you halt your CPU by some abusive state, it would probably bite you much more, making you wonder why they can't make computers that "just work". So the HW itself has a deterministic finite well defined nature, not expanding data types "as needed" on the fly. – Ped7g Jun 29 '17 at 13:10

0 Answers0