abtav		dw	0
abhossz		dw	0
babtav		dw	257			;	(0...255 => 2...257)
babhossz	dw	258			;	(1...255 => 4...258)

pack:		push	cx			;	LZ77 + BYTERUN:
		mov	ax,cs:babhossz		;	Be:
		push	si			;	DS:SI = honnan,
		mov	cs:abhossz,ax		;	ES:DI = hov ,
		push	ds			;	CX = mennyit.
		xor	ax,ax			;	Ki:
		push	di			;	DS:SI <=> ES:DI,
		mov	cs:abtav,ax		;	DX = CX,
		mov	bp,cx			;	CX = £j hossz.
		push	es
		jcxz	neppa
		push	di
		mov	cx,256
		rep	stosw
		mov	cx,bp
		pop	di

palker:		lodsb
		mov	bx,ax
		add	bx,bx
		inc	wr es:[di][bx]
		loop	palker
		push	di
		mov	ax,-1
		xor	bx,bx
		sub	si,bp
		inc	ch

palkis:		scasw
		sjbe	paltov
		lea	bx,[di-2]
		mov	ax,es:[bx]
		or	ax,ax
		sjz	plmegv
paltov:		loop	palkis
plmegv:		mov	dx,bx
		pop	di
		mov	ax,-1
		sub	dx,di
		mov	es:[bx],ax
		shr	dx,1
		mov	cx,256
		push	di
		xor	bx,bx

palkis2:	scasw
		sjbe	paltov2
		lea	bx,[di-2]
		mov	ax,es:[bx]
		or	ax,ax
		sjz	plmegv2
paltov2:	loop	palkis2
plmegv2:	pop	di
		sub	bx,di
		shr	bx,1
		mov	dh,bl
		mov	ax,dx
neppa:		xor	cx,cx
		stosw
		or	bp,bp
		stosw
		sjnz	tolop
		njmp	veppa

tlolp:		sja	blolp
		add	cx,bx
		dec	bx
		mov	cs:abtav,cx
		sjz	tolop
		cmp	cx,cs:babtav
		sjbe	tolop
blolp:		mov	cx,cs:babtav
		mov	cs:abtav,cx
		sjmp	tolop

tolp:		mov	bx,1
bolp:		mov	cx,cs:abtav
		cmp	cx,cs:babtav
		sjne	tlolp
tolop:		cmp	bp,cs:abhossz
		push	dx
		sjae	bolop
		mov	cs:abhossz,bp
bolop:		mov	ax,si
		push	bp
		mov	bx,si
		push	di
		sub	ax,cx
		push	es
		xor	dx,dx
		push	ds
		mov	bp,3
		pop	es
tmorp:		mov	cx,bx
		mov	di,ax
		mov	si,bx
		sub	cx,ax
		lodsb
		sjbe	tmegy
		repne	scasb
		sjne	tmegy
		mov	ax,di
		cmpsb
		sjne	tmorp
		cmpsb
		mov	cx,cs:abhossz
		sjne	tmorp
		sub	cx,3
		sjbe	tmorp
		inc	cx
		repe	cmpsb
		neg	cx
		add	cx,cs:abhossz
		cmp	cx,bp
		sjbe	tmorp
		lea	dx,[bx+1]
		mov	bp,cx
		sub	dx,ax
		cmp	cx,cs:abhossz
		lea	si,[bx+1]
		sjb	tmorp
		mov	al,[bx]
		sjmp	nasces
tmegy:		mov	di,si
		scasb
		sjne	nescas
		scasb
		mov	cx,cs:abhossz
		sjne	nescas
		sub	cx,3
		sjbe	nescas
		inc	cx
		repe	scasb
		neg	cx
		add	cx,cs:abhossz
		cmp	cx,bp
		sjb	nescas
		mov	bp,cx
		mov	dx,-1
