; PROGRAM: ADD TWO MATRIX
read macro num
mov ah, 01h
int 21h
mov bh, al
int 21h
mov bl, al
sub bh, 30h
mov al, 0ah
mul bh
sub bl, 30h
add al, bl
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msg_row db 'Rows: $'
msg_col db 'Columns: $'
comma db ', $'
space db ' $'
tab db ' $'
msg_a db 'Matrix A: $'
msg_b db 'Matrix B: $'
sumis db 'Sum of A & B: $'
nl db 0dh,0ah,'$'
rows db 00h
cols db 00h
mat_a db 25 dup('$')
mat_b db 25 dup('$')
mat_sum db 25 dup('$')
temp_row dw 00h
temp_col dw 00h
temp_prt db 10 dup('$')
cnt dw ?
num db 3 dup('$')
ele db ?
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
mov es, ax
print msg_row
read rows ;read rows
print nl
print msg_col
read cols ;read cols
print nl
print msg_a
mov si, offset mat_a ;read matrix A
call readMatrix
print nl
print msg_b
mov si, offset mat_b ;read matrix B
call readMatrix
print nl
print msg_a
print nl
mov di, offset mat_a
call printMatrix
print msg_b
print nl
mov di, offset mat_b
call printMatrix
;Logic to add two matrix
mov al, rows
mov cl, cols
mul cl ;ax hold rows*cols
mov bx, 00h
ADDMATRIX:
cmp bx, ax
jge EXITADD
mov si, offset mat_a
mov dl, [bx][si]
mov di, offset mat_b
mov dh, [bx][di]
add dl, dh
mov si, offset mat_sum
mov [bx][si], dl
inc bx
jmp ADDMATRIX
EXITADD:
print sumis
print nl
mov di, offset mat_sum
call printMatrix
FINISH:
mov ah, 4ch
int 21h
printMatrix proc near
mov cx, 00h
PRINTROWS:
cmp cl, rows
jge EXITPRINT
mov bx, 00h
PRINTCOLS:
cmp bl, cols
jge EXITCOLS
mov temp_row, cx
mov temp_col, bx
mov al, cl
mov dl, cols
mul dl
add bx, ax
mov al, [bx][di]
mov ah, 00h
mov si, offset temp_prt
call hex2ascii
print temp_prt
print space
mov cx, temp_row
mov bx, temp_col
inc bx
jmp PRINTCOLS
EXITCOLS:
print nl
inc cx
jmp PRINTROWS
EXITPRINT:
RET
printMatrix endp
readMatrix proc near
mov cl, rows
mov al, cols
mul cl
mov cx, ax ;cx=rows*cols
READARRAY:
read ele
print space
mov al, ele
mov [si], al
inc si
loop READARRAY
readMatrix endp
hex2ascii proc near
mov cx, 0000h
REPDIV:
mov dx, 0000h
mov bx, 000ah
div bx
add dl, 30h
push dx
inc cx
cmp ax, 000ah
JGE REPDIV
cmp al, 00h
je SKIP0
add al, 30h
mov [si], al
inc si
SKIP0:
SAVEASCI:
pop dx
mov [si], dl
inc si
loop SAVEASCI
STOP1:
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>matrix_add
Rows: 02
Columns: 03
Matrix A: 01 02 03 04 05 06
Matrix B: 02 02 02 02 02 02
Matrix A:
1 2 3
4 5 6
Matrix B:
2 2 2
2 2 2
Sum of A & B:
3 4 5
6 7 8
**************************
0 comments:
Post a Comment