; Seattle Computer Products 8086 Assembler  version 2.00
;   by Tim Paterson
; Runs on the Z80 under CP/M

SYMWID:	EQU	10
FCB:	EQU	5CH
BUFSIZ:	EQU	128
EOL:	EQU	13	;ASCII carriage return
OBJECT:	EQU	100H	;DEFAULT "PUT" ADDRESS

;System call function codes
PRINTMES: EQU	9
OPEN:	EQU	15
CLOSE:	EQU	16
DELETE:	EQU	19
READ:	EQU	20
SETDMA:	EQU	26
MAKE:	EQU	22
SEQWRT:	EQU	21

;The following equates define some token values returned by GETSYM
UNDEFID:EQU	0	;Undefined identifier (including no nearby RET)
CONST:	EQU	1	;Constant (including $)
REG:	EQU	2	;8-bit register
XREG:	EQU	3	;16-bit register (except segment registers)
SREG:	EQU	4	;Segment register

	ORG	100H

	JP	BEGIN

CPYRHT:	DB	'Copyright 1979 by Seattle Computer Products, Inc.'
	DB	13,10,13,10,'$'

BEGIN:
	LD	SP,STACK
	LD	DE,HEADER
	LD	C,PRINTMES
	CALL	SYSTEM
	LD	DE,CPYRHT
	CALL	SYSTEM
	LD	A,(FCB+17)
	LD	(SYMFLG),A	;Save symbol table request flag
	LD	HL,FCB+9	;Point to file extension
	LD	A,(HL)		;Get source drive letter
	CALL	CHKDSK		;Valid drive?
	OR	A
	JR	Z,DEFAULT	;If no extension, use existing drive spec
	LD	(FCB),A
DEFAULT:
	INC	HL
	LD	A,(HL)		;Get HEX file drive letter
	CP	'Z'		;Suppress HEX file?
	CALL	NZ,CHKDSK
	LD	(HEXFCB),A
	INC	HL
	LD	A,(HL)		;Get PRN file drive letter
	CP	'Z'		;Suppress PRN file?
	JR	Z,NOPRN
	CP	'X'		;PRN file to console?
	JR	Z,NOPRN
	CALL	CHKDSK
NOPRN:
	LD	(LSTFCB),A
	CALL	ADDEXT
	LD	DE,FCB
	LD	C,OPEN
	CALL	SYSTEM
	LD	HL,NOFILE
	INC	A
	JP	Z,PRERR
	XOR	A
	LD	(FCB+32),A	;Zero Next Record field
	LD	DE,HEXFCB
	CALL	MAKFIL
	LD	DE,LSTFCB
	CALL	MAKFIL
	LD	HL,0FFH
	EXX
	LD	HL,START+1	;POINTER TO NEXT BYTE OF INTERMEDIATE CODE
	LD	(CODE),HL
	LD	IY,START	;POINTER TO CURRENT RELOCATION BYTE
	LD	HL,0
	LD	(PC),HL		;DEFAULT PROGRAM COUNTER
	LD	(BASE),HL	;POINTER TO ROOT OF ID TREE=NIL
	LD	(RETPT),HL	;Pointer to last RET record
	DEC	HL
	LD	(LSTRET),HL	;Location of last RET
	LD	HL,(6)		;HL=END OF MEMORY
	LD	(HEAP),HL	;BACK END OF SYMBOL TABLE SPACE
	LD	A,4
	LD	(BCOUNT),A	;CODE BYTES PER RELOCATION BYTE
	XOR	A
	LD	(IFFLG),A	;NOT WITHIN IF/ENDIF
	LD	(CHKLAB),A	;LOOKUP ALL LABELS

;Assemble each line of code

LOOP:
	CALL	NEXTCHR		;Get first character on line
	CP	1AH
	JP	Z,END
	LD	A,-1		;Flag that no tokens have been read yet
	LD	(SYM),A
	CALL	ASMLIN		;Assemble the line
	LD	A,(SYM)
	CP	-1		;Any tokens found on line?
	CALL	Z,GETSYM	;If no tokens read yet, read first one
	CP	';'
	JR	Z,ENDLN
	CP	EOL
	JR	Z,ENDLN
	LD	A,14H		;Garbage at end of line error
	JR	ENDLIN
	JP	END

ENDLN:
	XOR	A		;Flag no errors on line
ENDLIN:
	LD	SP,STACK
	CALL	NEXLIN
	JR	LOOP

NEXLIN:
	LD	B,0C0H		;Put end of line marker and error code (AL)
	CALL	PUTCD
	CALL	GEN1
	LD	A,(CHR)
GETEOL:
	CP	10
	RET	Z
	CP	1AH
	JP	Z,END
	CALL	NEXTCHR		;Scan over comments for linefeed
	JR	GETEOL

ABORT:
	LD	HL,NOMEM
PRERR:
	EX	DE,HL
	LD	C,PRINTMES
	CALL	SYSTEM
	JP	0

