	ORG	0D00H
	LXI	SP,STACK
	LHLD    SBOT		; get b;
	MOV     D,H		; move b to DE;
	MOV     E,L
	LHLD    DTOP		; get c;
	MOV     B,H		; move c to BC;
	MOV     C,L
	LHLD    SSTRT		; get a;
	PUSH    B		; save c;
	CALL    COMPH		; complement HL;
	DAD     D		; HL := b-a;
	MOV     B,H		; move to BC;
	MOV     C,L
	POP     H		; get c;
	DAD     B		; set HL equal to bottom of
				;   destination area;
	LDA     FUNK		; get f;
	ORA     A		; if f=0, then slip phase 1;
	JZ      STEP3
X:	LDAX    D		; get byte from source;
	MOV     M,A		; store byte at destination;
	MOV     A,B
	ORA     A
	JNZ     Y
	MOV     A,C
	ORA     A
	JZ      TEST		; if move is over, then go to
				;   TEST;
Y:	DCX     H		; else keep a goin';
	DCX     D
	DCX     B
	JMP     X
TEST:	LDA     FUNK		; move is now over;
	CPI     02H
DONE:	JZ      DONE		; if move only, then go to
				;   DONE, (this is endless loop);
STEP3:	PUSH    H		; save c;
	MOV     H,D		; get a;
	MOV     L,E		; place a in HL;
	CALL    COMPH
	POP     D
	DAD     D		; DE := c-a;
	SHLD    DISP		; this is the displacement;
	LHLD    START		; get d;
	DCX     H
LOOP:	INX     H
	XCHG
	LHLD    STOP
	XCHG
	MOV     A,E
	SUB     L
	MOV     A,D
	SBB     H
DONE2:	JC      DONE2		; if reference fixing is com-
				;   pleted, then go to DONE2.
				;   (this is endless loop);
	MVI     B,1AH		; get 3 byte op code count;
	LXI     D,TABLE3		; address of 3 byte op table;
CHEK3:	LDAX    D
	CMP     M
	JZ      ACT		; if next byte in memory is
				;   start of a 3 byte
				;   go to ACT;
	DCR     B		; else try next op;
	INX     D
	JNZ     CHEK3		; if ther are more 3 byte
				;   ops left to check, go
				;   back to CHEK3;
	MVI     B,12H		; else perform same comparision
				; with table of 2 byte ops;
	LXI     D,TABLE2
CHEK2:	LDAX    D
	CMP     M
	JZ      SKIP
	DCR     B
	INX     D
	JNZ     CHEK2		; if there are more 2 byte
				;   ops left to try, go back
	JMP	LOOP		;   to CHEK2;
SKIP:	INX     H		; else must be 1 byte,
	JMP     LOOP		;   ignore it, skip the
				;   operand of a 2 byte
				;   instruction;
ACT:	PUSH    H		; a 3 byte operation;
	LHLD    SBOT		; get b;
	MOV     D,H		; move b to DE;
	MOV     E,L
	LHLD    SSTRT		; get a;
	MOV     B,H		; move a to BC;
	MOV     C,L
	POP     H
	INX     H		; HL points to B2-B3;
	MOV     A,E
	SUB     M
	INX     H
	MOV     A,D
	SBB     M
	JC      LOOP		; if operand is > b, ignore it
 
	DCX     H
	MOV     A,M
	SUB     C
	INX     H
	MOV     A,M
	SBB     B
	JC      LOOP		; if operand is < a, ignore it

	DCX     H
	XCHG
	LHLD    DISP		; get displacement value;
	XCHG
	MOV     A,M		; add to B2-B3;
	ADD     E
	MOV     M,A
	INX     H
	MOV     A,M
	ADC     D
	MOV     M,A
	JMP     LOOP		; go look for more 3 byte
				;   operations
COMPH:	MOV     A,H		; complement HL;
	CMA
	MOV	H,A
	MOV     A,L
	CMA
	MOV     L,A
	INX     H
	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:

