	title	Label delimiters for MICROSOFT M80
	name	('LABEL80')

	maclib	base80

; Program inserts the required label delimiter ':' for
; MICROSOFT M80 source files. Without this delimiter,
; the M80 assembler produces errors.
; Some assembler don't require this delimiter

; Program removes the delimiter for 'EQU' statement which
; results in an error

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Phone: 040/4223247

;		Version 1.1, November 1987

; To get program running type:

;		LABEL80 filename{.ext}

; A new file will be created but the old one will be
; updated. The default extension is .MAC

; ===== Constants =====

EQULEN	equ	3		; Length of 'EQU'
LABLEN	equ	8		; Max length of label

LINLEN	equ	80		; Standard line length

UPMASK	equ	01011111b

; ===== From library BASELIB =====

	ext	fillin,puteof,emplin,delbak,crebak
	ext	delete,close,crlf,wrbuf,wrfcb,string

; ===== Start code here =====

	ld	sp,LocStk	; Get our own stack
	ld	a,(FCB+.fdrv)	; Test legal file name
	cp	' '
	ld	de,$NO.FILE
	jp	z,FinMsg	; Nope, tell it
	ld	de,FCB+.fdrv+.fname
	ld	a,(de)		; Test extension given
	cp	' '
	jp	nz,GotEXT	; Yeap
	ld	hl,$MAC
	ld	bc,.fext
	ldir			; Set .MAC
GotEXT:
	ld	de,FOUT
	ld	hl,FCB
	ld	a,(hl)		; Get drive
	ld	(de),a		; Set same drive
	call	crebak		; Prepare .BAK file
	jp	nc,BAK.ok
	ld	de,$NO.OPEN
	or	a
	jp	z,FinMsg	; Cannot open file
	ld	de,$NO.CREC
	jp	FinMsg		; Cannot create file
BAK.ok:
	ld	hl,FOUT
	ld	(wrfcb),hl	; Set FCB for output
	ld	hl,DMAOUT
	ld	(wrbuf),hl	; And DMA
	ld	b,null
	ld	de,Line1
	call	fillin		; Read first line
	ld	de,Line2
	ld	hl,Line1+1
	ld	bc,LINLEN
	ldir			; Unpack line
	jp	c,EmpFile	; Empty file
	ld	(Len1),a	; Save length of line
LabelLoop:
	ld	b,null
	ld	de,Line1
	call	fillin		; Read a line
	jp	c,EndFile	; End of file
	ld	(Len2),a	; Save length of line
	ld	de,Line3
	ld	hl,Line1+1
	ld	bc,LINLEN
	ldir			; Unpack line
	ld	a,(Len1)	; Get length of line
	call	nz,PrcLine	; Not an empty line
	call	WrtLine		; Write line to file
	jp	c,WrtErr	; Write error
	ld	de,Line2
	ld	hl,Line3
	ld	bc,LINLEN
	ldir			; Unpack line
	ld	a,(Len2)	; Unpack length of line
	ld	(Len1),a
	jp	LabelLoop
WrtErr:
	ld	de,$WR.ERR
ClsErr:
	push	de
	ld	de,FOUT
	call	delete		; Delete file on error
	pop	de
	jp	FinMsg
EndFile:
	call	WrtLine		; Write line to file
	jp	c,WrtErr	; Error
EmpFile:
	call	puteof		; Close file		*** Message POINTER??????
	jp	c,ClsErr	; Error
	ld	de,FOUT
	call	close		; Close file
	ld	de,$CLS.ERR
	jp	c,WrtErr	; Cannot close		???????????????
	ld	hl,FCB
	ld	de,FOUT
	call	delbak		; Delete .BAK file, rename it
	ld	de,$DONE	; Success
FinMsg:
	call	crlf		; Give new line
	call	string		; Tell state
	call	crlf
	jp	OS		; That's it

