	.286
_DATA	segment	word public 'DATA'
	_vdu_seg	dw	0B000h
	MAX_ROW	DW	5,5,5,5,13,13,13,13,21,21,21,21
	MAX_COL	DW	18,38,58,78,18,38,58,78,18,38,58,78
	DEF_COL	DW	1,21,41,61,1,21,41,61,1,21,41,61
	DEF_ROW	DW	1,1,1,1,9,9,9,9,17,17,17,17
	DATAROW	DW	1,1,1,1,9,9,9,9,17,17,17,17
	DATACOL	DW	1,21,41,61,1,21,41,61,1,21,41,61
_DATA	ends

_BSS	segment	word public 'BSS'
_BSS	ends

DGROUP	group	_DATA,_BSS

_TEXT	segment	byte public 'CODE'
	assume  cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP

	public	_video_mode
_video_mode	proc	near
	int	11h
	and	ax,30h
	cmp	ax,20h
	jz	is_color
	cmp	ax,30h
	jz	is_mono
	xor	ax,ax
	ret
is_color:
	mov	_vdu_seg,0B800h
	mov	ax,1
	ret
is_mono:
	mov	_vdu_seg,0B000h
	mov	ax,1
	ret
_video_mode	endp

	public	_mem_putc
_mem_putc	proc	near
; [bp+4] = *str
; [bp+6] = attr
; [bp+8] = long offset
	push	bp
	mov	bp,sp
	push	di

	mov	ax,_vdu_seg
	mov	es,ax
	mov	di,word ptr [bp+8]
	mov	al,byte ptr [bp+4]
	mov	ah,byte ptr [bp+6]
	stosw

	pop	di
	pop	bp
	ret
_mem_putc	endp

	public	_mem_puts
_mem_puts	proc	near
; [bp+4] = *str
; [bp+6] = attr
; [bp+8] = long offset
	push	bp
	mov	bp,sp
	push	si
	push	di

	mov	si,[bp+4]
	mov	di,[bp+8]
	mov	bl,byte ptr [bp+6]
	mov	ax,_vdu_seg
	mov	es,ax
mpt00:
	lodsb
	or	al,al
	jz	mpt02
	mov	ah,bl
	stosw
	jmp	mpt00
mpt02:
	pop	di
	pop	si
	pop	bp
	ret
_mem_puts	endp

	public	_con_outs
_con_outs	proc	near
; [bp+4] = box
; [bp+6] = *str
	push	bp
	mov	bp,sp
	push	si
	push	di

	mov	si,[bp+6]
	mov	bx,[bp+4]
	shl	bx,1
	mov	ax,DATAROW[bx]
	mov	dx,ax
	push	dx
	mov	cx,DATACOL[bx]
	mov	dl,160
	mul	dl
	mov	di,ax
	shl	cx,1
	add	di,cx
	shr	cx,1
	pop	dx

	mov	ax,_vdu_seg
	mov	es,ax
	mov	ah,0Fh
con_loop:
	lodsb
	or	al,al
	jz	end_con
	stosw
	inc	cx
	cmp	cx,MAX_COL[bx]
	jbe	con_loop
	mov	cx,DEF_COL[bx]
	inc	dx
	cmp	dx,MAX_ROW[bx]
	jbe	con_next
	mov	dx,DEF_ROW[bx]
con_next:
	push	dx
	mov	ax,dx
	mov	dl,160
	mul	dl
	mov	di,ax
	shl	cx,1
	add	di,cx
	shr	cx,1
	mov	ah,0Fh
	pop	dx
	jmp	short con_loop

end_con:
	mov	DATAROW[bx],dx
	mov	DATACOL[bx],cx
	pop	di
	pop	si
	pop	bp
	ret
_con_outs	endp

	public	_clear_screen
_clear_screen	proc	near
	push	di

	mov	ax,_vdu_seg
	mov	es,ax
	mov	ax,0720h
	mov	cx,800h
	xor	di,di
	cld
	rep	stosw

	pop	di
	ret
_clear_screen	endp

	public	_read_key
_read_key	proc	near
	sti
	mov	ah,1
	int	16h
	jz	key_ret		; ZF=1 No Code Available
	mov	ah,0		; read out the key
	int	16h
	ret			; ZF=0 Code Is Ax
key_ret:
	xor	ax,ax
	ret
_read_key	endp

	public	_w_scrol
_w_scrol	proc	near
	enter	word ptr 0,0
	mov	ch,byte ptr[bp+4]		; row_1
	mov	cl,byte ptr[bp+6]		; col_1
	mov	dh,byte ptr[bp+8]		; row_2
	mov	dl,byte ptr[bp+10]		; col_2
	mov	al,1
	mov	ah,06
	mov	bh,07
	int	10h
	leave
	ret
_w_scrol	endp

	public	_set_cursor
_set_cursor	proc	near
	push	bp
	mov	bp,sp

	mov	dh,byte ptr [bp+4]
	mov	dl,byte ptr [bp+6]
	xor	bx,bx
	mov	ah,02
	int	10h

	pop	bp
	ret
_set_cursor	endp

_TEXT	ends
	end
