org	100h
		jmp begin
lf		equ 0ah
cr		equ 0dh
blank		equ 20h
point		equ 2eh
fdos		equ 05h
directory	equ 80h
print$char	equ 02h
print$cons	equ 09h
read$cons	equ 10
match$1st	equ 17
match$next	equ 18
renamef		equ 23
disk$reset	equ 13
input$buffer:	db  32
		ds  32
pwend		equ  $
fcb:		db  0,'???????????'
		dw  0,0,0,0,0,0,0,0,0,0,0
zeile:		db 12
epson$buffer:	db 0dh,1bh,30h,0fh,0dh,'$'
fcb$4:		db 4
fcb$count:	db  0
fcb$location:	dw  fcb$buffer
file$count:	db  0
file$total:	db  0
old$sp:		dw  0
stack:		ds  64
stack$top	equ $
m1buffer:	db  '       Label-Druck f}r Disc',27h,'s$'
m2buffer:	db  '       Welches Laufwerk bitte >$'
m4buffer:	db  12,'     R E A D Y .$'
mxbuffer:	db  0dh,0ah,'     Ich arbeite, Bitte warten!$'
cont:		db  '                $'
crlf:		push b
		push d
		push h
		mvi c,print$char
		mvi e,0dh
		call fdos
		mvi c,print$char
		mvi e,0ah
		call fdos
		pop h
		pop d
		pop b
		ret
begin:		lxi h,0
		dad sp
		shld old$sp
		lxi sp,stack$top
		mvi c,disk$reset
		call fdos
		call crlf
		call crlf
		call crlf
		lxi h,epson$buffer
eps:		mov a,m
		cpi '$'
		jz next
		call lister
		inx h
		jmp eps
next:		mvi c,print$cons
		lxi d,m1buffer
		call fdos
		call crlf
		call crlf
		call crlf
		mvi c,print$cons
		lxi d,m2buffer
		call fdos
		mvi c,read$cons
		lxi d,input$buffer
		call fdos
		lxi h,input$buffer +2
		mov a,m
		ani 0dfh
		sui 40h
		sta pwend
doneit:		mvi c,print$char
		mvi e,0dh
		call fdos
		mvi c,print$cons
		lxi d,mxbuffer
		call fdos
		call crlf
		call crlf
		call crlf
		mvi c,match$1st
ditlp1:		lxi d,fcb
		call fdos
		cpi 255
		jz all$match
		mvi b,1
		rrc
		jnc no$lsb
		inr b
no$lsb:		rrc
		jnc no$nsb
		inr b
		inr b
no$nsb:		lxi h,directory - 20h
		lxi d,20h
fcblp1:		dad d
		dcr b
		jz fcbset
		jmp fcblp1
fcbset:		lda fcb$count
		inr a
		sta fcb$count
		push h
		lhld fcb$location
		push h
		pop d
		pop h
		lxi b,12
		db  0edh,0b0h
		push d
		pop h
		shld fcb$location
		mvi c,match$next
		jmp ditlp1
all$match:	lda fcb$count
		inr a
		sta file$total
		call	sort
		lxi h,fcb$buffer
stellen:	mvi a,4
		sta fcb$4
		lda zeile
		dcr a
		sta zeile
		cz reset
print:		lda file$total
		dcr a
		sta file$total
		jz donren
		mvi a,blank
		inx h
		call lister
		mvi b,8
print2:		mov a,m
		inx h
		call lister
		dcr b
		jnz print2
		mvi a,point
		call lister
		mvi b,3
print3:		mov a,m
		inx h
		call lister
		dcr b
		jnz print3
		mvi a,blank
		call lister
		lda fcb$4
		dcr a
		sta fcb$4
		jnz print
		mvi a,cr
		call lister
		mvi a,lf
		call lister
		jmp stellen
reset:		mvi a,lf
		call lister
		mvi a,12
		sta zeile
		ret
lister:		push h
		push d
		push b
		push psw
		mvi c,05h	;print$lst
		mov e,a
		call fdos
		pop psw
		pop b
		pop d
		pop h
		ret
donren:		lda zeile
		mov b,a
donre1:		mvi a,cr
		call lister
		mvi a,lf
		call lister
		dcr b
		jnz donre1
		call reset
		call crlf
		call crlf
		mvi c,print$cons
		lxi d,m4buffer
		call fdos
fend:		lhld old$sp
		sphl
		jmp 0000h
lopcnt:	db	0
sba:	dw	0	;anfangsadresse
zwibuf:	ds	12	;zwischenbuffer f}r tausch

sort:	lda	file$total
	sta	lopcnt
	push	h
	push	d
	push	b
	push	psw
init:	lxi	h,fcb$buffer-12
	shld	sba
lade:	lhld	sba
	lxi	b,12
	dad	b
	shld	sba
	xchg
	lhld	fcb$location
	xchg
	call	hilo
	jc	newrun
	mov	e,l
	mov	d,h
	dad	b
	mov	b,c
	call	comst
	jm	lade
	jz	lade
tausch:	push	psw		;tauschen zweier namen
	push	b
	push	h
	push	d
	lxi	d,zwibuf	;b-string in zwischenbuffer
	lxi	b,12
	db	0edh,0b0h	;LDIR
	lxi	b,12		;a-string in b-string
	pop	h
	pop	d
	push	d
	push	h
	db	0edh,0b0h	;LDIR
	lxi	b,12		;zwischenbuffer in a-string
	pop	d
	push	d
	lxi	h,zwibuf
	db	0edh,0b0h	;LDIR
	pop	d
	pop	h
	pop	b
	pop	psw
	jmp	lade
newrun:	lda	lopcnt	;so oft wiederholen wie
	dcr	a	;namen vorhanden
	sta	lopcnt
	jnz	init
	pop	psw
	pop	b
	pop	d
	pop	h
	ret
comst:	push	b	;string vergleichsroutine
	push	d
	push	h
conti:	dcr	b
	jm	done
	ldax	d
	sub	m
	inx	h
	inx	d
	jz	conti
done:	pop	h
	pop	d
	pop	b
	ret
;Funktion hilo vergleicht reg. hl mit de
;carry = 0 if hl < de
;      = 1 if hl >= de
hilo:	push	b
	mov	b,a
	push	h
	mov	a,d
	ora	e
	jz	hilo5
	inx	h
	mov	a,h
	ora	l
	jz	hilo5
	pop	h
	push	d
	mvi	a,0ffh
	xra	d
	mov	d,a
	mvi	a,0ffh
	xra	e
	mov	e,a
	inx	d
	mov	a,l
	add	e
	mov	a,h
	adc	d
	pop	d
	mov	a,b
	pop	b
	ret
hilo5:	pop	h
	mov	a,b
	pop	b
sret:	stc
	ret
fcb$buffer	equ  $
		end 100h