; ===== Subroutines =====
;
; Process a line
;
PrcLine:
	ld	b,a		; Set length of line
	ld	hl,Line2	; Get line
	ld	a,(hl)		; Get first character
	cp	' '+1		; Test possible label
	ret	c		; Nope
	cp	';'		; Test comment
	ret	z		; Yeap
	cp	'.'		; Test pseudo instruction prefix
	ret	z		; Yeap
	call	FndLabel	; Test label delimiter
	ld	a,e
	cp	TRUE
	jp	z,GotLabel	; Yeap, test valid
	call	isEQU		; Test EQU follows
	ret	z		; Yeap
	ld	a,c		; Get label count
	and	LoMask
	cp	LABLEN		; Test max length
	jp	nz, ShrtLab
	dec	hl
	ld	a,(hl)		; Get previous character
	cp	' '+1		; Test label only
	jp	c,MarkLab	; Yeap, so mark it
	inc	hl
 ShrtLab:
	ld	a,b		; Get length of line
	sub	c		; Subtract length of lable
	add	a,2		; Remember new line (cr and lf)
	ld	c,a		; Make 16 bit length
	ld	b,0
	push	hl
	pop	de
	dec	hl
	push	hl
	push	bc
	push	bc
	push	de
	ld	de,TmpLine
	ldir			; Save line
	pop	de
	pop	bc
	ld	hl,TmpLine
	pop	bc
	ldir			; Bring back line
	pop	hl
MarkLab:
	ld	(hl),':'	; Insert label marker
	ret
GotLabel:
	call	isEQU		; Test EQU
	ret	nz		; Nope
	dec	hl
	ld	(hl),' '	; Overwrite ':' if EQU
	ret
;
; Test EQU found in label
;
isEQU:
	push	hl
	push	bc
	ld	b,FALSE		; Init flag
NxtEQU:
	ld	a,(hl)
	cp	lf		; Test end of line
	jp	z,EndEQU	; Yeap
	cp	' '+1		; Test printable
	inc	hl
	jp	c,NxtEQU	; Nope
	dec	hl
	push	hl
	call	FndLabel	; Test label delimiter
	pop	hl
	ld	a,c		; Get count
	cp	EQULEN+1
	jp	nz,NotEQU	; Not EQU
	ld	c,'E'
	call	CmpChr		; Test 'E', 'Q' and 'U'
	jp	nz,NotEQU
	ld	c,'Q'
	call	CmpChr
	jp	nz,NotEQU
	ld	c,'U'
	call	CmpChr
NotEQU:
	pop	bc
	pop	hl
	ret
EndEQU:
	ld	a,b		; Get flag
	cp	FALSE		; Test first line
	jp	nz,NotEQU	; Nope
	ld	b,TRUE
	ld	hl,Line3	; Set second line
	cp	' '+1		; Test label pssible
	jp	c,NxtEQU
	or	a
	jp	NotEQU
;
; Find label
;
FndLabel:
	ld	c,0		; Init count
	ld	e,FALSE		; Init no label
FndLoop:
	ld	a,(hl)		; Get character
	inc	c		; Count it
	inc	hl
	cp	':'		; Test label delimiter
	jp	nz,NotLabel
	ld	e,TRUE		; Set found
	ret
NotLabel:
	cp	' '+1		; Test possible label
	jp	nc,FndLoop	; Yeap
	ret
;
; Compare characters reg_C:^HL
;
CmpChr:
	ld	a,(hl)		; Get character
	inc	hl
	and	UPMASK		; As upper case
	cp	c		; Compare
	ret
;
; Write fixed line to file
;
WrtLine:
	ld	de,Line1+1
	ld	hl,Line2
	ld	bc,LINLEN
	ldir			; Bring back line
	ld	b,null
	ld	de,Line1+1
	call	emplin		; Write to file
	ret

	dseg

$MAC:
	db	'MAC'
$DONE:
	db	'Label fix done',eot
$NO.OPEN:
	db	bell,'Cannot open file',eot
$CLS.ERR:
	db	bell,'Cannot close file',eot
$NO.CREC:
	db	bell,'Cannot create file',eot
$WR.ERR:
	db	bell,'Cannot write to file',eot
$NO.FILE:
	db	bell,'File name required',eot
FOUT:
	ds	fcblen
DMAOUT:
	ds	reclng
;
	ds	2*32
LocStk	equ	$
;
Len1:
	db	0
Len2:
	db	0
TmpLine:
	ds	LINLEN
Line1:
	db	LINLEN
	ds	LINLEN
Line2:
	ds	LINLEN
Line3:
	ds	LINLEN

	end

