	.phase	0d00h
	ld	sp,stack
	ld	hl,(sbot)	; get b;
	ld	d,h		; move b to DE;
	ld	e,l
	ld	hl,(dtop)	; get c;
	ld	b,h		; move c to BC;
	ld	c,l
	ld	hl,(sstrt)	; get a;
	push	bc		; save c;
	call	comph		; complement HL;
	add	hl,de		; HL := b-a;
	ld	b,h		; move to BC;
	ld	c,l
	pop	hl		; get c;
	add	hl,bc		; set HL equal to bottom of
				;   destination area;
	ld	a,(funk)	; get f;
	or	a		; if f=0, then slip phase 1;
	jr	z,step3
x:	ld	a,(de)		; get byte from source;
	ld	(hl),a		; store byte at destination;
	ld	a,b
	or	a
	jp	nz,y
	ld	a,c
	or	a
	jr	z,test		; if move is over, then go to
				;   TEST;
y:	dec	hl		; else keep a goin';
	dec	de
	dec	bc
	jr	x
test:	ld	a,(funk)	; move is now over;
	cp	02h
done:	jr	z,done		; if move only, then go to
				;   DONE, (this is endless loop);
step3:	push	hl		; save c;
	ld	h,d		; get a;
	ld	l,e		; place a in HL;
	call	comph
	pop	de
	add	hl,de		; DE := c-a;
	ld	(disp),hl	; this is the displacement;
	ld	hl,(start)	; get d;
	dec	hl
loop:	inc	hl
	ex	de,hl
	ld	hl,(stop)
	ex	de,hl
	ld	a,e
	sub	l
	ld	a,d
	sbc	a,h
done2:	jr	c,done2		; if reference fixing is com-
				;   pleted, then go to DONE2.
				;   (this is endless loop);
	ld	b,1ah		; get 3 byte op code count;
	ld	de,table3	; address of 3 byte op table;
chek3:	ld	a,(de)
	cp	(hl)
	jr	z,act		; if next byte in memory is
				;   start of a 3 byte
				;   go to ACT;
	dec	b		; else try next op;
	inc	de
	jr	nz,chek3	; if ther are more 3 byte
				;   ops left to check, go
				;   back to CHEK3;
	ld	b,12h		; else perform same comparision
				; with table of 2 byte ops;
	ld	de,table2
chek2:	ld	a,(de)
	cp	(hl)
	jr	z,skip
	dec	b
	inc	de
	jr	nz,chek2	; if there are more 2 byte
				;   ops left to try, go back
	jr	loop		;   to CHEK2;
skip:	inc	hl		; else must be 1 byte,
	jr	loop		;   ignore it, skip the
				;   operand of a 2 byte
				;   instruction;
act:	push	hl		; a 3 byte operation;
	ld	hl,(sbot)	; get b;
	ld	d,h		; move b to DE;
	ld	e,l
	ld	hl,(sstrt)	; get a;
	ld	b,h		; move a to BC;
	ld	c,l
	pop	hl
	inc	hl		; HL points to B2-B3;
	ld	a,e
	sub	(hl)
	inc	hl
	ld	a,d
	sbc	a,(hl)
	jr	c,loop		; if operand is > b, ignore it
	
	dec	hl
	ld	a,(hl)
	sub	c
	inc	hl
	ld	a,(hl)
	sbc	a,b
	jr	c,loop		; if operand is < a, ignore it

	dec	hl
	ex	de,hl
	ld	hl,(disp)	; get displacement value;
	ex	de,hl
	ld	a,(hl)		; add to B2-B3;
	add	a,e
	ld	(hl),a
	inc	hl
	ld	a,(hl)
	adc	a,d
	ld	(hl),a
	jr	loop		; go look for more 3 byte
				;   operations
comph:	ld	a,h		; complement HL;
	cpl
	ld	h,a
	ld	a,l
	cpl
	ld	l,a
	inc	hl
	ret

table3:	db	001h,011h,021h	; table of 3 byte op codes;
	db	022h,02ah,031h
	db	032h,03ah,0c2h
	db	0c3h,0c4h,0cah
	db	0cch,0cdh,0d2h
	db	0d4h,0dah,0dch
	db	0e2h,0e4h,0eah
	db	0ech,0f2h,0f4h
	db	0fah,0fch

table2:	db	006h,00eh,016h	; table of 2 byte op codes;
	db	01eh,026h,02eh
	db	036h,03eh,0c6h
	db	0ceh,0d3h,0d6h
	db	0dbh,0deh,0e6h
	db	0eeh,0f6h,0feh

sstrt:	ds	2		; source start     a;
sbot:	ds	2		; source bottom    b;
dtop:	ds	2		; destination      c;
start:	ds	2		; start phase 2    d;
stop:	ds	2		; end phase 2      e;
funk:	ds	1		; function select  f;
disp:	ds	2		; displacement (program defined);

	ds	21
stack:
	.dephase

	end

