Question

Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and...

Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and product, using row major, and column major math (so this is actually 4 problems, but obviously they’re all pretty related).

I generated two sample arrays to test

2   1       9       2

7   9       10      10

3   4       4       4

2   5       4       4

8   7       1       2
2   7       8       6
7   5       6       8
9   4       8       9

The output of your program will be another 4x4 matrix, which you should print off. Don’t ask the user to input 16 numbers, just hard code some arrays

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

rowcol_major. asm .data word 2,1,9,2 A: . word 7,9,10,10 word 3,4,4,4 word 2,5,4,4 word 8,7,1,2 В: word 2,7,8,6 word 7,5,6,8

#Print resultant matrix C li v0,4 la a0,message3 syscall # pass address of the matrix C to printmatrix #Pass ro size # Pass c

#Procedure to add two matrices using row major add Rowmajor # i-0 j-0 calculate the offset of element at [i] [jl li t0,0 li t

#Procedure to multiply two matrices using row major mul Rowmajor: # i-0 j-0 li t0,0 li stl,0 li $t3,0 mulr loopl: mulr loop2:

#Procedure to add two matrices using rOw major mul Colmajor: li tl,0 li st0,0 #j-0 # i-0 mulc loop1: mulc loop2: li t3,0 li t

#Procedure to print the matrix of double precission values printma trix : li to,0 # i-0 # st2- address of the matrix move t2,

Sample output:

Mars Messages Run I/O A= 2 1 9 2 7 9 10 10 3 4 4 4 2 5 4 4 B 8 7 1 2 2 78 6 7 5 6 8 9 489 Using row major, C-A+B = 10 8 10 4

Code to copy:

######################################## rowcol_major.asm ##################################

.data

A:            .word 2,1,9,2

                .word 7,9,10,10

                .word 3,4,4,4

                .word 2,5,4,4

B:            .word 8,7,1,2

                .word 2,7,8,6

                .word 7,5,6,8

                .word 9,4,8,9

C:            .word 0:4

                .word 0:4

                .word 0:4

                .word 0:4

message1:.asciiz "A=\n"

message2:.asciiz "B=\n"

message3:.asciiz "Using row major, C=A+B=\n"

message4:.asciiz "Using column major, C=A+B=\n"

message5:.asciiz "Using row major, C=AxB=\n"

message6:.asciiz "Using column major, C=AxB=\n"

space: .asciiz " "

newline: .asciiz "\n"

.text

main:     li $v0,4

                la $a0,message1

                syscall

                #Print matrix A

                la $a0,A                                # Pass base address of A to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix

                li $v0,4

                la $a0,message2

                syscall

                #Print matrix B

                la $a0,B                                # pass address of the matrix B to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix   

                # Test add_Rowmajor

                #Add A and B using row major and save the result in C

                li $a0,4                                  # Pass row size(and column size) to mul_Rowmajor

                la $a1,A                                # Pass base address of A

                la $a2,B                                # Pass base address of B

                la $a3,C                                # Pass base address of C

                jal add_Rowmajor                           # Call add_Rowmajor

                #Print resultant matrix C

                li $v0,4

                la $a0,message3

                syscall

                la $a0,C                                # pass address of the matrix C to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix                    # Call printmatrix

                # Test add_Colmajor

                #Add A and B using column major and save the result in C

                li $a0,4                                  # Pass row size(and column size) to mul_Rowmajor

                la $a1,A                                # Pass base address of A

                la $a2,B                                # Pass base address of B

                la $a3,C                                # Pass base address of C

                jal add_Colmajor                              # Call add_Colmajor

                #Print resultant matrix C

                li $v0,4

                la $a0,message4

                syscall

                la $a0,C                                # pass address of the matrix C to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix                    # Call printmatrix

                # Test mul_Rowmajor

                # Multiply A and B using row major and save the result in C

                li $a0,4                                  # Pass row size(and column size) to mul_Rowmajor

                la $a1,A                                # Pass base address of A

                la $a2,B                                # Pass base address of B

                la $a3,C                                # Pass base address of C

                jal mul_Rowmajor                           # Call mul_Rowmajor

                #Print resultant matrix C

                li $v0,4

                la $a0,message5

                syscall

                la $a0,C                                # pass address of the matrix C to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix                    # Call printmatrix

                # Test mul_Colmajor

                # Multiply A and B using column major and save the result in C

                li $a0,4                                  # Pass row size(and column size) to mul_Rowmajor

                la $a1,A                                # Pass base address of A

                la $a2,B                                # Pass base address of B

                la $a3,C                                # Pass base address of C

                jal mul_Colmajor                              # Call mul_Colmajor

                #Print resultant matrix C

                li $v0,4

                la $a0,message6

                syscall

                la $a0,C                                # pass address of the matrix C to printmatrix

                li $a1,4                                  # Pass row size

                li $a2,4                                  # Pass column size

                jal printmatrix                    # Call printmatrix

                #exit

                li $v0, 10                              # End the program

                syscall

