;PAINT program 
IDEAL
MODEL small
STACK 100h
DATASEG

x      dw  ?
y      dw  0 
rows   db  ?
sqsz   equ 15   ;square size in pixels. 
height equ 10
yloc   equ [bp+6]
col    equ [bp+4]

color  db 0Ch  ;0Ch-red, 9- light blue, 0Ah - light green, 0Dh - light magenta, 0Eh - yellow
color1 dw 0Ah  
color2 dw 0Ch 
color3 dw 09h  
color4 dw 0Dh
color5 dw 0Eh  


CODESEG
proc paintrec
     push bp
	mov  bp,sp
	mov ax,col ; color
	mov [color],al
	mov ax,yloc 
	mov [y],ax 	 
	mov  al,[color]
	mov  [rows], height 	 ; we want height rows 
rowsloop:	 
          ; move on the Y axis 
     mov  cx,sqsz   
l1:  push cx        ; loop to draw one horizontal row 
     mov  bh,0h  
     mov  cx,[x]
     mov  dx,[y]
     inc  [x]
     mov  ah,0ch     
     int  10h
     pop  cx
     loop l1
	 
	mov [x],0      ; next row starts fro x=0 again
	inc [y]        ; Y increases for next row 
	dec [rows]
	cmp [rows],0
	ja  rowsloop   ; we count down rows from 10 down to zero 
     pop bp
	ret 4 
endp paintrec


;---------------------------------------------------------------------------


start:
     mov ax, @data
     mov ds, ax 
;------------------------------------------------------------------------------------------
; Graphic mode
     mov  ax,13h
     int  10h
	;  rec1
	xor ax,ax 
	push ax
	push [color1]
	call paintrec
	;rec2
	mov  ax,10 
	push ax
	push [color2]
	call paintrec
	;rec3
	mov  ax,20 
	push ax
	push [color3]
	call paintrec
	;rec4
	mov  ax,30 
	push ax
	push [color4]
	call paintrec
	;rec5
	mov  ax,40 
	push ax
	push [color5]
	call paintrec
	; finished color recs, now start mouse
	 
	;Init mouse
     mov  ax,0h
     int  33h
;Show mouse on screen
     mov  ax,1h
     int  33h                                         
 
 
; Loop and wait until the mouse is clicked 
; Left click to start painting.
; right click if on color ne black, pick color. On black stop program.  
; If color not picked, no painting is allowed
     
     mov [color],0 	 
mousecl:              ; main program loop 
     xor  bx,bx           
     mov  ax,3h
     int  33h
	cmp  bx,01h  
     je   paint     ; left-click
     cmp  bx,02h    ; right-click - read the pixel color                 
	je   readdot
     jmp  mousecl
              
;Print dot near mouse (sub 1 from dx)
paint:
     shr  cx,1    ; adjust cx to 0-319 range
     sub  dx,1    ; So the mouse does not block the dot (one pixel above)
     mov  bh,0h
     mov  al,[color]
	cmp  al,0
	je   mousecl 
     mov  ah,0Ch
     int  10h
	jmp  mousecl
	 
readdot:          ; pick a color 
     shr  cx,1    ; adjust cx to 0-319 range
     sub  dx,1    ; So the mouse does not block the dot (one pixel above)
     mov bh,0h
	mov ah,0Dh 
	int 10h
	mov [color],al 
	cmp al,0     ; if color black picked - get out (stop)
	jne paint 
	  
	 ; return to Text mode
stop:         
     mov  ax,3h
     int  10h 	 
	 
; Wait for key press to exit just for fun.
     mov  ah,00h  
     int  16h
     inc  bl     

; Return to text mode
     mov  ah,0
     mov  al,2
     int  10h   
;------------------ end pgm    
exit:
     mov ax, 4c00h
     int 21h
END start  