;=========================================================
; File:    RLEc.asm
; Purpose: Show how to do RLE compression
;=========================================================

include 'emu8086.inc'

    org  100h                  ; set location counter to 100h
    
    jmp  CodeStart             ; jump to program start

DataStart:
; this is the data to be run-length compressed
Data     dw  4, 9, 9, 9, 3, 3, 3, 3, 3, 7, 7, 0
Space    db  ' ', 0
Fini     db  13, 10, 13, 10, "Fini", 13, 10, 0

CodeStart:
    
    mov  bx, offset Data       ; pointer to base of the array
    
OuterLoop:
         
         cmp  [bx], 0          ; check for end of array
         je   Done
         
         mov  ax, 1            ; initialize counter to 1
         mov  dx, [bx]         ; copy data[bx] into dx
         
Lup:
              add  bx, 2       ; look at next location
              cmp  dx, [bx]    ; check for match
              jne  Output      ; no match; done with sequence
              inc  ax          ; increment counter
              jmp  Lup         ; go back for more
              
              
Output:
         
         call print_num        ; ax contains the counter
         mov  si, offset space
         call print_string     ; print counter
         mov  ax, dx
         call print_num        ; dx contains the data number
         mov  si, offset space
         call print_string     ; print data number
         loop OuterLoop        ; go back for more
         
Done:
    mov  ax, 0                 ; print final zero
    call print_num
    mov  si, offset Fini       ; print final message
    call print_string
    ret                        ; return to caller
    
DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
