delay	macro	
	local	retry,_end,ah_retry,_end_ah,bl_retry,bl_end,bh_retry,bh_end

	mov	al,0
	mov	al,01h
retry:	
	cmp	al,0
	jz	_end

	mov	ah,10h
ah_retry:
	cmp	ah,0
	jz	_end_ah

	mov	bl,0ffh
bl_retry:
	cmp	bl,0
	jz	bl_end

	mov	bh,0ffh
bh_retry:
	cmp	bh,0
	jz	bh_end
	dec	bh
	jmp	bh_retry

bh_end:
	dec	bl
	jmp	bl_retry
bl_end:

	dec	ah
	jmp	ah_retry
_end_ah:
	dec	al
	jmp	retry

_end:
		endm

delay2	macro	
	local	retry,_end,ah_retry,_end_ah,bl_retry,bl_end,bh_retry,bh_end

	mov	al,0
	mov	al,01h
retry:	
	cmp	al,0
	jz	_end

	mov	ah,01h
ah_retry:
	cmp	ah,0
	jz	_end_ah

	mov	bl,0ffh
bl_retry:
	cmp	bl,0
	jz	bl_end

	mov	bh,0ffh
bh_retry:
	cmp	bh,0
	jz	bh_end
	dec	bh
	jmp	bh_retry

bh_end:
	dec	bl
	jmp	bl_retry
bl_end:

	dec	ah
	jmp	ah_retry
_end_ah:
	dec	al
	jmp	retry

_end:
		endm

print	macro	buffer
	mov	ah,09h
	lea	dx,buffer
	int	21h
	endm

get_key	macro
	mov	ah,0h
	int	16h
	endm

get_string	macro buffer
	mov	ax,@data
	mov	ds,ax
	mov	dx,offset buffer
	mov	ah,0ah
	int	21h
	;asc_to_hex
	endm
putchar	macro	char
	mov	dl,char
	mov	ah,02h
	int	21h
	endm

Clear_LCM	Macro
;Clear display Function
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0d8h			;bit3=RS
	out	dx,al
	delay
	mov	dx,DataPort
	mov	al,38h			;set function 2line 8bits 10dots
	out	dx,al	
	delay
	mov	dx,DataPort		;bit1=back light 
	inc	dx
	inc	dx
	mov	al,0dah
	out	dx,al
	delay
	mov	dx,DataPort		;back dark
	inc	dx
	inc	dx
	mov	al,0d8h
	out	dx,al
	delay
	mov	dx,DataPort		;display on
	mov	al,0ch
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0dah
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0d8h
	out	dx,al
	delay
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0dah
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0d8h
	out	dx,al
	delay
	delay
	mov	dx,DataPort		;write data
	inc	dx
	inc	dx
	mov	al,0d1h
	out	dx,al
	delay
	delay
;For STB ( backlight)
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0d1h
	out	dx,al
	delay
	endm

Show_data	Macro
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c8h
	out	dx,al
	delay
	mov	dx,DataPort
	mov	al,38h			;set function
	out	dx,al	
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0cah
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c8h
	out	dx,al
	delay
	mov	dx,DataPort		;display on
	mov	al,0fh
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0cah
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c8h
	out	dx,al
	delay
	mov	dx,DataPort	       ;clear display
	mov	al,01h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0cah
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c8h
	out	dx,al
	delay
	mov	dx,DataPort		;write data
	inc	dx
	inc	dx
	mov	al,0c0h
	out	dx,al
	delay
	endm

DTA	equ	80h		;PSP start address

public	argv,argc		
	
.model	small
.386
.code
main:
	mov	ax,@data
	mov	es,ax	;es->.data , ds-> PSP

	cld				
	mov	cx,80h			;only copy psp 80bytes to dta_copy
	mov	si,dta			;the PSP start address
	mov	di,offset dta_copy	;offset the .data dta_copy
main10: movsb				;mov data
	loop	main10
	push	es			
	pop	ds			;ds->.data
	mov	di,00h			
	mov	si,offset dta_copy
	call	parse_command		;process the argc
	print	title_m
	print 	cr_lf
	cmp	argc,0			;not input any value so exit
	je	in_err2	
;---------detect LPT port---------------------------------
	mov	dx,dataport3
	in	al,dx
	cmp	al,0ffh
	jne	scan_end3
	mov	dx,dataport2
	in	al,dx
	cmp	al,0ffh
	jne	scan_end2
	mov	dataport,dataport1
	jmp	go_on
scan_end3:
	mov	dataport,dataport3
	jmp	go_on
scan_end2:
	mov	dataport,dataport2
