Population

Population(2)

1¡£The problem of human

Programming Project
The game of life is a simulation of population movement and fluctuation developed by
John Conway of Cambridge University. The simulation takes place on a grid of
locations, each of which can contain an ¡°entity¡±. Each such location has eight
¡°neighbours¡±, e.g. N, S, E, W, NE, NW, SE, and SW. The state of each ¡°generation¡± is
determined by the state of the previous one according to the following two rules:
¡¤ An entity in a cell (represented by 1) survives to the next generation if it has either
two or three neighbours; otherwise it dies.
¡¤ An empty cell (represented by 0) that has exactly three neighbours has a new
entity born in it.
For example:
Current Generation Next Generation
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 0 1 0 1 1 0
0 0 1 1 1 1 0 0 1 1 0 1 1 0
0 0 1 0 0 0 0 0 1 1 0 1 0 0
0 0 1 0 0 0 0 0 1 1 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
You are going to write a program that implements the rules of life for an area, which is
15 by 15. The project consists of three key components, to be completed in three parts:
(i) Initialize a binary (0 or 1 valued) data array of 15 by 15, and print it to the
screen for several generations.
(ii) Display each generation in graphics, in place of printings,
(iii) Enable users to input 1¡¯s and 0¡¯s in the grid cells by using the mouse.
Programming Assignment I [30 marks]
In this assignment, you are to complete part (i) of the programming project.
You must structure your program with procedures.
Specification of Part I of the project:
You will need to set up two 15 x 15 arrays, one which has been initialized with 0¡¯s and
1¡¯s will be the current grid, the other will be the grid into which you place the next
generation. After each iteration you will need to copy the new array into the old one,
print it out, and then start again. Make your program modular by writing procedures that
perform specific well-defined functions. In particular, you must write the following
procedures:
1. PrintArray:
This procedure outputs the given array D of 15x15 in ASCII characters. It prints a
¡°.¡± character instead of a 0, and a ¡°*¡± character instead of a 1.
2. CountNeighbors:
This procedure takes input of the row index and column index [r,c] in (bx, si), and
returns in AL its total number of neighbor(s) of the array element D[r,c].
For simplicity, we will assume that the cells located on the left, right, top and
bottom boarders are always empty (0).
3. NewGeneration:
For each cell (interior), call CountNeighbors, and then based on the given rules
above, decide the life of its corresponding cell in next generation. Copy all cells
of the new generation back to the cells of current generation.
Extend your program Main to do the following:
call PrintArray;
repeat 5 times
call NewGeneration;
call PrintArray;
return to DOS;

¡¡

1. DrawGrid:

Display a 15 x 15 grid in yellow. Pass on stack to the procedure the

coordinates (x, y) of the left-top corner of the grid.

Graphics mode 13h has a resolution of 320 x 200, which means 320 pixels

horizontally across the screen (x coordinate) and 200 pixels vertically

across the screen (y coordinate). Set the grid size to 120 x 120 pixels so

that each compartment is 8 x 8 pixels.

2. DrawAllElements:

The coordinates (x, y) of the left-top corner of the grid must be passed on

the stack. For each array element in memory, color using

whites the

corresponding compartment of the grid if the array element is

0, and blues

if

1.

3. DisplayArray:

Set to graphic mode. Draw the grid, and draw all the array elements. You

should see each cell with ¡°entity¡± in blue, and the empty ones in white.

In order to view your array in graphics, you may want to wait for a key

press before proceeding to the next part of your program.

Always set back to text mode before return if text inputs/outputs are

expected afterwards.

¡¡

