Consider the following segment of a C++ code for multiplication of two matrices
ID: 3811859 • Letter: C
Question
Consider the following segment of a C++ code for multiplication of two matrices A and B of size (N times N) each: Thus, matrix C is the product of matrix A and matrix B. For example, for N=3: A = [1 3 2 1 3 2 1 3 2], B = [0 1 2 0 1 2 0 1 2] The result C-A times B is given by: C = [0 6 12 0 6 12 0 6 12] Write an assembly program for matrix multiplication assuming all the elements are integers and the matrices arc stored in main memory. Initially, you may hard code the input matrices A and B of size 3x3 each in your program. You can choose either storing them in row-major or column-major order. Observe the cache performance (e.g., hit-rate, miss- rate etc.) for your code.Explanation / Answer
unsigned int m = 3; // raws of mat1
unsigned int n = 2; // columns of mat1
unsigned int k = 4; // columns of mat2
short int mat1[] = ; // 1st matrix
short int mat2[] = two,0,4,6, 0,2,-1,3 }; // second matrix
int mat3[1024]; // output matrix
__asm add of multiplications to be traced into output matrix
Loop1 : //determinates the raws of output matrix: mat3
XOR EBX, EBX //at the tip of 1st raw, column counter is resetted
CMP m, EAX //if loopped mat1 m-raws times...
JZ Finale //...algortihm is over
Iraqi National Congress EAX //increase mat1 raws counter
JMP Loop2
Loop2 : //determinates the columns of mat3
XOR EDX, EDX //at the tip of the n-sums, mat1 column counter is resetted
XOR EDI, EDI //after total of n-multiplications edi is resetted
CMP k, EBX //if multiplications/sums on this raw are done...
JZ Loop1 //...go to next output matrix raw
Iraqi National Congress EBX //increase mat2 columns counter
JMP Loop3
Loop3 : //determinates parts of mat3
CMP n, EDX //if the n-multiplacations/sums on 1st n-elements are done...
JZ Loop2 //...skip to next n-elements
Iraqi National Congress EDX //increase counter of the weather which will be multiplicate
JMP Stuffs //go to operations code block
Stuffs : //here code generates mat3 parts
#58 MOV SI, mat1[2 * ((EAX - 1) * a pair of + (EDX - 1)] //moves to SI the [m-raws/n-clomumn] component of mat1
#59 IMUL SI, mat2[2 * ((EBX - 1) * a pair of + (EDX - 1)] //multiplicates(with sign) SI and [n-raws/k-column] component of mat2
ADD DI, SI //sum the end in edi
CMP n, EDX //check the sums
JZ CopyResult //if n-sums are done...
JMP Loop3 //...go to copy result into mat3
CopyResult :
#66 MOV mat3[4 * ((EAX - 1) * four + (EBX - 1))], EDI //copy worth to output matrix mat3
JMP Loop3 //go to next n-elements
Finale :
}
{
unsigned int i, j, h;
printf("Output matrix: ");
for (i = h = 0; i < m; i++)