TITLE Programming Project 2 (COP 3402, Spring 2002) Comment ! This program displays a 3 x 3 grid containing 9 red rectangles. Each rectangle is 9 columns wide and 5 rows high. The grid is centered on the screen horizontally. The following loop executes: 1. The user is prompted for an integer (0-8), which specifies the desired location of the blue square. Values 0-2 are the top row, values 3-5 are the middle row, and values 6-8 are the bottom row. 2. The grid is redrawn with the blue square in the desired position. 3. If the user enters a value outside the range 0-8, the program clears the screen and exits. No external library procedures are called. Suggested reading: Chapters 5 & 6. Demo program by Irvine, 2/22/02. ! INCLUDE Irvine16.inc LEFT_OFFSET = 27 ; distance to left side of screen COLOR1 = 10011100b ; light red on lightblue COLOR2 = 11001001b ; lightblue on lightred .data selectSquare DW 999 ; index into board array board LABEL WORD ; UL corner, LR corner, color DW 0000h,0408h ; top row RECT_SIZE = ($-board) DW 0009h,0411h DW 0012h,041Ah DW 0500h,0908h ; second row DW 0509h,0911h DW 0512h,091Ah DW 0A00h,0E08h ; third row DW 0A09h,0E11h DW 0A12h,0E1Ah .code main PROC mov ax,@data mov ds,ax L1: call ClearScreen call DrawBoard call AskSquare mov ax,selectSquare .IF ax < 0 || ax > 8 jmp quit .ENDIF jmp L1 quit: call ClearScreen exit main ENDP ;----------------------------------------------------------- DrawBoard PROC ; ; Draw the board, with all 9 squares. Draw the selected ; square in the contrasting color (COLOR2). ; Receives: nothing ; Returns:nothing ;----------------------------------------------------------- pusha mov si,offset board mov cx,9 ; number of squares mov di,0 ; rectangle number L1: push cx ; save loop counter ; select the rectangle color .IF di == selectSquare mov bh,COLOR2 .ELSE mov bh,COLOR1 .ENDIF ; scroll the rectangle mov cx,[si] ; upper left corner add cl,LEFT_OFFSET mov dx,[si+2] ; lower right corner add dl,LEFT_OFFSET mov ax,0600h ; scroll all lines int 10h ; scroll now add si,RECT_SIZE ; next rectangle offset inc di ; next rectangle number pop cx ; restore loop counter Loop L1 popa ret DrawBoard ENDP ;-------------------------------------------------------- AskSquare PROC ; ; Ask the user to select a square by number. This value will ; be used to select the position of the black square. ; Receives: nothing ; Returns: sets the value of selectSquare (a variable) ;-------------------------------------------------------- .data strAskSquare DB "(YOUR NAME SHOULD APPEAR HERE)",0dh,0ah DB "Enter square number [0-8] or any other value to quit: $" .code pusha mov ah,2 ; locate cursor mov bh,0 mov dh,23 ; row mov dl,0 ; column int 10h mov ah,9 ; display string mov dx,offset strAskSquare int 21h mov ah,1 ; character input int 21h ; into AL .IF al < '0' || al > '8' mov selectSquare,999 ; force program to exit .ELSE and al,0Fh ; convert ASCII digit to binary mov ah,0 ; zero the upper half of AX mov selectSquare,ax ; save the number .ENDIF popa ret AskSquare ENDP ;-------------------------------------- ClearScreen PROC ; ; Clear the screen. ; Receives: nothing ; Returns: nothing ;-------------------------------------- pusha mov ax,0600h ; scroll entire screen mov cx,0 mov dx,1B4Fh mov bh,7 int 10h mov ah,2 ; cursor at 0,0 mov dx,0 mov bh,0 int 10h popa ret ClearScreen ENDP END main