2. My Program

    TITLE	LIFESTYLE

    DIM	    EQU 15;

    .DOSSEG
    .MODEL SMALL
    .STACK 100H
    .DATA

    DIMENSION DB DIM

    RETURNSTR   DB 0AH, 0DH, '$'
    MSG	DB "PRESS RETURN KEY TO ENTER A NEW GENERATION ", "$"

    COUNTER DB 0;
    POS DW	0;

    OLD		DB  0,0,0,0,	0,0,0,0,    0,0,0,0,	0,0,0	;0

    		DB  0,0,1,0,	1,0,0,1,    1,0,1,0,	1,0,0

    		DB  0,1,0,0,	0,1,0,1,    0,0,0,0,	0,0,0

    		DB  0,0,0,0,	0,0,0,0,    0,0,0,0,	1,0,0

    		DB  0,1,1,1,	0,1,1,1,    0,1,1,1,	0,0,0	;4

    		DB  0,1,1,1,	0,0,0,0,    0,1,1,0,	0,1,0

    		DB  0,1,1,1,	0,0,0,0,    1,1,1,0,	0,0,0

    		DB  0,0,0,0,	1,1,0,0,    1,1,1,1,	0,1,0

    		DB  0,0,0,0,	1,0,0,0,    1,1,1,1,	0,0,0	;8

    		DB  0,0,0,0,	0,1,1,0,    0,0,1,0,	0,0,0

    		DB  0,1,1,1,	0,0,0,1,    1,0,0,0,	1,1,0

    		DB  0,0,0,0,	0,0,0,0,    1,1,1,1,	1,0,0

    		DB  0,0,0,0,	1,1,1,1,    1,1,1,1,	1,1,0	;12

    		DB  0,0,0,1,	0,1,1,1,    1,0,0,0,	0,0,0

    		DB  0,0,0,0,	0,0,0,0,    0,0,0,0,	0,0,0	;14

    NEW		DB 225 DUP(?)	;0

    .CODE

    EXTRN ENTERGRAPHIC:NEAR, ENTERTEXT:NEAR, WAITRETURN:NEAR, DRAWBOX:NEAR,
          FILLGRID:NEAR, DRAWVERLINE:NEAR , DRAWHORLINE:NEAR, DRAWGRID:NEAR

    START:
    		MOV AX, @DATA
    		MOV DS, AX;

    		MOV CX, 5;

    MAINLOOP:
    		CALL SHOWMESSAGE
    		CALL WAITRETURN;
    		CALL ENTERGRAPHIC
    		MOV AX, 0
    		PUSH AX
    		MOV AX, 15;
    		PUSH AX
    		CALL DRAWALLELEMENTS;
    		CALL NEWGENERATION;
    		MOV AX, 170
    		PUSH AX
    		MOV AX, 15
    		PUSH AX
    		CALL DRAWALLELEMENTS;
    		CALL WAITRETURN;
    		CALL ENTERTEXT;

    		LOOP MAINLOOP;

      		MOV AX, 4C00H
    		INT 21H

    ;INPUT: NONE
    ;OUTPUT: NONE
    ;FUNCTION: SHOW SOME GARBAGE
    SHOWMESSAGE	PROC
    		PUSH AX
    		PUSH DX
    		MOV AH, 09H
    		MOV DX, OFFSET MSG
    		INT 21H
    		POP DX
    		POP AX
    		RET
    SHOWMESSAGE	ENDP

    ;INPUT: NONE
    ;OUTPUT: NONE
    ;FUNCTION: CHANGE NEW LINE
    CHANGELINE	PROC
    		PUSH AX
    		PUSH DX
    		MOV AH, 09H
    		MOV DX, OFFSET RETURNSTR
    		INT 21H
    		POP DX
    		POP AX
    		RET
    CHANGELINE ENDP

    ;INPUT: X,Y
    ;OUTPUT: NONE
    DRAWALLELEMENTS	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH DX
    		PUSH SI;

    		; FIRST DRAW GRID
    		MOV AX, [BP+6]; X
    		PUSH AX
    		MOV AX, [BP+4]; Y
    		PUSH AX
    		CALL DRAWGRID

    		MOV BX, OFFSET OLD; BX IS ARRAY
    		MOV CX, DIM; CX IS THE COUNTER
    		MOV DX, 0; DX IS THE ROW
    		MOV SI, 0; SI IS THE COL

    LOCALOUTER:
    		PUSH CX
    		MOV CX, DIM; INITIALIZE INNER COUNTER

    LOCALINNER:

    		;PREPAREPARAM
    		; AS THIS IS PART IS SAME PARAM NO MATTER DIE OR LIVE
    		MOV AX, [BP+6]; X
    		PUSH AX
    		MOV AX, [BP+4]; Y
    		PUSH AX
    		PUSH DX; ROW
    		PUSH SI; COL
    		MOV AX, 8; SIZE OF CELL IS 8
    		PUSH AX

    		MOV AL, [BX]; THIS IS TO SEE IF IT IS LIVE OR DIE
    		CMP AL, 1
    		JE DRAWLIFE
    DRAWDIE:
    		MOV AX, 07H; WHITE
    		PUSH AX
    		CALL FILLGRID;
    		JMP NEXTINNER
    DRAWLIFE:
    		MOV AX, 01H; COLOR BLUE
    		PUSH AX
    		CALL FILLGRID
    NEXTINNER:
    		INC SI; COL IS INC
    		INC BX; BX IS NEXT ELEMENT
    		LOOP LOCALINNER

    		;OUTER LOOP
    		MOV SI, 0; NEW COL START FROM 0
    		INC DX; NEXT ROW
    		POP CX; RESTORE OUTER COUNTER
    		LOOP LOCALOUTER

    		POP SI
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 4;
    DRAWALLELEMENTS	ENDP

    COUNTNEIGHBORS	PROC	NEAR

    		PUSH	CX;
    		PUSH	DX;
    		MOV DX, OFFSET OLD; DX HOLD ARRAY OLD

    		MOV AX, BX;	COPY ROW TO AX TO DO MUL
    		MUL DIMENSION
    		ADD AX, SI; NOW AX HOLDS ARRAY INDEX
    		MOV POS, AX; NOW POS HOLD INDEX DURING LOOPING
    		MOV AX, BX; NOW AX HOLDS BX
    		MOV COUNTER, 0; RECORD NEIGHBORS

    UPLEFT:
    		CMP AX, 0; CHECK IF IT IS FIRST ROW
    		JE  UP;
      		CMP SI, 0; CHECK IF IT IS FIRST COL
    		JE  UP;
    		MOV BX, POS
    		SUB BX, 16
    		ADD BX, DX  ;BX IS POINTING TO ELEMENT OF ARRAY
    		MOV CL, [BX];
    		ADD COUNTER,CL
    UP:
    		CMP AX, 0; CHECK IF IT IS FIRST ROW
    		JE UPRIGHT;
    		MOV BX, POS
    		SUB BX, 15
    		ADD BX, DX  ;BX IS POINTING TO ELEMENT OF ARRAY
    		MOV CL, [BX];
    		ADD COUNTER,CL
    UPRIGHT:
    		CMP AX, 0; CHECK IF IT IS FIRST ROW
    		JE  RIGHT;
    		CMP SI, 14; CHECK IF IT IS LAST COL
    		JE RIGHT;
    		MOV BX, POS
    		SUB BX, 14
    		ADD BX, DX
    		MOV CL, [BX];
    		ADD COUNTER,CL
    RIGHT:
    		CMP SI, 14
    		JE  DOWNRIGHT
    		MOV BX, POS
    		ADD BX, 1
       		ADD BX, DX
    		MOV CL, [BX];
      		ADD COUNTER,CL

    DOWNRIGHT:
    		CMP SI, 14
    		JE DOWN
    		CMP AX, 14
    		JE DOWN
      		MOV BX, POS
      		ADD BX, 16
      		ADD BX, DX
      		MOV CL, [BX];
    		ADD COUNTER,CL

    DOWN:
    		CMP AX, 14
    		JE  DOWNLEFT
       		MOV BX, POS
      		ADD BX, 15
       		ADD BX, DX
       		MOV CL, [BX];
       		ADD COUNTER,CL

    DOWNLEFT:
      		CMP AX, 14
       		JE LEFT
      		CMP SI, 0
    		JE LEFT
    		MOV BX, POS
       		ADD BX, 14
      		ADD BX, DX
    		MOV CL, [BX];
    		ADD COUNTER,CL

    LEFT:
    		CMP SI, 0
      		JE ENDDIR
      		MOV BX, POS
      		SUB BX, 1
      		ADD BX, DX
    		MOV CL, [BX];
    		ADD COUNTER,CL
    ENDDIR:
    		MOV BX, AX
       		MOV AX, 0
    		MOV AL, COUNTER
    		POP DX
    		POP CX

    		RET
    COUNTNEIGHBORS	ENDP

    NEWGENERATION PROC NEAR
      		PUSH AX
      		PUSH BX
    		PUSH CX
    		PUSH DX
    		PUSH SI
      		PUSH DI
    		PUSH BP

      		MOV BP, OFFSET OLD; BP HOLDS OLD ARRAY
       		MOV DX, OFFSET NEW; DX HOLDS NEW ARRAY
    		MOV BX, 1; BX IS ROW COUNTER OR OUTLOOP COUNTER

    OUTLOOP:
    		CMP BX, 14
    		JE  ENDALLLOOP
    		MOV SI, 1; SI IS COL COUNTER OR INNERLOOP COUNTER

    INLOOP:
    		CMP SI, 14
    		JE ENDOUTLOOP
    		XOR AX, AX;
    		CALL COUNTNEIGHBORS
    		MOV COUNTER, AL; COUNTER HOLDS NUM OF NEIGHBORS
    		MOV AX, BX; COPY ROW TO AX TO DO MUL
    		MUL DIMENSION
    		ADD AX,  SI	   ; AX HOLDS INDEX OF ARRAY
    		MOV DI, AX; COPY INDEX OF ARRAY TO DI
    		ADD DI, BP; DI IS POINTING TO ELEMENT OF ARRAY OLD
    		MOV CL, [DI]; USE BP TO DO COM NEXT
    		CMP CL, 0
    		JE  TOBORN; IF THERE IS NO LIFE PREVIOUSLY
    TODIE:
    		CMP COUNTER, 2
    		JE  LIFE
    		CMP COUNTER, 3
    		JE  LIFE
    		JMP DIE;
    TOBORN:
    		CMP COUNTER, 3
    		JE  LIFE
    		JMP DIE

    LIFE:
    		MOV DI, AX; DI HOLDS INDEX OF ARRAY OF NEW
    		ADD DI, DX; DI POINTING TO ELEMENT OF ARRAY OF NEW
    		MOV CL, 1;
    		MOV [DI], CL;
    		JMP ENDINLOOP
    DIE:
    		MOV DI, AX;
    		ADD DI, DX
    		MOV CL, 0;
    		MOV [DI],CL

    ENDINLOOP:
    		INC SI;
    		JMP INLOOP
    ENDOUTLOOP:
    		INC BX;
    		JMP OUTLOOP;
    ENDALLLOOP:
      		MOV BX, 1;

    ROWLOOP:
    		CMP BX, 14
    		JE ENDCOPY;
       		MOV SI, 1
    COLLOOP:
    		CMP SI, 14
    		JE ENDROWLOOP
    		MOV AX, BX; PREPARE MUL
    		MUL DIMENSION
    		ADD AX, SI; AX HOLDS THE INDEX OF ARRAY
    		MOV DI, DX;
    		ADD DI, AX; DI POINTINT TO ELEMENT OF ARRAY NEW
    		MOV CL, [DI]; BP HOLDS VALUE OF NEW
    		MOV DI, BP;
    		ADD DI, AX; DI PINTING TO ELEMENT OF ARRAY OLD
    		MOV [DI], CL; COPY VALUE TO OLD ARRAY

    		INC SI;
    		JMP COLLOOP;
    ENDROWLOOP:
    		INC BX;
    		JMP ROWLOOP;

    ENDCOPY:
    		POP BP
    		POP DI
    		POP SI
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		RET

    NEWGENERATION	ENDP;

    PRINTARRAY  PROC    NEAR
    		PUSH	AX;
    		PUSH	BX;
    		PUSH	CX;
    		PUSH	DX;

    		MOV BX, OFFSET	OLD ;BX IS ARRAY OLD
       		MOV CX, 0;  CX IS INNER COUNTER
    		MOV COUNTER, 0;
    OUTERLOOP:
    		CMP COUNTER, DIM
    		JE  ENDLOOP ;
    		MOV CX, 0;	INITIALIZE INNER COUNTER TO 0
    INNERLOOP:
    		CMP CX, DIM;
    		JE  ENDOUTER;
    		MOV AL, [BX]
    		CMP AL, 0
    		JE  PRINTDOT
    PRINTSTAR:
    		MOV DL, '*'
    		JMP DOPRINTING;
    PRINTDOT:
    		MOV DL, '.'

    DOPRINTING:
    		MOV AH, 02H
    		INT 21H

    PRINTSPACE:
    		MOV DL, 32
    		MOV AH, 02H
    		INT 21H

    ENDINNER:
    		INC BX;
    		INC CX;

    		JMP INNERLOOP;
    ENDOUTER:
    		MOV AH, 09H
    		MOV DX, OFFSET RETURNSTR;
    		INT 21H
    		INC COUNTER;
    		JMP OUTERLOOP;

    ENDLOOP:
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		RET
    PRINTARRAY ENDP

    END START

