	LHLD	6		;Load stack
	SPHL
	CALL	INIT		;Init relocator

	LXI	H,CODE		;Load code
	LXI	D,1234H		;Destination address
	PUSH	D
	MVI	B,CODLEN
MV:
	MOV	A,M
	STAX	D		;Unpack code
	INX	H
	INX	D
	DCR	B
	JNZ	MV
	POP	H
	PCHL			;Execute code
; -------------------------------
; Code moved into memory anywhere
; -------------------------------
CODE:
	RST	1
	LXI	D,TAB-$
NEXT:
	LDAX	D		;Return content
	INX	D
	ORA	A		;Test zero
	RST	1
	JNZ	NEXT-$		;Nope
	JMP	0

TAB:
	DB	7,0
CODLEN	EQU	$-CODE
; -------------------------------

;
;	Program for initializing modifier
;
INIT:	LXI	H,0E9E1H        ;Put POP H, PCHL at RST 1
	SHLD	0008H
	RST	1               ;Get PC in H,L
	LXI	D,PUT-$	        ;Add offset to get PUT
	DAD	D
	MOV	B,H             ;Move PUT to B,C
	MOV	C,L
	RST	1               ;Get PC in H,L
	LXI	D,RELRT-$       ;Add offset to get RELRT
	DAD	D
	LXI	D,0008H         ;Set D,E to RST 1
	MVI	A,RELLEN          ;Set A to byte count
PUT:	PUSH	B               ;Transfer to RST 1
	PUSH	PSW
	MOV	A,M
	STAX	D
	INX	H
	INX	D
	POP	PSW
	DCR	A
	RNZ
	POP	B		;Clean up stack
	RET			;Continue with program
				;(Can be changed to RET if
				;initialization complete)
;
RELRT:
;
;	Program for modifying addresses 
;
RST1:	XTHL		;Save H,L and get next PC
	DCX	H	;Change RST 1 to NOP.
	MVI	M,00H
	INX	H
	INX	H
	PUSH	D	;Save D,E.
	MOV	E,M	;Get relative addr. in D, E.
	INX	H
	MOV	D,M
	PUSH	PSW	;Save condition codes.
	XCHG		;Add offset for abs. addr.
	DAD	D
	XCHG
	DCX	D	;Set to beginning of instr
	DCX	D
	MOV	M,D	;Store absolute addr.
	DCX	H
	MOV	M,E
	POP	PSW	;Restore condition codes.
	POP	D	;Restore D,E.
	DCX	H	;Set H,L to start of instr
	XTHL		;Restore H,L
	RET		;Execute.

RELLEN	equ	$-RELRT

	END

