home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
1989
/
02
/
zigon.asc
< prev
Wrap
Text File
|
1989-01-04
|
4KB
|
99 lines
_Run Length Encoding_
by Robert Zigon
[LISTING ONE]
;--------------------------------------------------------------------
; RLE.asm Run Length Encoding Routines
; Author : Bob Zigon
;--------------------------------------------------------------------
; -------------------------------------------------------------------
; PACK
; This routine will pack a source buffer into a destination buffer
; by reducing sequences of identical characters down to a 1 byte
; repetition count and a 1 byte character.
; INPUT : DS:SI -- Points to source buffer to pack
; ES:DI -- Points to destination
; DX -- Number of characters in source buffer to pack
; OUTPUT: DI -- Is updated to allow for multiple calls.
; -------------------------------------------------------------------
pack proc near
push ax
push bx
push cx
lodsb
mov bl,al
xor cx,cx ; Counts number of characters packed
cld ; All moves are forward
p10: lodsb ; Get chara into AL
inc cx ; Inc chara count
sub dx,1 ;
je p30 ; Exit when DX = 0
cmp cx,255 ; 255 characters in this block yet?
jne p20 ; If not then jump
mov [di],cl ; output the length
inc di
xor cx,cx
mov [di],bl ; output the character
inc di
p20: cmp al,bl ; Has there been a character transition?
je p10 ; NOPE ... so go back for more
mov [di],cl ; output the length
inc di
xor cx,cx
mov [di],bl ; output the character
inc di
mov bl,al ; Move Latest chara into BL
jmp p10 ; Loop back for more
;
; We will get here when DX finally goes to zero.
;
p30: mov al,cl ; Output the length
stosb
mov al,bl ; Output the character
stosb
pop cx
pop bx
pop ax
ret
pack endp
; -------------------------------------------------------------------
; UNPACK
; This routine will unpack a sequence of characters that were
; previously PACKed.
; NOTE : Source Region must be terminated with a NULL.
; INPUT : DS:SI -- Source buffer to unpack
; ES:DI -- Destination buffer to unpack into
; OUTPUT:
; -------------------------------------------------------------------
unpack proc near
push ax
push cx
xor cx,cx ; Make sure CH is zero
cld ; All moves are forward
unp10: lodsb ; Load into AL a character from DS:[SI]
cmp al,0 ; Length of zero is end of region
je unp40 ;
mov cl,al ; Length goes into CL
lodsb ; AL has chara to repeat
rep stosb ; Store AL to [DI] and repeat while CX <> 0
jmp unp10 ; Loop back forever
unp40: pop cx
pop ax
ret
unpack endp
-30-