MAKFIL:
	LD	A,(DE)		;Get drive select byte
	CP	20H		;If not valid, don't make file
	RET	NC
	PUSH	DE
	INC	DE
	LD	HL,FCB+1
	LD	BC,8
	LDIR			;Copy source file name
	POP	DE
	LD	C,DELETE
	CALL	SYSTEM
	LD	C,MAKE
	CALL	SYSTEM
	LD	HL,NOSPAC
	INC	A		;Success?
	JR	Z,PRERR
	LD	C,OPEN
	JP	SYSTEM

ADDEXT:
	LD	DE,FCB+9
	LD	HL,EXTEND	;Set extension to ASM
	LD	BC,7
	LDIR
	RET

CHKDSK:
	SUB	' '		;If not present, set zero flag
	RET	Z
	SUB	20H
	JR	Z,DSKERR	;Must be in range A-W
	CP	'X'-'@'
	RET	C
DSKERR:
	LD	HL,BADDSK
	JR	PRERR

ERROR:
	LD	A,C
	JR	ENDLIN

NEXTCHR:
	EXX
	INC	L
	JR	NZ,GETCH
;Buffer empty so refill it
	LD	DE,80H
	LD	C,SETDMA
	CALL	SYSTEM
	EX	DE,HL
	LD	DE,FCB
	LD	C,READ
	CALL	SYSTEM
	OR	A
	LD	A,1AH		;Possibly signal End of File
	JR	NZ,NOMOD	;If nothing read
GETCH:
	LD	A,(HL)
NOMOD:
	EXX
	LD	(CHR),A
	RET


MROPS:
	PUSH	BC		;Save type flags
	CALL	GETOP
	PUSH	DE		;Save first operand
	CALL	GETOP2
	POP	HL		;First op in BX, second op in DX
	LD	A,SREG		;Check for a segment register
	CP	H
	JR	Z,SEGCHK
	CP	D
	JR	Z,SEGCHK
	LD	A,CONST		;Check if the first operand is immediate
	LD	C,26
	CP	H
	JR	Z,ERROR		;Error if so
	POP	BC		;Restore type flags
	CP	D		;If second operand is immediate, then done
	RET	Z
	LD	A,UNDEFID	;Check for memory reference
	CP	H
	JR	Z,STORE		;Is destination memory?
	CP	D
	JR	Z,LOAD		;Is source memory?
	BIT	0,C		;Check if register-to-register operation OK
	LD	C,27
	JR	Z,ERROR
	LD	A,D
	CP	H		;Registers must be of same length
RR:
	LD	C,22
	JR	NZ,ERROR
RR1:
	AND	1		;Get register length (1=16 bits)
	OR	B		;Or in to OP code
	CALL	PUT		;And write it
	POP	BC		;Dump return address
	LD	A,L
	ADD	A,A		;Rotate register number into middle position
	ADD	A,A
	ADD	A,A
	OR	0C0H		;Set register-to-register mode
	OR	E		;Combine with other register number
	JP	PUT

SEGCHK:
;Come here if at least one operand is a segment register
	POP	BC		;Restore flags
	BIT	3,C		;Check if segment register OK
	LD	C,22
	JR	Z,ERROR
	LD	BC,8E03H	;Segment register move OP code
	LD	A,UNDEFID
	CP	D		;Check if source is memory
	JR	Z,LOAD
	CP	H		;Check if destination is memory
	JR	Z,STORE
	LD	A,XREG
	SUB	D		;Check if source is 16-bit register
	JR	Z,RR		;If so, AL must be zero
	RES	1,B		;Change direction
	EX	DE,HL		;Flip which operand is first and second
	LD	A,XREG
	SUB	D		;Let RR perform finish the test
	JR	RR

STORE:
	BIT	2,C		;Check if storing is OK
	JR	NZ,STERR
	EX	DE,HL		;If so, flip operands
	RES	1,B		;   and zero direction bit
LOAD:
	LD	D,25
	CP	H		;Check if memory-to-memory
	JR	Z,MRERR
	LD	A,H
	CP	REG		;Check if 8-bit operation
	JR	NZ,XRG
	LD	D,22
	BIT	0,C		;See if 8-bit operation is OK
	JR	Z,MRERR
XRG:
	LD	A,E
	SUB	6		;Check for R/M mode 6 and register 0
	OR	L		;   meaning direct load/store of accumulator
	JR	NZ,NOTAC
	BIT	3,C		;See if direct load/store of accumulator
	JR	Z,NOTAC		;   means anything in this case
; Process direct load/store of accumulator
	LD	A,B
	AND	2		;Preserve direction bit only
	XOR	2		;   but flip it
	OR	0A0H		;Combine with OP code
	LD	B,A
	LD	A,H		;Check byte/word operation
	AND	1
	OR	B
	POP	BC		;Dump return address
	JP	PUTADD		;Write the address

