Accessing 2-dimensional arrays

Here is some code that will print a matrix. I have omitted the details of print_tab and new_line. The procedures print_decimal and read_decimal are in the file ASM_IO.INC.

This will print the array by row.

		lea	bx,matrix
                mov	cx,row
pm_outer:	xor	si,si
		push	cx
                mov	cx,col
                xor	dx,dx
pm_inner:	mov	ax,[bx][si]
                call	print_decimal
                call	print_tab
		add	si,col_incr
                loop	pm_inner
		print	new_line
		add	bx,row_incr
                pop	cx
                loop	pm_outer

This will print the array by column. Just swap all row and col, and swap all row_incr and col_incr. (What I mean by 'printing by column' is that all of column 1 will be printed horizontally on one line, then on the next line, all of column 2 will be printed, etc.)

		lea	bx,matrix
                mov	cx,col
pm_outer:	xor	si,si
		push	cx
                mov	cx,row
                xor	dx,dx
pm_inner:	mov	ax,[bx][si]
                call	print_decimal
                call	print_tab
                add	si,row_incr
                loop	pm_inner
                call	new_line
                add	bx,col_incr
                pop	cx
                loop	pm_outer

The equates control the size of the array and how the array is stored in memory. These equates row_incr and col_incr indicate that the array is stored in row-major form.

data_size	equ	2			;array of words
row		equ	8			
col		equ	7
row_incr	equ	col*data_size		;storing in row-major
col_incr	equ	data_size
matrix		db	data_size*row*col dup(0)

To indicate the array is stored in column-major, just change row_incr and col-incr:

data_size	equ	2			;array of words
row		equ	8			
col		equ	7
row_incr	equ	data_size		;storing in col-major
col_incr	equ	row*data_size
matrix		db	data_size*row*col dup(0)