
k3              equ     1

k1              equ     512
k2              equ     8192  ;1024

                .model  tpascal
                .386p
                .code
                assume  cs:code, ds:code, es:code, ss:code
                locals  @@
                jumps

                org     100h
start:
                mov     ax, 3
                int     10h

                mov     ah, 9
                lea     dx, msg1
                int     21h

                call    pack

                mov     ah, 9
                lea     dx, msg2
                int     21h

                call    unpack

                mov     ax, 4c00h
                int     21h

msg1            db      'packing...',13,10,'$'
msg2            db      'unpacking...',13,10,'$'

; ---------------------------------------------------------------------------

pack:           lea     dx, file1
                call    istream_init
                lea     dx, file2
                call    ostream_init

                call    tree_init
                call    tree_create

                call    treeman_init

@@cycle:        call    istream_getbyte
                jz      @@eof

                push    ax
                call    tree_getcode
                call    ostream_putcode
                pop     ax

                call    treeman

               ; int     29h

                jmp     @@cycle

@@eof:          call    istream_done
                call    ostream_done

                ret

; ---------------------------------------------------------------------------

unpack:         lea     dx, file2
                call    istream_init
                lea     dx, file3
                call    ostream_init

                call    tree_init
                call    tree_create

                call    treeman_init

@@cycle:        mov     ebx, tree_max

@@2:            call    istream_getbit
                jz      @@eof
                mov     ax, tree_prev1[ebx*2]
                jnc     @@1
                mov     ax, tree_prev2[ebx*2]
@@1:            mov     bx, ax
                cmp     bx, 255
                ja      @@2

                mov     al, bl
                call    ostream_putbyte

                call    treeman

               ; int     29h

                jmp     @@cycle

@@eof:          call    istream_done
                call    ostream_done

                ret

; ---------------------------------------------------------------------------

treeman_init:   mov     count1, 0
                mov     count2, 0
                mov     count3, 0
                ret

treeman:
                inc     count3
                cmp     count3, k3
                jne     @@3
                mov     count3, 0

                and     eax, 255
                inc     tree_count[eax*4]
                inc     tree_total
@@3:


                inc     count1
                cmp     count1, k1
                jne     @@1
                mov     count1, 0
                call    tree_create
@@1:

                inc     count2
                cmp     count2, k2
                jne     @@2
                mov     count2, 0
                call    tree_init
                call    tree_create
@@2:
                ret

; ---------------------------------------------------------------------------

file1           db      'i',0
file2           db      'o',0
file3           db      'x',0

include         istream.inc
include         ostream.inc
include         tree.inc

;include         treedump.inc

include         istream.d
include         ostream.d
include         tree.d

count1          dw      ?
count2          dw      ?
count3          dw      ?

                end     start