NOTAC:
	LD	A,H
	AND	1		;Get byte/word bit
	AND	C		;But don't use it in word-only operations
	OR	B		;Combine with OP code
	CALL	PUT
	LD	A,L
	ADD	A,A		;Rotate to middle position
	ADD	A,A
	ADD	A,A
	OR	E		;Combine register field
	POP	BC		;Dump return address
	JP	PUTADD		;Write the address

STERR:
	LD	D,29
MRERR:
	LD	C,D
	JP	ERROR

GETOP2:
;Get the second operand: look for a comma and drop into GETOP
	LD	A,(SYM)
	CP	','
	LD	C,21
	JP	NZ,ERROR


GETOP:
	CALL	GETSYM
GETOP1:
;Enter here if we don't need a GETSYM first
	CP	'['		;Memory reference?
	JR	Z,MEM
	CP	5		;Flag ("B", "W", etc.)?
	JR	Z,FLG
	CP	REG		;8-Bit register?
	JR	Z,NREG
	CP	XREG		;16-Bit register?
	JR	Z,NREG
	CP	SREG		;Segment register?
	JR	Z,NREG
VAL:				;Must be immediate
	XOR	A		;No addressing modes allowed
VAL1:
	CALL	GETVAL
	LD	HL,(CON)	;Defined part
	LD	(DATA),HL
	LD	HL,(UNDEF)	;Undefined part
	LD	(DLABEL),HL
	LD	E,B
	LD	D,CONST
	LD	A,D
	RET
NREG:
	PUSH	DE
	CALL	GETSYM
	POP	DE
	LD	A,D
	RET
MEM:
	CALL	GETSYM
	LD	A,1
	CALL	GETVAL
	LD	A,(SYM)
	CP	']'
	LD	C,24
	JP	NZ,ERROR
	CALL	GETSYM
	LD	HL,(CON)
	LD	(ADDR),HL
	LD	HL,(UNDEF)
	LD	(ALABEL),HL
	LD	E,B
	LD	D,UNDEFID
	LD	A,D
	RET
FLG:
	CALL	GETSYM
	CP	','
	JR	Z,GETOP
	JR	GETOP1


GETVAL:
	EX	AF,AF'
	PUSH	HL
	LD	HL,0
	LD	(CON),HL
	LD	(UNDEF),HL
	POP	HL
	LD	A,(SYM)
	CP	'+'
	JR	Z,PLSMNS
	CP	'-'
	JR	Z,PLSMNS
	LD	B,'+'
	PUSH	BC
	JR	OPERATE
PLSMNS:
	PUSH	AF
	EX	AF,AF'
	SET	2,A		;Flag that a sign was found
	EX	AF,AF'
	CALL	GETSYM