go_on:
	clear_lcm
;-----------------translat str1---------------------------------------	
	mov	cx,00
	mov	si,offset dta_copy
	add	si,3
	mov	di,offset string1
	cld
main11: mov	al,[si]
	mov	[di],al
	inc	si
	inc	di
	inc	cx
	cmp	al,22h
	jne	main11
	cmp	cx,21
	ja	in_err
;--------------start show string1 -------------------------------------------
	push	cx		;actual string length
	dec	cx		
	Show_data
	mov	si,offset string1
show1:	
	mov	dx,DataPort	       
	mov	al,[si]
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c2h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c0h
	out	dx,al
	delay
	inc	si
	cmp	cx,0
	dec	cx
	jne	show1
	delay
;---------add space for last---------------------------------------------
	pop	ax		;actual string length
	push	ax		;actual length push to stack
	dec	ax
	mov	cx,40
	sub	cx,ax
show11:	
	mov	dx,DataPort	       
	mov	al,20h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c2h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c0h
	out	dx,al
	delay
	cmp	cx,0
	dec	cx
	jne	show11
	delay
;------show end---------------------------------------------
comment ~
 	pop	cx			;actual lenth
	push	cx
	dec	cx
	mov	si,offset string1
pp:	mov	al,[si]
	putchar	al
	inc	si
	loop	pp
	print	cr_lf
end-comment~
	cmp	argc,1
	je	_exit
;--------start translate str2---------------------------------------------	
	mov	cx,0
	mov	si,offset dta_copy
	pop	ax		;string 1 actual lenth
	add	ax,5
	add	si,ax
	mov	di,offset string2
main12: mov	al,[si]
	mov	[di],al
	inc	si
	inc	di
	inc	cx
	cmp	al,22h
	jne	main12
	cmp	cx,21
	ja	in_err1

;--------------start show string2 -------------------------------------------
	push	cx			;string 2 actual lenth
	dec	cx
	mov	si,offset string2
show2:	
	mov	dx,DataPort	       
	mov	al,[si]
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c2h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c0h
	out	dx,al
	delay
	inc	si
	cmp	cx,0
	dec	cx
	jne	show2
	delay
;---------add space for last---------------------------------------------
	pop	ax
	push	ax
	mov	cx,40
	sub	cx,ax
show22:	
	mov	dx,DataPort	       
	mov	al,20h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c2h
	out	dx,al
	delay
	mov	dx,DataPort
	inc	dx
	inc	dx
	mov	al,0c0h
	out	dx,al
	delay
	cmp	cx,0
	dec	cx
	jne	show22
	delay	     
;------show end---------------------------------------------
comment ~
	pop	cx
	dec	cx
	mov	si,offset string2
pp1:	mov	al,[si]
	putchar	al
	inc	si
	loop	pp1
	print	cr_lf
end-comment~
	jmp	_exit

in_err2:	
	print	message91		;not input
        jmp	_exit
in_err:	
	print	message90		;input error
        jmp	_exit
in_err1:	
	print	message901		;input error
        jmp	_exit
_exit:
        mov	ah,4ch
        int	21h

	public	parse_command		;The refix argv[]
first	db	?
parse_command	proc	near
	push	ax
	push	cx
	mov	argc,00h
	mov	cl,[si]
	xor	ch,ch
	cmp	cx,0
	je	exit1
	inc	si
	mov	al,' '
scan:	cmp	al,[si]
	jne	next
	mov	byte ptr [si],20h
	inc	si
	mov	first,1
	loop	scan
	jmp	exit1
next:	cmp	first,1
	jne	next1
	mov	first,0
	mov	argv[di],si
	inc	di
	inc	di
	inc	argc
next1:	inc	si
	loop	scan
	mov	byte ptr[si],20h
exit1:	pop	cx
	pop	ax
	ret
parse_command	endp


.data
	title_m		db	'LCM Display String tool .....Made by Mark(2003/01/20)$'
	CR_LF		db	0ah,0dh,'$'
	message90	db	'Input Error String 1 !!!$'
	message901	db	'Input Error String 2 !!!$'
	message91	db	'Usage:',0dh,0ah
			db	'LCM_ASM "12345678901234567890" "ABCDEFGHIJKLMNOPQRST" $'
	string1		db	40 dup(?)
	string2		db	40 dup(?)

	dta_copy	db	80h dup(?)
	argv		dw	40h dup(?)
	argc		dw	?

	DataPort1 	equ 378h
	DataPort2 	equ 278h
	DataPort3 	equ 3BCh
	dataport	dw	?
.stack	200h
end     main



