Printing a two-dimensional array
A program that prints the elements of a two-dimensional array using nested loops over rows and columns.
Program source code
title .model small .stack 100h data segment public array dw 10, 20, 30, 40, 50, 60 dw 70, 80, 90, 10, 20, 30 dw 40, 50, 60, 70, 80, 90 dw 10, 20, 30, 40, 50, 60 rows dw 4 cols dw 6 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 lea bx, ds:[array] ; Load the address of the array variable into the bx register mov cx, rows ; Set the counter to the value in the rows variable PrintRowsLoop: push cx ; Save the value of the cx counter register on the stack mov cx, cols ; Set the counter to the value in the cols variable PrintColsLoop: push word ptr[bx+si] ; 2nd: push the value at bx with index si onto 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 ; Call the incw macro. Increment the si index by 2 loop PrintColsLoop ; Loop while CX≠0 pop cx ; Pop the value from the stack into the cx register call PrintNewLine ; Call the PrintNewLine procedure loop PrintRowsLoop ; Loop while CX≠0 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
10 20 30 40 50 60
70 80 90 10 20 30
40 50 60 70 80 90
10 20 30 40 50 60
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".