0% found this document useful (0 votes)
26 views3 pages

Assembly Program for 2x2 Matrix Addition

The document describes an assembly language program that adds two 2x2 matrices and outputs the result. It provides the input matrices, explains the problem, and shows the assembly code that implements the matrix addition and output.

Uploaded by

ziniaafrin1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Assembly Program for 2x2 Matrix Addition

The document describes an assembly language program that adds two 2x2 matrices and outputs the result. It provides the input matrices, explains the problem, and shows the assembly code that implements the matrix addition and output.

Uploaded by

ziniaafrin1988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question

Click Here

Answer

Step 1 of 2
Given Data:

T he following information is given in the question.


T here are two 2x2 matrices: matrix1 and matrix2.
Each matrix contained non-negative single-digit numbers.
T he matrices are represented as arrays in memory: matrix1 and matrix2.
One can use EM U 8086 emulator for assembly programming.
Objective:

T he objective of the question is to write an assembly program that for the following
requiremnts-
It will take input for the two 2x2 matrices.
T hen add the corresponding elements of the two matrices.
After that , the program should stores the result in another 2x2 matrix.
T hen , it will prints the elements of the resultant matrix.

Expalantion

A matrix is defined as a two-dimensional array that will store the elements arranged
in rows and column. A matrix stores the integer or string elements. T he different
mathematical operation such subtraction or addition can be done on matrix.

Step 2 of 2
Program:
T he assembly program for the EM U 8086 emulator that will take two 2x2 matrices
as input and add them, and print the resultant matrix, is implemented here-

.model small
.stack 100h

.data
matrix1 db 4, 5, 2, 3 ; First matrix: [4, 5; 2, 3]
matrix2 db 1, 2, 3, 4 ; Second matrix: [1, 2; 3, 4]
resultMatrix db 4 dup(?) ; Resultant matrix: Placeholder for the result

.code
main proc
mov ax, @data
mov ds, ax

; Add matrices and store the result in resultMatrix


mov si, 0 ; Initialize source index for matrices
mov di, 0 ; Initialize destination index for resultMatrix

; Loop through each element in the matrices


mov cx, 4 ; Number of elements in the matrices
matrix_loop:
mov al, [matrix1 + si] ; Load element from matrix1
add al, [matrix2 + si] ; Add corresponding element from matrix2
mov [resultMatrix + di], al ; Store the result in resultMatrix

inc si ; Move to the next element in matrices


inc di ; Move to the next element in resultMatrix
loop matrix_loop

; Print the resultant matrix


mov dl, [resultMatrix] ; Load the first element of the result matrix
add dl, 30h ; Convert to ASCII
mov ah, 02h ; BIOS function to print character
int 21h ; Print the character

mov dl, ',' ; Print a comma


mov ah, 02h
int 21h

mov dl, [resultMatrix + 1] ; Load the second element of the result matri
add dl, 30h
mov ah, 02h
int 21h

mov dl, 0Dh ; Print a carriage return


mov ah, 02h
int 21h

mov dl, 0Ah ; Print a line feed


mov ah, 02h
int 21h

mov dl, [resultMatrix + 2] ; Load the third element of the result matrix
add dl, 30h
mov ah, 02h
int 21h

mov dl, ','


mov ah, 02h
int 21h
mov dl, [resultMatrix + 3] ; Load the fourth element of the result matri
add dl, 30h
mov ah, 02h
int 21h

mov ah, 4Ch ; Exit program


int 21h
main endp

end main

Expalantion

T his assembly code will take the input for the two matrices and then print their sum
in another matrix.

Final Answer
T he assembly code will take the input for the two matrices and then print their sum
in another matrix i is implemented in step 2.

T he sample output of the code is given below-

You might also like