Matrix Multiplication Algorithm in 8085 Assembly Language


Before multiplying two matrices in the 8085 Microprocessor, let's take a look at multiplication of two 8-bit numbers in 8085 as no predefined multiplication subroutine exists here, already. The result is stored at location 3050H and 3051H.

Address Mnemonics Comments
2000H LHLD 2050H The content of 2051H is loaded in H and that of 2050H in L register
2003H XCHG Swaps the values of HL and DE register pairs
2004H MOV C, D Loads the content of D in C register
2005H MVI D, 00 Assigns 0 value to D register
2007H LXI H, 0000 Loads 00 value to H and L
200AH DAD D Assigns the sum of HL and DE pairs to HL pair
200BH DCR C Decrement C register by 1
200CH JNZ 200AH If zero flag is not set jump to 2000H
200FH SHLD 3050H Stores the value at H to 3051H address and the value in L to 3050H memory address
2012H HLT Terminates the program

Now, we can use this subroutine to perform matrix multiplcation for 1xN with Nx1 and 2x2 with 2x2 matrices. For generalised matrix multiplication for MxN with NxP, we would have to use stacks or have more than 4 registers in the microprocessor. The use of stacks hinders our ability to use the HL register pair and we may loose the value stored in the HL pair due to previous multiplications and addition operations.

Here, the matrices are stored at addresses 0200H and 0300H respectively, both are 2x2 matrices and result matrix is stored at 0600H. Hence, the algorithm may be given as:

Address Label Mnemonics Comments
0100H LXI H,0200H The adress of matrix 1 is stored in HL pair
0103H LXI D, 0300H The adress of matrix 2 is stored in DE pair
0106H CALL MUL Multiply subroutine is called
0109H STA 0500H Product is stored at 0500H
010CH INX H Increment HL pair for getting the next value of matrix 1
010DH LXI D, 0302H Get address of next column element of matrix 2 in DE pair
0110H CALL MUL Multiply subroutine is called
0113H STA 0501H Product is stored at 0500H
0116H LXI H, 0200H Get address of row element of matrix 1 in HL pair
0119H LXI D, 0301H Get address of row element of matrix 2 in DE pair
011CH CALL MUL Call Multiply subroutine
011FH STA 0502H Product is stored at 0502H
0122H INX H Increment HL pair
0123H LXI D, 0303H Get address of 2nd row element of matrix 2 in DE pair
0126H CALL MUL Call Multiply subroutine
0129H CALL MUL Call Multiply subroutine
012CH STA 0503H Store Product in 0503H
012FH LXI H, 0202H Load address of matrix 1 element in HL pair
0132H LXI D, 0300H Load address of matrix 2 element in DE pair
0135H CALL MUL Call Multiply subroutine
0138H STA 0504H Store product at 0504H
013BH INX H Increment address of HL pair
013CH LXI D, 0302H Load value at address 0302H in DL pair
013FH CALL MUL Call Multiply subroutine
0142H STA 0505H Store product in 0505H
0145H LXI H, 0202H Load value at 0202H in HL pair
0148H LXI D, 0301H Load value at 0301H in DE pair
014BH CALL MUL Call Multiply subroutine
014EH STA 0506H Store product at 0506H
0151H INX H Increment HL pair
0152H LXI D, 0303H Load value at 0303H in DL pair
0155H CALL MUL Call Multiply subroutine
0158H STA 0507H Store Product at 0507H
015AH CALL ADD Call addition label
015DH HLT Terminate the Program
015EH MUL MOV A, M Move value at the address stored in HL pair to Accumulator
015FH MOV C, A Move value in accumulator to C register
0160H LDAX D Load value at address stored in DE pair to the accumulator
0161H MOV B, A Move contents from A to B register
0162H MVI A, 00 Clear accumulator
0164H L1 ADD B Add B to A and store in A
0165H DCR C Decrement C register
0166H JNZ L1 Jump if not zero to L1 Label
016FH RET Return to the next instruction
0170H ADD LXI H, 0500H Load value at 0500H in HL pair
0173H MOV A, M Move value stored in address at HL pair to accumulator
0174H INX H Increment HL pair
0175H ADD M Add value to accumulator
0176H STA 0600H First value of result matrix is stored at 0600H
0179H INX H Increment HL pair
017AH MOV A, M Move value stored in address at HL pair to accumulator
017BH INX H Increment HL pair
017CH ADD M Add value to accumulator
017DH STA 0601H Second value of result matrix is stored at 0601H
0180H INX H Increment HL pair
0181H ADD M Add value to accumulator
0182H STA 0602H Third value of result matrix is stored at 0602H
0185H INX H Increment HL pair
0186H ADD M Add value to accumulator
0187H STA 0603H Last value of result matrix is stored at 0603H
018AH RET Return


You can try the same in the simulator below: