MASM: Problem in 3X3 matrix multiplication in the 8086?

4.1k Views Asked by At

I am trying to multiply two matrices of dimension 3x3 and want to store the result in the new 2d array. I know very well the algorithm in C++ that how to do, but the main problem for me is with iterating the three loops and setting the pointers in case of 8086

In c++ , below will be the simple code
   for(int i=0;i<3;i++)
   { 
      for(int j=0;j<3;j++)
     {
        for(int k=0;k<3;k++) 
           mat3[i][j]+=(mat1[i][k]*mat2[k][j]);
     }
   }

But coding the above code in MASM seems very difficult, please help me that how to approach the coding part in MASM (8086) Below is the code that I have written and below is the sample array, actually, I know the below code is incomplete but I am not able to think how to go further with so limited number of register contents(maybe there will be a good approach but my knowledge is limited about it)

ASSUME CS:CODE , DS:DATA
DATA SEGMENT
ARR1 DW 01H,02H,03H
     DW 05H,06H,07H
     DW 08H,09H,02H
ARR2 DW 03H,04H,05H
     DW 06H,07H,08H
     DW 10H,11H,12H
ARR RES DW 00H,00H,00H
        DW 00H,00H,00H
        DW 00H,00H,00H
DATA ENDS
CODE SEGMENT
START:
   MOV AX,DATA
   MOV DS,AX
   MOV SI,OFFSET ARR1
   MOV DX,OFFSET ARR2
   MOV AX,OFFSET RES3
   MOV CL,00H
   MOV CH,03H
   MOV BL,03H
   MOV BH,00H
LOOP1:
    
    LOOP2:
    LOOP3:

    ADD CL,03H
    SUB CL,09H
    JZ END
    JMP LOOP1
CODE ENDS
END START
;DOUBTS
;HOW TO USE INTIALIZE THE STARTING ADDRESS OF THE 2D ARRAY WITH SO MYCH
;LIMITED REGISTERS 
1

There are 1 best solutions below

2
On

Don't look too much at that C++ code.

  mov bx, offset ARR3

Study below how you would calculate the R(0,0) element by multiplying the 1st row of ARR1 with the 1st column of ARR2:

  mov si, offset ARR1
  mov di, offset ARR2
  mov cx, 3
  xor bp, bp
Sum:
  mov ax, [si]
  mul word ptr [di]
  add bp, ax
  add si, 2    ; Next element on 1st row of ARR1
  add di, 6    ; Next element in 1st column of ARR2
  dec cx
  jnz Sum
  mov [bx], bp ; Store in R(0,0)

Study below how you would calculate the R(0,1) element by multiplying the 1st row of ARR1 with the 2nd column of ARR2:

  mov si, offset ARR1
  mov di, offset ARR2 + 2
  mov cx, 3
  xor bp, bp
Sum:
  mov ax, [si]
  mul word ptr [di]
  add bp, ax
  add si, 2    ; Next element on 1st row of ARR1
  add di, 6    ; Next element in 2nd column of ARR2
  dec cx
  jnz Sum
  mov [bx+2], bp ; Store in R(0,1)

Study below how you would calculate the R(0,2) element by multiplying the 1st row of ARR1 with the 3rd column of ARR2:

  mov si, offset ARR1
  mov di, offset ARR2 + 4
  mov cx, 3
  xor bp, bp
Sum:
  mov ax, [si]
  mul word ptr [di]
  add bp, ax
  add si, 2    ; Next element on 1st row of ARR1
  add di, 6    ; Next element in 3rd column of ARR2
  dec cx
  jnz Sum
  mov [bx+4], bp ; Store in R(0,2)

Do you see a pattern emerging?
The task now is to combine these snippets into a single loop. Might require pushing/popping a few registers and/or using some memory based variables!

Later you repeat the lot for the 2nd and 3rd rows of the resulting matrix.