TITLE WDEX --- Variable_size video window 
;-----------------------------------------------------
dseg	segment
cury	db	0		;current row #
curx	db	0		;current column #
luy	db	10		;upper_left row #
lux	db	30		;upper_left col #
rdy	db	15		;lower_right row #
rdx	db	40		;lower_right col #
cont	db	5		;row num of window
dseg	ends
;-----------------------------------------------------
clear	macro			;clear screen macro
	mov	ah,6
	mov	al,0
	mov	ch,0
	mov	cl,0
	mov	dh,24
	mov	dl,79
	mov	bh,7
	int	10h
	endm
scroll	macro	cont		;make a window macro
	mov	ah,6
	mov	al,cont
	mov	ch,luy
	mov	cl,lux
	mov	dh,rdy
	mov	dl,rdx
	mov	bh,70h
	int	10h
	endm
postc	macro			;position cursor macro
	mov	ah,2
	mov	dh,cury
	mov	dl,curx
	mov	bh,0
	int	10h
	endm
;-----------------------------------------------------
cseg	segment
assume	cs:cseg, ds:dseg
main	proc	far
	push	ds		;save for return
	sub	ax,ax
	push	ax
	mov	ax,dseg		;set data segment addr.
	mov	ds,ax

	clear		      ;clear screen
	scroll	cont	      ;make a window in centrn
	postc		      ;position cursor at(0,0)
input:
	mov	ah,0	      ;keyboard input function
	int	16h

	cmp	ah,4bh		;is left arrow ?
	jnz	no_left		;no

	cmp	curx,0		;yes,left moving func 
	jnz	l1
	cmp	cury,0
	jnz	l3
	jmp	input
l1:	dec	curx
	jmp	l5
l3:	dec	cury
	mov	curx,79
l5:	postc	
	jmp	input		;receive next char

no_left:
	cmp	ah,4dh		;is right arrow ?
	jnz	no_right	;no

	cmp	curx,79		;yes,right moving func
	jnz	r1
	cmp	cury,24
	jnz	r3
	jmp	input
r1:	inc	curx
	jmp	r5
r3:	inc	cury
	mov	curx,0
r5:	postc
	jmp	input		;receive next char

no_right:
	cmp	ah,48h		;is up arrow ?
	jnz	no_up		;no 

	cmp	cury,0		;yes, up moving func
	jz	up1
	dec	cury
	postc
up1:	jmp	input		;recrive next char

no_up:
	cmp	ah,50h		;is down arrow ?
	jnz	no_down		;no

	cmp	cury,24		;yes,down moving func
	jz	d1
	inc	cury
	postc
d1:	jmp	input		;receive next char

no_down:
	cmp	ah,4fh		;is end key ?
	jz	setxy		;yes,cursor moving end
	cmp	ah,01		;is escape ?
	jnz	disp		;no,display char

	ret			;yes,return to DOS

disp:	mov	ah,0ah		;display a character
	mov	bh,0		;video page
	mov	cx,1		;count of repeat
	int	10h		;video ROM call
	inc	curx		;current column + 1
	mov	al,curx
	cmp	al,rdx		;boundary control
	jl	next
	scroll	1		;scroll one line
	mov	al,lux
	mov	curx,al		;current column=lux
next:	postc			
	jmp	input		;entrn next key

setxy:
	mov	al,cury		;current position 
	mov	bl,curx		;  of cursor
	cmp	al,luy		;>= upper_left row# ?
	jl	s1		;no
	cmp	bl,lux		;>= upper_left col# ?
	jl	s2		;no
	mov	rdy,al		;it is new coordinate
	mov	rdx,bl		;   of upper_left		 
	jmp	new		;make a new window
s1:
	cmp	bl,rdx		;> lower_right row # ? 
	jle	s3		;no
	jmp	input
s2:
	cmp	al,rdy		;>= right_down col # ?
	jg	s5		;no
s3:
	mov	luy,al		;it is new coordinate
	mov	lux,bl		;   oflower_right
new:
	mov	al,rdy		 
	sub	al,luy
	inc	al
	mov	cont,al		;row num of new window
	clear			;clear screen
	scroll	cont			;generate new window
s5:	jmp	input

main	endp
;-----------------------------------------------------
cseg	ends			;end of segment
	end	main		;end assembly
			
		