OPERATE:
	CP	CONST
	JP	Z,CVAL
	CP	UNDEFID
	JP	Z,UVAL
	CP	'"'
	JP	Z,STRING
	CP	'''
	JP	Z,STRING
	CP	XREG		;Only 16-bit register may index
	LD	C,20
	JP	NZ,ERROR
	EX	AF,AF'
	BIT	0,A		;Check to see if indexing is OK
	LD	C,1
	JP	Z,ERROR
	EX	AF,AF'
	LD	A,E
	LD	C,3
	CP	3		;Check for BX
	JR	Z,BXJ
	SUB	5		;Check for BP
	JR	Z,BPJ
	DEC	A		;Check for SI
	LD	C,4
	JR	Z,SIJ
	DEC	A		;Check for DI
	JR	Z,DIJ
	LD	C,2		;Invalid base/index register
	JP	ERROR
DIJ:
	EX	AF,AF'
	BIT	4,A		;Check if already seen index register
	JP	NZ,ERROR
	OR	30H		;Flag seeing index register DI
	JR	LPCHK
SIJ:
	EX	AF,AF'
	BIT	4,A		;Check if already seen index register
	JP	NZ,ERROR
	SET	4,A		;Flag seeing index register SI
	JR	LPCHK
BXJ:
	EX	AF,AF'
	BIT	7,A		;Check if already seen base register
	JP	NZ,ERROR
	SET	7,A		;Flag seeing base register BX
	JR	LPCHK
BPJ:
	EX	AF,AF'
	BIT	7,A		;Check if already seen base register
	JP	NZ,ERROR
	OR	0C0H		;Flag seeing base register BP
LPCHK:
	EX	AF,AF'
	POP	AF		;Get operator
	CP	'-'		;Can't subtract a register
	LD	C,5
	JP	Z,ERROR
NEXTER:
	EX	AF,AF'
	SET	2,A		;Flag that a term was processed
	EX	AF,AF'
	CALL	GETSYM
	CP	'+'
	JR	Z,PLSMNS
	CP	'-'
	JP	Z,PLSMNS
	EX	AF,AF'
	LD	B,0
	BIT	4,A		;Test INDEX bit
	RLA			;BASE bit (zero flag not affected)
	JR	Z,NOIND		;Jump if not indexed, with BASE bit in carry
	CCF
	RL	B
	RLA			;BP bit
	RL	B
	RLA			;DI bit
	RL	B		;The low 3 bits now have indexing mode
MODE:
	LD	HL,(UNDEF)
	LD	A,H
	OR	L
	SET	7,B		;If undefined label, force 16-bit displacement
	RET	NZ
	LD	HL,(CON)
	CALL	SIGNEXT		;Check if 8-bit with sign extended
	RET	NZ		;If not, use 16-bit displacement
	RES	7,B		;Reset 16-bit displacement
	SET	6,B		;Set 8-bit displacement
	LD	A,H
	OR	L
	RET	NZ		;Use it if not zero displacement
	LD	A,B
	AND	7		;Specify no displacement
	LD	B,A
	CP	6		;Check for BP+0 addressing mode
	RET	NZ
	SET	6,B		;If BP+0, use 8-bit displacement
	RET

SIGNEXT:
	LD	A,H
	OR	A
	JR	Z,HIZERO
	INC	A
	RET	NZ
HIZERO:
	LD	A,L
	XOR	H
	AND	80H
	RET

NOIND:
	LD	B,6		;Try direct address mode
	RET	NC		;If no base register, that's right
	RLA			;Check BP bit
	JR	C,MODE
	INC	B		;If not, must be BX
	JR	MODE

CVAL:
	LD	HL,(CON)
	POP	AF
	CP	'-'
	JR	NZ,ADDCON
	SBC	HL,DE
SETCON:
	LD	(CON),HL
	JR	NEXTER
ADDCON:
	ADD	HL,DE
	JR	SETCON

UVAL:
	EX	AF,AF'
	LD	C,6
	BIT	3,A		;Check if undefined label has been seen
	JP	NZ,ERROR
	SET	3,A		;Flag seeing undefined label
	EX	AF,AF'
	LD	(UNDEF),HL
	POP	AF
	CP	'+'		;Undefined label may only be added
	LD	C,5
	JP	NZ,ERROR
	JR	NEXTER


GETSYM:
	CALL	GETSY
	LD	A,(SYM)
	RET

SCANB:
	LD	A,(CHR)
SCANT:
	CP	' '
	JR	Z,NEXB
	CP	9
	RET	NZ
NEXB:
	CALL	NEXTCHR
	JR	SCANT

DOLLAR:
	LD	DE,(OLDPC)
	LD	A,CONST
	LD	(SYM),A
	JP	NEXTCHR

GETSY:
	CALL	SCANB
	CP	'$'
	JR	Z,DOLLAR
	LD	(SYM),A
	OR	20H
	CP	'z'+1
	JP	NC,NEXTCHR
	CP	'a'
	JP	NC,LETTER
	CP	'9'+1
	JP	NC,NEXTCHR
	CP	'0'
	JP	C,NEXTCHR
	LD	HL,SYM
	LD	(HL),CONST
	CALL	READID
	DEC	HL
	LD	A,(HL)
	LD	C,7
	LD	HL,0
	CP	'h'
	JR	Z,HEX
	INC	C
	LD	IX,ID
DEC:
	LD	A,(IX)
	INC	IX
	CP	'9'+1
	JP	NC,ERROR
	SUB	'0'
	LD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C	HL
	LD	D,(HL)
	LD	A,D
	OR	E
	SCF
	RET	Z
	EX	DE,HL
	JR	LOOK

LOOKRET:
	LD	A,B
	CP	3		;RET has 3 letters
	JR	NZ,LOOKUP
	DEC	HL
	SET	7,(HL)
	LD	DE,RETSTR+2
CHKRET:
	LD	A,(DE)
	CP	(HL)
	JR	NZ,LOOKIT
	DEC	HL
	DEC	DE
	DJNZ	CHKRET
	LD	DE,(LSTRET)
	LD	A,E
	AND	D
	INC	A
	JR	Z,ALLRET
	LD	HL,(PC)
	SBC	HL,DE
	CALL	SIGNEXT
	LD	A,1
	RET	Z
ALLRET:
	LD	HL,(RETPT)
	LD	A,H
	OR	L
	LD	A,0
	RET	NZ
	LD	HL,(HEAP)
	DEC	HL
	DEC	HL
	DEC	HL
	LD	(HEAP),HL
	XOR	A
	LD	(HL),A
	LD	(RETPT),HL
	RET

LOOKUP:
	DEC	HL
	SET	7,(HL)
LOOKIT:
	LD	HL,(BASE)
	LD	A,H
	OR	L
	JR	Z,EMPTY
	CALL	LOOK
	JR	C,ENTER
	LD	DE,4
	ADD	HL,DE
	LD	A,(HL)
	OR	A
	RET	Z
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	RET

ENTER:
	PUSH	HL		;Save pointer to link field
	CALL	CREATE		;Add the node
	EX	(SP),HL
	LD	(HL),D		;Link new node
	DEC	HL
	LD	(HL),E
	POP	HL
	RET			;Zero was set by CREATE

EMPTY:
	CALL	CREATE
	LD	(BASE),DE
	RET


CREATE:
	LD	A,(LENID)
	ADD	A,8		;Storage needed for the node
	LD	HL,(HEAP)
	LD	E,A
	LD	D,0
	SBC	HL,DE		;Heap grows downward
	LD	(HEAP),HL
	EX	DE,HL
	LD	HL,(CODE)	;Check to make sure there's enough
	SBC	HL,DE
	JP	NC,ABORT
	PUSH	DE
	LD	HL,LENID
	LD	C,(HL)
	INC	C
	LD	B,0
	LDIR			;Move identifier and length into node
	LD	B,4
	EX	DE,HL
NILIFY:
	LD	(HL),C		;Zero left and right links
	INC	HL
	DJNZ	NILIFY
	XOR	A		;Set zero flag
	LD	(HL),A		;Zero defined flag
	POP	DE		;Restore pointer to node
	RET

CPSLP:
	LD	A,(DE)
	CP	(HL)
	INC	DE
	INC	HL
	RET	NZ
	DJNZ	CPSLP
	RET

GETLAB:
	LD	HL,0
	LD	(LABPT),HL
	LD	A,-1
	LD	(FLAG),A
	LD	(LONG),A
	LD	D,0
	LD	A,(CHR)
	CP	' '+1
	JR	C,NOT1
	SET	0,D
NOT1:
	CALL	GETLET
	RET	C
	CP	':'
	JR	NZ,LABCHK
	CALL	NEXTCHR
	JR	LABEL
LABCHK:
	OR	A
	BIT	0,D
	RET	Z
LABEL:
	LD	A,(CHKLAB)
	OR	A
	JP	NZ,GETLET
	CALL	LOOKUP
	LD	C,11
	JP	NZ,ERROR
	LD	DE,(PC)
	LD	(HL),1
	INC	HL
	LD	(HL),E
	LD	(LABPT),HL
	INC	HL
	LD	(HL),D
	JP	GETLET

ASMLIN:
	LD	HL,(PC)
	LD	(OLDPC),HL
	CALL	GETLAB
	JP	C,ENDLN
	LD	HL,LENID
	LD	A,(HL)
	LD	C,12
	SUB	2
	LD	B,A
	JP	C,ERROR
	CP	5
	JP	NC,ERROR
	INC	HL
	LD	A,(HL)
	SUB	'a'
	LD	C,A
	ADD	A,A
	ADD	A,A
	ADD	A,C
	ADD	A,B
	ADD	A,A
	LD	HL,OPTAB
	LD	E,A
	LD	D,0
	ADD	HL,DE
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	EX	DE,HL
	INC	B
	LD	C,B
	LD	A,(HL)
	INC	HL
	OR	A
	JR	Z,OPERR
FINDOP:
	LD	B,C
	EX	AF,AF'
	LD	DE,ID+1
	CALL	CPSLP
	JR	Z,HAVOP
	LD	D,0
	LD	E,B
	INC	E
	INC	E
	ADD	HL,DE
	EX	AF,AF'
	DEC	A
	JR	NZ,FINDOP
OPERR:
	LD	C,12
	JP	ERROR

HAVOP:
	LD	E,(HL)
	INC	H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
	LD	HL,(DLABEL)
	LD	A,H
	OR	L
	JR	NZ,PUTBW
	LD	HL,(DATA)
	OR	H
	JR	Z,PUTBW
	INC	H
	JR	Z,PUTBW
	LD	C,31
	JP	ERROR
PUTBW:
	LD	DE,(DLABEL)
	LD	HL,(DATA)
PUTCHK:
	LD	A,D
	OR	E
	JR	Z,NOUNDEF
	LD	A,E
	CALL	PUTCD
	LD	A,D
	CALL	PUTCD
	LD	A,L
	CALL	PUTINC
	LD	A,H
	BIT	7,B
	JR	Z,SMPUT
	CALL	GEN
	JR	PRET
SMPUT:
	CALL	PUTCD
	CALL	GEN1
PRET:
	POP	HL
	POP	DE
	POP	BC
	RET

NOUNDEF:
	LD	A,L
	LD	C,H
	PUSH	BC
	LD	B,0
	CALL	GEN
	POP	BC
	LD	A,C
	BIT	7,B
	LD	B,0
	CALL	NZ,GEN
	JR	PRET

PUTADD:
	PUSH	BC
	PUSH	DE
	PUSH	HL
	LD	B,0
	LD	C,A
	CALL	GEN		;Save the addressing mode as pure code
	LD	A,C
	LD	B,80H
	AND	0C7H
	CP	6
	JR	Z,TWOBT		;Direct address?
	AND	0C0H
	JR	Z,PRET		;Indirect through reg, no displacement?
	CP	0C0H
	JR	Z,PRET		;Register to register operation?
	LD	B,A		;Save whether one- or two-byte displacement
TWOBT:
	LD	HL,(ADDR)
	LD	DE,(ALABEL)
	JR	PUTCHK

GRP2:
	CALL	GETOP
	LD	BC,0FF30H
	CP	UNDEFID
	JR	Z,PMEM
	LD	B,50H
	CP	XREG
	JR	Z,PXREG
	LD	B,6
	CP	SREG
	JP	Z,PACKREG
	LD	C,20
	JP	ERROR

PMEM:
	LD	A,B
	CALL	PUT
	LD	A,C
	OR	E
	JR	PUTADD

PXREG:
	LD	A,B
	OR	E
	JP	PUT

GRP3:
	CALL	GETOP
	PUSH	DE
	CALL	GETOP2
	POP	HL
	LD	BC,8614H
	LD	A,SREG
	CP	H
	JP	Z,ERROR
	CP	D
	JP	Z,ERROR
	LD	A,CONST
	C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SAVDAT:
	OR	A
	JP	NZ,PUTBYT
	JP	PUTWOR
IF:
	LD	A,H
	OR	L
	LD	A,1
	JR	Z,SKIPCD
	LD	(IFFLG),A
	RET

SKIPCD:
	LD	(CHKLAB),A
SKIPLP:
	XOR	A
	CALL	NEXLIN
	CALL	NEXTCHR
	CP	1AH
	JR	Z,END
	CALL	GETLAB
	JR	C,SKIPLP
	LD	HL,LENID
	LD	DE,IFEND
	LD	B,(HL)
	INC	B
	CALL	CPSLP
	JR	NZ,SKIPLP
	XOR	A
	LD	(CHKLAB),A
	RET

ENDIF:
	LD	HL,IFFLG
	LD	A,(HL)
	LD	(HL),0
	LD	C,36
	OR	A
	JP	Z,ERROR
	RET

;*********************************************************************
;	PASS 2
;*********************************************************************

END:
	LD	C,4
WREND:
	LD	B,0FFH
	LD	A,B
	CALL	GEN
	DEC	C
	JR	NZ,WREND
	LD	A,(LSTFCB)
	CP	'Z'
	JR	Z,NOREOPN
	CALL	ADDEXT
	LD	DE,FCB
	LD	C,OPEN
	CALL	SYSTEM
	LD	HL,0FFH
	EXX
NOREOPN:
	LD	A,-5
	LD	(HEXCNT),A	;FLAG HEX BUFFER AS EMPTY
	LD	(SKPFLG),A
	LD	HL,HEXBUF
	LD	(HEXPNT),HL
	LD	HL,LSTBUF
	LD	(LSTPNT),HL
	LD	HL,0
	LD	(ERRCNT),HL
	LD	(PC),HL
	LD	HL,OBJECT
	LD	(HEXADD),HL
	XOR	A
	LD	(FCB+20H),A	;Set NEXT RECORD field to zero
	LD	(COUNT),A
	CALL	STRTLIN
	LD	HL,START
NEXBT:
	LD	B,4
	LD	C,(HL)		;Get flag byte
	INC	HL		;Point to code
CDLP:
	RL	C		;Get high bit
	JR	C,TESTTYP	;Word fixup or special
	RL	C
	JR	C,DEFBYT	;Byte fixup
	LD	A,(HL)
OBJBT:
	CALL	SAVCD
NEXCD:
	INC	HL
	DJNZ	CDLP		;Still have codes left?
	JR	NEXBT
TESTTYP:
	RL	C
	JP	NC,DEFWRD
	LD	A,(HL)
	CP	-10
	JP	NC,SPCFUN
	PUSH	BC
	PUSH	HL
	LD	HL,COUNT
	LD	A,6
	SUB	(HL)
	LD	(HL),0
	LD	B,A
	LD	A,' '
	JR	Z,NOFIL
BLNK:				;Put in 3 blanks for each byte not present
	CALL	LIST
	CALL	LIST
	CALL	LIST
	DJNZ	BLNK
NOFIL:
	CALL	OUTLIN
	POP	HL
	LD	A,(HL)
	PUSH	HL
	CALL	REPERR
	LD	A,(ERR)
	CALL	REPERR
	CALL	STRTLIN
	POP	HL
	POP	BC
	JR	NEXCD

OUTLIN:
	CALL	LIST
	LD	A,(LSTFCB)
	CP	'Z'
	RET	Z
	LD	HL,LINFLG
	LD	A,(HL)
	LD	(HL),-1
	OR	A
	JR	NZ,CRLF		;Output line only if first time
OUTLN:
	CALL	NEXTCHR
	CALL	LIST
	CP	10		;Output until linefeed found
	JR	NZ,OUTLN
	RET

PRTCNT:
	LD	HL,ERCNTM
	CALL	PRINT
	LD	HL,(ERRCNT)
	CALL	CONV10
	CALL	PRNT10
CRLF:
	LD	A,13
	CALL	LIST
	LD	A,10
	JR	LIST

DEFBYT:
	CALL	FIXUP
	EX	DE,HL
	CALL	SIGNEXT		;See if in range +127 to -128
	EX	DE,HL
	JR	Z,OBJBTE	;If so, it's always OK
	LD	A,D
	OR	A		;Check for range +255 to -256
	JR	Z,CHKJMP
	INC	A
	JR	Z,CHKJMP
RNGERR:
	LD	A,101		;Value out of range
	LD	(ERR),A
OBJBTE:
	LD	A,E
	JP	OBJBT
CHKJMP:
;Check for short jump. If so, we're out of range; otherwise we're OK
	LD	A,(COUNT)
	DEC	A		;Only one other byte on line?
	JR	NZ,OBJBTE	;Can't be short jump if not
	LD	A,(PREV)	;Get the first byte of this line
	CP	0EBH		;Direct short jump?
	JR	Z,RNGERR
	AND	0FCH
	CP	0E0H		;LOOP or JCXZ instruction?
	JR	Z,RNGERR
	AND	0F0H
	CP	70H		;Conditional jump?
	JR	Z,RNGERR
	JR	OBJBTE

DEFWRD:
	CALL	FIXUP
	LD	A,E
	CALL	SAVCD
	LD	A,D
	JP	OBJBT

FIXUP:
	PUSH	BC
	LD	E,(HL)		;Get pointer to symbol
	INC	HL
	LD	D,(HL)
	INC	HL
	EX	DE,HL
	LD	A,(HL)		;Whether symbol was defined
	INC	HL
	LD	C,(HL)		;Get symbol value
	INC	HL
	LD	B,(HL)
	EX	DE,HL
	LD	E,(HL)		;Get constant part
	INC	HL
	LD	D,(HL)
	EX	DE,HL
	ADD	HL,BC		;Add symbol value to constant part
	POP	BC
	EX	DE,HL
	OR	A		;See if symbol got defined
	RET	NZ
	LD	A,100		;Undefined - flag error
	LD	(ERR),A
	LD	DE,0
	RET

STRTLIN:
	XOR	A
	LD	(ERR),A
	LD	(LINFLG),A
	LD	HL,(PC)
	LD	A,H
	CALL	PHEX
	LD	A,L
PHEXB:
	CALL	PHEX
	LD	A,' '
LIST:
	PUSH	AF
	LD	A,(LSTFCB)
	CP	'Z'
	JR	Z,OUTRET
	CP	'X'
	JR	Z,LSTCON
	POP	AF
	PUSH	AF
	AND	7FH
	PUSH	HL
	LD	HL,(LSTPNT)
	LD	(HL),A
	CALL	INCBUF
	LD	(LSTPNT),HL
	JR	NZ,HAVSPC
	LD	HL,LSTBUF
	LD	(LSTPNT),HL
	PUSH	DE
	EX	DE,HL
	PUSH	BC
	LD	C,SETDMA
	CALL	5
	LD	DE,LSTFCB
	LD	C,SEQWRT
	CALL	5
	POP	BC
	POP	DE
HAVSPC:
	POP	HL
OUTRET:
	POP	AF
	RET
LSTCON:
	POP	AF
	PUSH	AF
	CALL	OUTA
	POP	AF
	RET

PHEX:
	LD	B,A
	CALL	UHALF
	CALL	LIST
	LD	A,B
	CALL	LHALF
	JR	LIST

SPCFUN:
	INC	A		;End of file?
	JR	Z,FINI
	INC	HL
	LD	E,(HL)		;Get address
	INC	HL
	LD	D,(HL)
	PUSH	HL
	INC	A		;ORG?
	JR	Z,DORG
	INC	A		;PUT?
	JR	Z,DPUT
D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
	JR	NC,LENOK
	LD	A,1
LENOK:
	LD	D,A
WRTSYM:
	LD	A,(HL)
	INC	HL
	CALL	LIST
	DJNZ	WRTSYM
	LD	B,D
	INC	B
	LD	A,' '
OUTB:
	CALL	LIST
	DJNZ	OUTB
	INC	HL
	INC	HL
	PUSH	HL
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	LD	A,(HL)
	CALL	PHEX
	DEC	HL
	LD	A,(HL)
	CALL	PHEX
	CALL	CRLF
	POP	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	LD	A,D
	OR	E
	JR	NZ,NODE
	RET

CONV10:
	LD	B,16
	LD	DE,0
CONVLP:
	ADD	HL,HL
	LD	A,E
	ADC	A,A
	DAA
	LD	E,A
	LD	A,D
	ADC	A,A
	DAA
	LD	D,A
	RL	C
	DJNZ	CONVLP
	RET

PRNT10:
	EX	DE,HL
	LD	B,'0'-' '	;Enable leading zero suppression
	LD	A,C
	CALL	LODIG
	LD	A,H
	CALL	HIDIG
	LD	A,H
	CALL	LODIG
	LD	A,L
	CALL	HIDIG
	LD	A,L
	LD	B,0		;Don't suppress last digit
LODIG:
	CALL	LHALF
DIGIT:
	CP	'0'
	JR	Z,SUPZ
	LD	B,0		;Turn off zero suppression if not zero
SUPZ:
	SUB	B		;Convert leading zeros to blanks
	JP	LIST

HIDIG:
	CALL	UHALF
	JR	DIGIT

HEXEOF:
	LD	A,1AH
	CALL	PUTCHR
	JR	NZ,HEXEOF
	RET

SAVCD:
	LD	(PREV),A
	PUSH	HL
	PUSH	BC
	PUSH	AF
	PUSH	DE
	CALL	CODBYT
	POP	DE
	LD	HL,COUNT
	INC	(HL)
	LD	A,(HL)
	CP	7
	JR	NZ,NOEXT
	LD	(HL),1
	LD	A,' '
	CALL	OUTLIN
	LD	A,' '
	LD	B,5
TAB:
	CALL	LIST
	DJNZ	TAB
NOEXT:
	POP	AF
	CALL	PHEXB
	POP	BC
	LD	HL,(PC)
	INC	HL
	LD	(PC),HL
	LD	HL,(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M4
	DW	NONE
	DW	NONE
	DW	NONE	;N
	DW	N3
	DW	NONE
	DW	NONE
	DW	NONE
	DW	O2	;O
	DW	O3
	DW	O4
	DW	NONE
	DW	NONE
	DW	NONE	;P
	DW	P3
	DW	P4
	DW	P5
	DW	NONE
	DW	NONE	;Q
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE	;R
	DW	R3
	DW	R4
	DW	R5
	DW	NONE
	DW	NONE	;S
	DW	S3
	DW	S4
	DW	NONE
	DW	NONE
	DW	NONE	;T
	DW	NONE
	DW	T4
	DW	NONE
	DW	NONE
	DW	U2	;U
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE	;V
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE	;W
	DW	NONE
	DW	W4
	DW	NONE
	DW	NONE
	DW	NONE	;X
	DW	X3
	DW	X4
	DW	NONE
	DW	NONE
	DW	NONE	;Y
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE	;Z
	DW	NONE
	DW	NONE
	DW	NONE
	DW	NONE

HEADER:	DB	13,10,'Seattle Computer Products 8086 Assembler Version 2.00'
	DB	13,10,'$'
ERRMES:	DM	'***** ERROR no. '
NOSPAC:	DB	13,10,'No directory space',13,10,24H
ENDMES:	DM	'H',13,10
NOMEM:	DB	13,10,'Insufficient memory',13,10,'$'
NOFILE:	DB	13,10,'File not found',13,10,'$'
WRTERR:	DB	13,10,'Disk write error',13,10,'$'
BADDSK:	DB	13,10,'Bad disk specifier',13,10,'$'
ERCNTM:	DM	13,10,13,10,'Error Count ='
SYMMES:	DM	13,10,'Symbol Table',13,10,13,10
EXTEND:	DB	'A86',0,0,0,0
IFEND:	DB	5,'endif'
RETSTR:	DM	'ret'
HEXFCB:	DB	0,'        HEX',0,0,0,0
	DS	16
	DB	0
LSTFCB:	DB	0,'        PRN',0,0,0,0
	DS	16
	DB	0
PC:	DS	2
OLDPC:	DS	2
LABPT:	DS	2
FLAG:	DS	1
LONG:	DS	1
ADDR:	DS	2
ALABEL:	DS	2
DATA:	DS	2
DLABEL:	DS	2
CON:	DS	2
UNDEF:	DS	2
LENID:	DS	1
ID:	DS	80
CHR:	DS	1
SYM:	DS	1
BASE:	DS	2
HEAP:	DS	2
SYMFLG:	DS	1
CODE:	DS	2
DATSIZ:	DS	1
RELOC:	DS	1
BCOUNT:	DS	1
COUNT:	DS	1
ERR:	DS	1
HEXPNT:	DS	2
HEXLEN:	DS	2
HEXADD:	DS	2
LASTAD:	DS	2
HEXCNT:	DS	1
CHKSUM:	DS	1
SKPFLG:	DS	1
LINFLG:	DS	1
PREV:	DS	1
IFFLG:	DS	1
CHKLAB:	DS	1
LSTPNT:	DS	2
ERRCNT:	DS	2
LSTRET:	DS	2
RETPT:	DS	2
HEXBUF:	DS	BUFSIZ+BUFSIZ
LSTBUF:	DS	BUFSIZ
	DS	50
STACK:	EQU	$
START:	EQU	$