#Procedure to add two matrices using row major

add_Rowmajor:                               

                                li $t0,0                   # i=0

addr_loop1:       li $t1,0                   # j=0

addr_loop2:       mul $t2,$t0,$a0 # calculate the offset of element at [i][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2

                               

                                add $t3,$t2,$a1

                                lw $t4,($t3)         # $t4=A[i][j]

                                add $t3,$t2,$a2

                                lw $t5,($t3)         # $t5=B[i][j]

                               

                                add $t4,$t4,$t5 # $t4=A[i][j]+B[i][j]

                               

                                add $t3,$t2,$a3

                                sw $t4,($t3)       # C[i][j]=A[i][j]+B[i][j]

                                               

                                add $t1,$t1,1      # j=j+1

                                beq $t1,4,addr_next       # if j=4, exit the inner loop          

                                j addr_loop2

addr_next:         add $t0,$t0,1      # i=i+1

                                beq $t0,4,return_add_Rowmajor # if i=4 and j=4 , exit the outer loop

                                j addr_loop1

               

return_add_Rowmajor: jr $ra                                      #Return to main program

#Procedure to add two matrices using column major

add_Colmajor:                                 

                                li $t1,0                   # j=0

addc_loop1:       li $t0,0                   # i=0

addc_loop2:       mul $t2,$t0,$a0 # calculate the offset of element at [i][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2

                               

                                add $t3,$t2,$a1

                                lw $t4,($t3)         # $t4=A[i][j]

                                add $t3,$t2,$a2

                                lw $t5,($t3)         # $t5=B[i][j]

                               

                                add $t4,$t4,$t5 # $t4=A[i][j]+B[i][j]

                               

                                add $t3,$t2,$a3

                                sw $t4,($t3)       # C[i][j]=A[i][j]+B[i][j]

                                               

                                add $t0,$t0,1      # i=i+1

                                beq $t0,4,addc_next # if j=4, exit the inner loop

                                j addc_loop2

addc_next:         add $t1,$t1,1      # j=j+1

                                beq $t1,4,return_add_Colmajor # if i=4 and j=4 , exit the outer loop

                                j addc_loop1

return_add_Colmajor: jr $ra                                        #Return to main program

#Procedure to multiply two matrices using row major

mul_Rowmajor:                               

                                li $t0,0                   # i=0

mulr_loop1:       li $t1,0                   # j=0

mulr_loop2:       li $t3,0                   # k=0

                                li $t7,0                   # sum=0

mulr_loop3:       mul $t2,$t0,$a0 # calculate the offset of element A[i][k]

                                add $t2,$t2,$t3

                                sll $t2,$t2,2         # multiply with storage size

                                add $t4,$t2,$a1

                                lw $t4,($t4)

                               

                                mul $t2,$t3,$a0 # calculate the offset of element at B[k][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2         # multiply with storage size

                                add $t5,$t2,$a2

                                lw $t5,($t5)

                               

                                mul $t4,$t4,$t5

                                add $t7,$t7,$t4

                                add $t3,$t3,1

                                beq $t3,$a0,mulr_next1

                                j mulr_loop3

mulr_next1:

                                mul $t2,$t0,$a0 # calculate the offset of element at [i][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2         # multiply with storage size

                                add $t3,$t2,$a3

                                sw $t7,($t3)       # C[i][j]=sum or A[i][k]*B[k][j]

                                               

                                add $t1,$t1,1      # j=j+1

                                beq $t1,$a0,mulr_next2                # if j=4, exit the inner loop          

                                j mulr_loop2