¡¡

¡¡

¡¡

3. This is my library

¡¡

¡¡

    TITLE  MYLIB
    COMMENT #	THIS IS MY LIBRARY ALTHOUGH MANY FUNCTIONS ARE JUST QUITE SAME AS
    		# EXAMPLE GIVEN IN GRAPHIC, BUT I PREFER TO WRITE IT AGAIN BY MYSELF

    .DOSSEG
    .MODEL SMALL
    .STACK 100H
    .DATA

    .CODE

    PUBLIC ENTERGRAPHIC
    PUBLIC ENTERTEXT
    PUBLIC WAITRETURN
    PUBLIC DRAWBOX
    PUBLIC DRAWVERLINE
    PUBLIC DRAWHORLINE
    PUBLIC DRAWGRID
    PUBLIC FILLGRID

    ; INPUT: NONE
    ; OUTPUT: NONE
    ; FUNCTION: CHANGE TO GRAPHIC MODE
    ENTERGRAPHIC PROC
    		PUSH AX
    		MOV AX, 0A000H
    		MOV ES, AX
    		MOV AH, 00H
    		MOV AL, 13H
    		INT 10H
    		;CALL REFRESH
    		POP AX
    		RET
    ENTERGRAPHIC ENDP

    ;INPUT X, Y, ROW, COL, WIDTH, COLOR
    INTERNALDRAWGRID	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH DX
    		PUSH SI
    		PUSH DI

    		MOV AX, [BP+12]; Y
    		MOV BX, 320
    		MUL BX
    		ADD AX, [BP+14]; +X
    		MOV BX, AX; BX HOLDS THE OFFSET
    		MOV SI, [BP+10]; THE ROW
    		MOV DI, [BP+8]; THE COL

    		MOV AX, [BP+6]; THE GRIDLENGTH
    		MUL SI; CAL ROW LENGTH
    		ADD SI, AX; PLUS THE ROW LINE WIDTH
    		INC SI; ONE MORE LINE FOR ROW, AS 15 ROW HAS 16 LINE, SI IS THE HEIGHT

    		MOV AX, [BP+6]; GRIDLENGTH TO PREPARE TO CAL COL LENGTH
    		MUL DI; CAL COL LEN
    		ADD DI, AX; DI IS LENGTH OF COL
    		INC DI; ONE MORE LINE, DI IS THE WIDTH OF GRID IN TOTAL

    		MOV DX, [BP+4]; COLOR
    		MOV CX, [BP+10]; THE ROW NUMBER
    		INC CX; ONE MORE LINE
    		PUSH BX

    ROWLOOP:
    		PUSH CX; SAVE CX FOR INNER LOOP
    		PUSH BX; SAVE OFFSET
    		MOV CX, DI; THE WIDTH OF EACH ROW LINE
    HORLINELOOP:
    		MOV ES:[BX], DL
    		INC BX
    		LOOP HORLINELOOP
    		POP BX; RESTORE OFFSET

    		; CAL THE OFFSET OF NEXT ROW
    		MOV AX, [BP+6]; THE LENGTH OF EACH GRID
    		INC AX; THE DIFFERENCE OF LINE OF NEXT ROW
    		PUSH DX
    		MOV DX, 320
    		MUL DX
    		POP DX

    		ADD BX, AX; CHANGE LINE
    		POP CX; RESTORE THE ROW COUNTER
    		LOOP ROWLOOP

    		POP BX; THIS IS THE ORIGINAL OFFSET FOR COL USE
    		MOV CX, [BP+8]; THE COL NUMBER
    		INC CX; ONE MORE LINE
    COLLOOP:
    		PUSH CX
    		PUSH BX
    		MOV CX, SI; THE HEIGHT OF TOTAL GRID
    VERLINELOOP:
    		MOV ES:[BX], DL
    		ADD BX, 320
    		LOOP VERLINELOOP
    		POP BX
    		POP CX
    		; NEXT COL IS THE LENGTH OF GRID
    		MOV AX, [BP+6]; THE LENGTH OF GRID
    		INC AX; ONE MORE LINE
    		ADD BX, AX
    		LOOP COLLOOP;

    		POP DI
    		POP SI
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 12
    INTERNALDRAWGRID	ENDP

    ;INPUT ROW, COL, SIZE OF EACH CELL
    ;OUT PUT X, Y OFFSET BY STACK
    CALCOFFSET  PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		MOV  CX, [BP+4]; THE SIZE OF CELL
    		INC CX; THE DIFFERENCE BTW TWO CELL

    		MOV AX, [BP+8]; THE ROW
    		MUL CX; EACH ROW OFFSET
    		MOV BX, AX; SAVE IN BX THE Y OFFSET

    		MOV AX, [BP+6]; THE COL
    		MUL CX; THE X DIFFERENCE
    		MOV [BP+8], AX; SAVE THE X
    		MOV [BP+6], BX; SAVE THE Y
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 2
    CALCOFFSET  ENDP

    REFRESH	PROC
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		MOV AX, 00H
    		MOV CX, 64000
    		MOV BX, 0
    LOCAL1:
    		MOV ES:[BX], AL
    		INC BX
    		LOOP LOCAL1
    		POP CX
    		POP BX
    		POP AX
    		RET
    REFRESH   ENDP

    ;INPUT: X,Y,ROW,COL,SIZE OF CELL,COLOR
    FILLGRID	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH DX
    		MOV CX, [BP+6]; THE SIZE OF CELL

    		MOV AX, [BP+10]; ROW INDEX
    		PUSH AX; PREPARE PARAM FOR "CALCOFFSET" CALL
    		MOV AX, [BP+8]; THE COL INDEX
    		PUSH AX
    		PUSH CX; PASS THE SIZE
    		CALL CALCOFFSET
    		POP BX; IT IS Y OFFSET
    		ADD BX, [BP+12]; PLUS ORIGINAL Y
    		POP AX; THE NEW X
    		ADD AX, [BP+14]; PLUS ORIGINAL X
    		MOV DX, [BP+4]; THE COLOR
    		INC AX
    		INC BX
    		PUSH AX; X
    		PUSH BX; Y
    		PUSH CX; THE SIZE OF BOX
    		PUSH DX; THE COLOR OF BOX
    		CALL DRAWBOX
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 12
    FILLGRID	ENDP

    ; INPUT X,Y
    DRAWGRID	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		MOV AX, [BP+6]; THE X
    		PUSH AX
    		MOV AX, [BP+4]; THE Y
    		PUSH AX
    		MOV AX, 15
    		PUSH AX
    		PUSH AX
    		MOV AX, 8
    		PUSH AX
    		MOV AX, 0EH; THE COLOR OF YELLOW
    		PUSH AX
    		CALL INTERNALDRAWGRID
    		POP AX
    		POP BP
    		RET 4;
    DRAWGRID ENDP

    ;INPUT: X,Y,LENGTH,COLOR BY STACK
    ;OUTPUT: NONE
    ;FUNCTION: DRAW HORIZONTAL LINE WITH STARTING COORDINATE, LENGTH, COLOR,
    DRAWHORLINE	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH ES
    		MOV AX, 0A000H
    		MOV ES, AX
    		MOV AX, [BP+8]; THE Y
    		MOV BX, 320
    		MUL BX
    		ADD AX, [BP+10]; X
    		MOV BX, AX; BX HOLDS THE OFFSET ORIGIN
    		MOV CX, [BP+6]; THE LENGTH
    		MOV AX, [BP+4]
    HLINE:
    		MOV ES:[BX], AL; DRAW THE LINE
    		INC BX
    		LOOP HLINE
    		POP ES
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 8
    DRAWHORLINE	ENDP

    DRAWVERLINE	PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH ES
    		MOV AX, [BP+8]; THE Y
    		MOV BX, 320
    		MUL BX
    		ADD AX, [BP+10]; ADD THE X
    		MOV BX, AX; BX HOLDS THE OFFSET
    		MOV CX, [BP+6]; THE LENGTH
    		MOV AX, [BP+4]; THE COLOR
    VLINE:
    		MOV ES:[BX], AL
    		ADD BX, 320
    		LOOP VLINE
    		POP ES
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 8
    DRAWVERLINE	ENDP

    ENTERTEXT PROC
    		PUSH AX
    		MOV AH, 00H
    		MOV AL, 03H
    		INT 10H
    		MOV AX, 0B800H
    		MOV ES, AX
    		POP AX
    		RET
    ENTERTEXT ENDP

    ;INPUT: NONE
    ;OUTPUT: NONE
    ;FUNCTION SIMPLY WAIT FOR ANY KEY TO BE PRESSED
    WAITRETURN  PROC
    		PUSH AX
    		XOR AX, AX
    		MOV AH, 01H
    CHECK:
    		INT 21H
    		CMP AL, 0DH
    		JNE CHECK
    		POP AX
    		RET
    WAITRETURN  ENDP

    ;INPUT: X, Y, LENGTH, COLOR BY STACK
    ;OUTPUT: NONE
    ;FUNCTION: DRAW A BOX OF WHICH THE LEFTTOP HAS COORDINATE OF (X,Y),LENGTH, COLOR
    ;SAVE ES INTERNALLY,
    DRAWBOX     PROC
    		PUSH BP
    		MOV BP, SP
    		PUSH AX
    		PUSH BX
    		PUSH CX
    		PUSH DX
    		PUSH ES
    		MOV AX, 0A000H
    		MOV ES, AX
    		MOV BX, [BP+10]; X
    		MOV AX, [BP+8]; Y

    		MOV CX, 320
    		MUL CX
    		ADD BX, AX; BX IS OFFSET=320* Y + X
    		MOV AX, [BP+4]; COLOR
    		MOV CX, [BP+6]; R
    		MOV DX, CX ;DX SAVE LENGTH

    BOXLOOP:
    		PUSH CX; SAVE OUTTER COUNTER
    		PUSH BX; SAVE OFFSET
    		MOV CX, DX; NEW START
    SINGLELOOP:
    		MOV ES:[BX], AL
    		INC BX
    		LOOP SINGLELOOP
    		POP BX
    		ADD BX, 320;NEXT LINE
    		POP CX
    		LOOP BOXLOOP
    		POP ES
    		POP DX
    		POP CX
    		POP BX
    		POP AX
    		POP BP
    		RET 8
    DRAWBOX     ENDP

    END

¡¡