home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1989 / 02 / zigon.asc < prev   
Text File  |  1989-01-04  |  4KB  |  99 lines

  1. _Run Length Encoding_
  2. by Robert Zigon
  3.  
  4. [LISTING ONE]
  5.  
  6. ;--------------------------------------------------------------------
  7. ; RLE.asm   Run Length Encoding Routines
  8. ; Author : Bob Zigon
  9. ;--------------------------------------------------------------------
  10. ; -------------------------------------------------------------------
  11. ; PACK
  12. ;    This routine will pack a source buffer into a destination buffer
  13. ;    by reducing sequences of identical characters down to a 1 byte
  14. ;    repetition count and a 1 byte character.
  15. ;    INPUT : DS:SI -- Points to source buffer to pack
  16. ;            ES:DI -- Points to destination 
  17. ;            DX    -- Number of characters in source buffer to pack
  18. ;    OUTPUT: DI    -- Is updated to allow for multiple calls. 
  19. ; -------------------------------------------------------------------
  20. pack           proc   near
  21.                push   ax
  22.                push   bx
  23.                push   cx
  24.                lodsb
  25.                mov    bl,al
  26.                xor    cx,cx            ; Counts number of characters packed
  27.                cld                     ; All moves are forward
  28.  
  29. p10:           lodsb                   ; Get chara into AL
  30.                inc   cx                ; Inc chara count
  31.                sub   dx,1              ; 
  32.                je    p30               ; Exit when DX = 0
  33.                cmp   cx,255            ; 255 characters in this block yet?
  34.                jne   p20               ; If not then jump
  35.  
  36.                mov   [di],cl           ; output the length
  37.                inc   di
  38.                xor   cx,cx
  39.                mov   [di],bl           ; output the character
  40.                inc   di
  41.  
  42. p20:           cmp   al,bl             ; Has there been a character transition?
  43.                je    p10               ; NOPE ... so go back for more
  44.  
  45.                mov   [di],cl           ; output the length
  46.                inc   di
  47.                xor   cx,cx
  48.                mov   [di],bl           ; output the character
  49.                inc   di
  50.  
  51.                mov   bl,al             ; Move Latest chara into BL
  52.                jmp   p10               ; Loop back for more
  53. ;
  54. ;  We will get here when DX finally goes to zero.
  55. ;
  56. p30:           mov   al,cl             ; Output the length
  57.                stosb
  58.                mov   al,bl             ; Output the character
  59.                stosb
  60.  
  61.                pop   cx
  62.                pop   bx
  63.                pop   ax
  64.                ret
  65. pack           endp
  66.  
  67. ; -------------------------------------------------------------------
  68. ; UNPACK
  69. ;    This routine will unpack a sequence of characters that were
  70. ;    previously PACKed.
  71. ;    NOTE  : Source Region must be terminated with a NULL.
  72. ;    INPUT : DS:SI -- Source buffer to unpack
  73. ;            ES:DI -- Destination buffer to unpack into
  74. ;    OUTPUT:  
  75. ; -------------------------------------------------------------------
  76. unpack         proc   near
  77.                push   ax
  78.                push   cx
  79.                xor    cx,cx    ; Make sure CH is zero
  80.                cld             ; All moves are forward
  81.  
  82. unp10:         lodsb           ; Load into AL a character from DS:[SI]
  83.                cmp  al,0       ; Length of zero is end of region
  84.                je   unp40      ; 
  85.                mov  cl,al      ; Length goes into CL
  86.                lodsb           ; AL has chara to repeat
  87.  
  88.                rep stosb       ; Store AL to [DI] and repeat while CX <> 0
  89.                jmp unp10       ; Loop back forever
  90.  
  91. unp40:         pop  cx
  92.                pop  ax
  93.                ret
  94. unpack         endp
  95.  
  96.  
  97.  
  98. -30-
  99.