mulr_next2:       add $t0,$t0,1      # i=i+1

                                beq $t0,$a0,return_mul_Rowmajor # if i=4 and j=4 , exit the outer loop

                                j mulr_loop1

               

return_mul_Rowmajor: jr $ra                                     #Return to main program

#Procedure to add two matrices using row major

mul_Colmajor:                                 

                                li $t1,0                   # j=0

mulc_loop1:       li $t0,0                   # i=0

mulc_loop2:                      

                                li $t3,0                   # k=0

                                li $t7,0                   # sum=0

mulc_loop3:       mul $t2,$t0,$a0 # calculate the offset of element A[i][k]

                                add $t2,$t2,$t3

                                sll $t2,$t2,2         # multiply with storage size

                                add $t4,$t2,$a1

                                lw $t4,($t4)

                               

                                mul $t2,$t3,$a0 # calculate the offset of element at B[k][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2         # multiply with storage size

                                add $t5,$t2,$a2

                                lw $t5,($t5)

                               

                                mul $t4,$t4,$t5

                                add $t7,$t7,$t4

                                add $t3,$t3,1

                                beq $t3,$a0,mulc_next1

                                j mulc_loop3

mulc_next1:

                                mul $t2,$t0,$a0 # calculate the offset of element at [i][j]

                                add $t2,$t2,$t1

                                sll $t2,$t2,2         # multiply with storage size

                                add $t3,$t2,$a3

                                sw $t7,($t3)       # C[i][j]=sum or A[i][k]*B[k][j]

                                               

                                add $t0,$t0,1      # i=i+1

                                beq $t0,$a0,mulc_next2               # if j=4, exit the inner loop          

                                j mulc_loop2

mulc_next2:       add $t1,$t1,1      # j=j+1

                                beq $t1,$a0,addover # if i=4 and j=4 , exit the outer loop

                                j mulc_loop1

               

addover: jr $ra                                   #Return to main program

#Procedure to print the matrix of double precission values

printmatrix :                      

                                li $t0,0                   # i=0

                                                               

                                move $t2,$a0     # $t2=address of the matrix

oloop:

                                li $t1,0                   # j=0

iloop:

                                lw $a0, ($t2)      

                                li $v0,1

                                syscall                   # print the number

                                add $t2,$t2,4      # go to next element

                                add $t0,$t0,1      # i=i+1

                                add $t1,$t1,1      # j=j+1

                                beq $t1,4,print_next      # if j=4, exit the inner loop

                                la $a0,space       # print space

                                li $v0,4

                                syscall

                                j iloop                    # go to label iloop

print_next:         la $a0,newline   # print new line

                                li $v0,4                 

                                syscall  

                                beq $t0,16,printingover # if i=16, exit the outer loop

                                j oloop

printingover:      la $a0,newline   # print new line

                                li $v0,4

                                syscall

                                jr $ra                      #Return to main procedure

############################################################################################

               

Add a comment
Know the answer?
Add Answer to:
Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • phyton help please!!!!! Find the sum of two equal-number matrices. Example: First ask the user for...

    phyton help please!!!!! Find the sum of two equal-number matrices. Example: First ask the user for the number of rows of the two matrices. 2 2 + 5 8 = 7 10 54 4 10 9 14 Then ask the value of each of the rows of the two matrices, where each column of the row must be separated by a space (use the split function to convert the captured text to a list)

  • python help please and thank you Find the sum of two equal-number integer matrices. Example: 2...

    python help please and thank you Find the sum of two equal-number integer matrices. Example: 2 2 + 5 8 = 7 10 5 4 4 10 9 14 First it asks the user for the number of rows of the two matrices. Then ask for the value of each of the rows of the two matrices, where each column of the row must be separated by a space (use the split function to convert the captured text to a...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • 3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B...

    3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B and display the resulting matrix C, which is the sum of A and B. Procedures must be used to organize the code.

  • C Language Question

    Write a program to calculate the row averages of a 4x4 matrix. In main program get the elements of a 4x4 matrix and display the matrix, then call the function “average” to calculate the row averages. Function “average” will find the average of rows of a 4x4 matrix and store the results into a 1-dimensional array and return it as parameter. Main program should display the row averages which are greater than 5.0 returned by the function “average”. Use c language  Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT