Matrix addition
A program that adds two 2x2 matrices element by element and stores the result in a third matrix.
Program source code
title .model small .stack 100h data segment public aMatrix dw 1, 2 dw 3, 4 bMatrix dw 5, 6 dw 7, 8 cMatrix dw ?, ? dw ?, ? rows equ 2 cols equ 2 fmtout db "%d \0" data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure mov cx, [rows*cols] lea bp, es:[cMatrix] ; Load the address of the cMatrix array into es:bp lea di, es:[bMatrix] ; Load the address of the bMatrix array into es:di lea si, ds:[aMatrix] ; Load the address of the aMatrix array into ds:si @AdditionLoop: mov ax, word ptr ds:[si] add ax, word ptr es:[di] mov word ptr es:[bp], ax incw si ; Increment the si index by 2 incw di ; Increment the di index by 2 incw bp ; Increment the bp index by 2 loop @AdditionLoop xor bx, bx ; Clear the bx register, it will serve as an index lea si, ds:[cMatrix] ; Load the address of the cMatrix array into ds:si jmp @COLS_Loop ; Jump unconditionally to the @COLS_Loop label @ROWS_Loop: xor bl, bl ; Clear the bl register @COLS_Loop: push word ptr[si] ; 2nd: load the value at index si onto the top of the stack lea ax, ds:[fmtout] ; 1st: load the address of the fmtout format string into ax call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack incw si ; Increment the si index by 2 inc bl cmp bl, cols jl @COLS_Loop ; Loop while the bl register is not equal to 2 call PrintNewLine ; Call the PrintNewLine procedure inc bh cmp bh, rows jl @ROWS_Loop ; Loop while the bh register is not equal to 2 xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
6 8
10 12
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".