nescas:		mov	cx,bp
nasces:		pop	es
		mov	bx,bp
		pop	di
		sub	cx,3
		pop	bp
		mov	ah,cl
		mov	cx,dx
		pop	dx
		sjbe	nettal
		or	cx,cx
		sjs	escas

		sub	cx,2
		sjb	nettal
		mov	al,dl
		lea	si,[si][bx-1]
		stosw
		mov	al,cl
		sub	bp,bx
		stosb
		sjbe	veppa
		njmp	bolp

escas:		mov	cl,al
		mov	al,dh
		lea	si,[si][bx-1]
		stosw
		mov	al,cl
		sub	bp,bx
		stosb
		sjbe	veppa
		njmp	bolp

nettal:		cmp	al,dl
		sje	jelby
		cmp	al,dh
		sje	jelby
		dec	bp
		stosb
		sjz	veppa
		njmp	tolp

jelby:		xor	ah,ah
		dec	bp
		stosw
		sjz	veppa
		njmp	tolp

veppa:		pop	ds
		mov	cx,di
		pop	si
		sub	cx,4
		pop	es
		sub	cx,si
		mov	al,-1
		xchg	cl,ch
		pop	di
		mov	[si],cx
		cmp	al,cl
		pop	dx
		xchg	cl,ch
		sje	pmask
		add	cx,4
		cmp	cx,dx
		sjbe	pock
pmask:		push	si
		push	di
		mov	cx,dx
		push	es
		xchg	si,di
		push	ds
		shr	cx,1
		pop	es
		pop	ds
		stosb
		sjnc	pk0
		movsb
pk0:		rep	movsw
		push	ds
		push	es
		mov	cx,dx
		pop	ds
		pop	es
		inc	cx
		pop	di
		pop	si
pock:		ret

unpack:		push	si			;	LZ77 + BYTERUN:
		lodsw				;	l sd fent!
		push	cx			;	+ Be:
		xchg	al,ah			;	CX = a t”m”r¡tett
		push	ds			;	adat teljes hossza!
		or	ax,ax			;	(CX = 0 => nincs is)
		push	di
		mov	bp,ax
		push	es
		sjz	uppa
		cmp	ah,-1
		jcxz	uppa
		sjne	puck
		dec	si
		dec	cx
		sjz	uppa
		shr	cx,1
		sjnc	pk1
		movsb
pk1:		rep	movsw
		sjmp	uppa

puck:		sub	cx,4
		sjbe	uppa
		cmp	bp,cx
		lodsw
		sjbe	pucka
		mov	bp,cx
pucka:		xor	cx,cx
		cmp	al,ah
		xchg	ax,dx
		sje	uppa

unplp:		lodsb
unpsp:		cmp	al,dl
		sje	jeleb1
		cmp	al,dh
		sje	jeleb2
		dec	bp
		stosb
		sjnz	unplp

uppa:		pop	ds
		mov	cx,di
		pop	si
		pop	es
		sub	cx,si
		pop	dx
		pop	di
		ret

unsp1:		mov	al,dl
		sub	bp,2
		stosb
		mov	al,ah
		sja	unpsp
		sjmp	uppa

unsp2:		mov	al,dh
		sub	bp,2
		stosb
		mov	al,ah
		sja	unpsp
		sjmp	uppa

jeleb1:		lodsw
		or	al,al
		mov	cl,al
		sjz	unsp1
		mov	al,ah
		push	si
		mov	ah,ch
		push	ds
		add	ax,2
		mov	si,di
		add	cx,3
		push	es
		sub	si,ax
		shr	cx,1
		pop	ds
		sjnc	jb1
		movsb
jb1:		sub	bp,3
		rep	movsw
		pop	ds
		pop	si
		sja	unplp
		sjmp	uppa

jeleb2:		lodsw
		or	al,al
		mov	cl,al
		sjz	unsp2
		add	cx,3		
		mov	al,ah
		shr	cx,1
		sjnc	jb2
		stosb
jb2:		sub	bp,3
		rep	stosw
		sja	unplp
		sjmp	uppa
