Reading and writing a 2-dimensional array

This program assumes that an array of bytes is being read and printed. It also uses full segment definitions. I will be updating this program over the weekend of 11/14/97 to include the equates that we are discussing in class. Check this file again next week.

;This program reads a 3x4 array and stores it in row-major form
;Then it displays the array by rows, then displays it by columns
;It uses prompts to display the locations of the array

          	DOSSEG
          	.MODEL    SMALL,C

	  	.DATA
;declarations for a 3 X 4 array   
DATA_SIZE	EQU	1		;array of bytes
ROW     	EQU     3
COL     	EQU     4
ARR     	DB      DATA_SIZE*ROW*COL DUP(?)
ROW_INCR	EQU	DATA_SIZE*COL
COL_INCR	EQU	DATA_SIZE
;declarations for the prompts
CR      	EQU     0DH
LF      	EQU     0AH
TAB     	EQU     09H
CRLF            DB      CR,LF,'$'
INTRO_P         DB      CR,LF,'THIS PROGRAM WILL READ A 3X4 ARRAY',CR,LF
                DB      'THE ELEMENTS WILL BE STORED BY ROW',CR,LF
                DB      'THEN IT WILL DISPLAY THE ELEMENTS BY ROW',CR,LF
                DB      'THEN IT WILL DISPLAY THE ELEMENTS BY COLUMN',CR,LF
                DB      CR,LF,CR,LF,'$'
PAUSE_ROW       DB      CR,LF,'STRIKE A KEY TO SEE ELEMENTS BY ROW...$'
PAUSE_COL       DB      CR,LF,'STRIKE A KEY TO SEE ELEMENTS BY COLUMN...$'
NUM_PROMPT      DB      CR,LF,'PLEASE ENTER A NUMBER FOR POSITION ('
ROW_NUM         DB      '1'
                DB      ', '
COL_NUM         DB      '1'
                DB      '): $'
ROW_PROMPT      DB      CR,LF,'ELEMENTS IN ROW '
RP_ROW          DB      '0'
                DB      '$'
COL_ITEM        DB      CR,LF,TAB,'COLUMN '
CI_COL          DB      '0'
                DB      ': $'
COL_PROMPT      DB      CR,LF,'ELEMENTS IN COLUMN '
CP_COL          DB      '0'
                DB      '$'
ROW_ITEM        DB      CR,LF,TAB,'ROW '
RI_ROW          DB      '0'
                DB      ': $'

	.STACK

PRINT   MACRO   NAME
        PUSH    AX
        PUSH    DX
        LEA     DX,NAME
        MOV     AH,09H
        INT     21H
        POP     DX
        POP     AX
        ENDM

PAUSE   MACRO   NAME
        PRINT   CRLF
        PRINT   NAME
        MOV     AH,01H
        INT     21H
        ENDM

	.CODE

INCLUDE ASM_IO.INC

READ_ARR        PROC    NEAR
        LEA     BX,ARR                  ;load array address 
        MOV     CX,ROW                  ;initialize outer loop counter
        MOV     ROW_NUM,'0'             ;initialize row number in prompt
READ_OUTER:                                                 
        XOR	SI,SI			;clear inner loop reference
        INC     ROW_NUM                 ;increment row number in prompt
        MOV     COL_NUM,'0'             ;initialize column number in prompt
        PUSH    CX                      ;save outer counter
        MOV     CX,COL                  ;initialize inner loop counter
READ_INNER:
        INC     COL_NUM                 ;increment column number in prompt
        PRINT   NUM_PROMPT
        CALL    READ_SIGNED            ;read word into AX
        MOV     [BX][SI],AL             ;store low byte in array
        ADD     SI,COL_INCR             ;move to next array element
        LOOP    READ_INNER              ;loop to read next number in row
        ADD	BX,ROW_INCR
        POP     CX                      ;restore outer loop counter
        LOOP    READ_OUTER              ;loop to start reading a new row
        RET
READ_ARR        ENDP

PRINT_BY_ROW    PROC    NEAR
        LEA     BX,ARR                  ;load array address
        MOV     CX,ROW                  ;init outer counter
        MOV     RP_ROW,'0'              ;init row in prompt
PBR_OUTER:
        INC     RP_ROW                  ;inc row in prompt
        PRINT   ROW_PROMPT
        PUSH    CX                      ;save outer counter
        MOV     CX,COL                  ;init inner counter
        MOV     CI_COL,'0'              ;init col number in prompt
        XOR     SI,SI                   ;clear col register
PBR_INNER:
        INC     CI_COL                  ;inc col in prompt
        PRINT   COL_ITEM
        XOR     AH,AH                   ;clear upper byte of AX
        MOV     AL,[BX][SI]             ;move array element to AX's low byte
        CALL    PRINT_SIGNED
        ADD	SI,COL_INCR             ;point to next element in row
        LOOP    PBR_INNER
        ADD     BX,ROW_INCR             ;move to start of next row
        POP     CX                      ;restore outer counter
        LOOP    PBR_OUTER               ;loop to start next row
        RET
PRINT_BY_ROW    ENDP

PRINT_BY_COL    PROC    NEAR
        LEA     BX,ARR                  ;init array address
        MOV     CX,COL                  ;init outer counter
        MOV     CP_COL,'0'              ;init col in prompt
PBC_OUTER:
        INC     CP_COL                  ;incr col in prompt
        PRINT   COL_PROMPT
        PUSH    CX                      ;save outer counter
        MOV     CX,ROW                  ;init inner counter
        MOV     RI_ROW,'0'              ;init row in prompt
        XOR     SI,SI                   ;init row register
PBC_INNER:
        INC     RI_ROW                  ;incr row in prompt
        PRINT   ROW_ITEM
        XOR     AH,AH                   ;clear high byte of AX
        MOV     AL,[BX][SI]             ;store element in AX's low byte
        CALL    PRINT_SIGNED
        ADD     SI,ROW_INCR             ;move array to next element in col
        LOOP    PBC_INNER               ;loop to print next col element
        ADD     BX,COL_INCR             ;move to start of next column
        POP     CX                      ;restore outer counter
        LOOP    PBC_OUTER               ;loop to start next column
        RET
PRINT_BY_COL    ENDP

START:  MOV       DX,@DATA      	;SET UP DS REGISTER
        MOV       DS,DX


        CALL    READ_ARR
        PAUSE   PAUSE_ROW
        CALL    PRINT_BY_ROW
        PAUSE   PAUSE_COL
        CALL    PRINT_BY_COL

        MOV       AX,4C00H            	;TERMINATE PROGRAM
        INT       21H

	END	START