home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / ddjcompr / comp2_as / decomp2.asm < prev   
Assembly Source File  |  1991-04-29  |  4KB  |  242 lines

  1. ; Copyright 1991 by Jussi Puttonen, Timo Raita and Jukka Teuhola
  2.  
  3. ; Written by Jussi Puttonen, 19.4.1991 at University of Turku, Finland
  4. ; Algorithms suggested by Timo Raita and Jukka Teuhola
  5.  
  6. InFile    equ    0
  7. OutFile    equ    1
  8.  
  9. ReadBufferSize        equ    16384
  10. WriteBufferSize    equ    16384
  11.  
  12. stack segment stack 'stack'
  13.         db 1024 dup (?)
  14. stack ends
  15.  
  16. data segment para public 'data'
  17.  
  18. FailString    db    'DECOMP failed.', 10,13, '$'
  19.  
  20.         align
  21.  
  22. ReadBuffer    db    ReadBufferSize dup (?)
  23. ReadBufferEnd    label byte
  24. ReadEnd        dw    offset ReadBufferEnd
  25.  
  26.         align
  27.  
  28. WriteBuffer    db    WriteBufferSize dup (?)
  29. WriteBufferEnd    label byte
  30.  
  31. nine            dw    9
  32.  
  33. data ends
  34.  
  35. table segment para public 'data'
  36.     db    8000h dup (' ')
  37.     db    8000h dup (' ')
  38. table ends
  39.  
  40. code segment para public 'code'
  41.     assume cs:code, ds:data, es:table
  42.  
  43. WriteBlock:
  44.             push    ax
  45.             push    bx
  46.             push    cx
  47.             push    dx
  48.             mov    ah, 40h
  49.             mov    bx, OutFile
  50.             mov    cx, WriteBufferSize
  51.             lea    dx, WriteBuffer
  52.             mov    di, dx
  53.             int    21h
  54.             pop    dx
  55.             pop    cx
  56.             pop    bx
  57.             pop    ax
  58.             ret
  59.  
  60. FlushBuffer    proc
  61.             mov    ah, 40h
  62.             mov    bx, OutFile
  63.             mov    cx, di
  64.             lea    dx, WriteBuffer
  65.             sub    cx, dx
  66.             int    21h
  67.             ret
  68. FlushBuffer    endp
  69.  
  70.  
  71. jProcessWithChecks:
  72.             jmp    ProcessWithChecks
  73.  
  74. start:        cld
  75.             mov    ax, data
  76.             mov    ds, ax
  77.             mov    ax, table
  78.             mov    es, ax
  79.  
  80. ; si contains pointer to ReadBuffer
  81.             mov    si, offset ReadBufferEnd
  82. ; di contains pointer to WriteBuffer
  83.             mov    di, offset WriteBuffer
  84. ; bx contains the Addr
  85.             mov    bx, 0
  86.  
  87. ReadLoop:        mov    ax, ReadEnd
  88.             sub    ax, si
  89.             xor    dx, dx
  90.             div    nine
  91.             cmp    ax, 1
  92.             jbe    jProcessWithChecks
  93.             mov    dx, ax
  94.             dec    dx
  95.  
  96.             mov    ax, offset WriteBufferEnd
  97.             sub    ax, di
  98.             mov    cl, 3
  99.             shr    ax, cl
  100.             cmp    ax, 1
  101.             jbe    jProcessWithChecks
  102.             dec    ax
  103.             mov    cx, ax
  104.  
  105.             cmp    cx, dx
  106.             jbe    MinFound
  107.             mov    cx, dx
  108. MinFound:
  109.  
  110. ByteLoop:        lodsb
  111.             mov    dl, al
  112. ProcessBit    macro
  113.             local zerobit, over
  114.             shl    dl, 1
  115.             jnc    zerobit
  116.             lodsb
  117.             mov    es:[bx], al
  118.             jmp    short over
  119. zerobit:        mov    al, es:[bx]
  120. over:        mov    bh, bl
  121.             mov    bl, al
  122.             mov    ds:[di], al
  123.             inc    di
  124.             endm
  125.  
  126.             ProcessBit
  127.             ProcessBit
  128.             ProcessBit
  129.             ProcessBit
  130.             ProcessBit
  131.             ProcessBit
  132.             ProcessBit
  133.             ProcessBit
  134.  
  135.             loop    jByteLoop
  136.             jmp    ReadLoop
  137. jByteLoop:    jmp    ByteLoop
  138.  
  139. ProcessWithChecks:
  140. ; read a byte
  141.         cmp    si, ReadEnd
  142.         jb    noreadblock$1
  143.         push    bx
  144.         mov    dx, offset ReadBuffer
  145.         mov    si, dx
  146.         mov    bx, InFile
  147.         mov    ah, 3Fh
  148.         mov    cx, ReadBufferSize
  149.         int    21h
  150.         pop    bx
  151.         jnc    readsuccess$1
  152.         jmp    fail
  153. readsuccess$1:
  154.         or    ax, ax
  155.         jne    notready$1
  156.         jmp    ready
  157. notready$1:
  158.         add    ax, offset ReadBuffer
  159.         mov    ReadEnd, ax
  160. noreadblock$1:
  161.         lodsb
  162. ;
  163.         mov    dl, al
  164.         mov    cx, 8
  165. bitloop:
  166.         shl    dl, 1
  167.         jnc    zerobit
  168. onebit:
  169. ; read a byte
  170.         cmp    si, ReadEnd
  171.         jb    noreadblock$2
  172.         push    bx
  173.         push    cx
  174.         push    dx
  175.         mov    dx, offset ReadBuffer
  176.         mov    si, dx
  177.         mov    bx, InFile
  178.         mov    ah, 3Fh
  179.         mov    cx, ReadBufferSize
  180.         int    21h
  181.         pop    dx
  182.         pop    cx
  183.         pop    bx
  184.         jnc    readsuccess$2
  185.         jmp    fail
  186. readsuccess$2:
  187.         or    ax, ax
  188.         jne    notready$2
  189.         jmp    ready
  190. notready$2:
  191.         add    ax, offset ReadBuffer
  192.         mov    ReadEnd, ax
  193. noreadblock$2:
  194.         lodsb
  195. ;
  196.         mov    es:[bx], al
  197.         mov    bh, bl
  198.         mov    bl, al
  199. ; Write a byte
  200.         cmp    di, offset WriteBufferEnd
  201.         jb    nowriteblock$1
  202.         call    WriteBlock
  203. nowriteblock$1:
  204.         mov    ds:[di], al
  205.         inc    di
  206. ;
  207.         loop    bitloop
  208.         jmp    ReadLoop
  209.  
  210. zerobit:
  211.         mov    al, es:[bx]
  212.         mov    bh, bl
  213.         mov    bl, al
  214. ; Write a byte
  215.         cmp    di, offset WriteBufferEnd
  216.         jb    nowriteblock$2
  217.         call    WriteBlock
  218. nowriteblock$2:
  219.         mov    ds:[di], al
  220.         inc    di
  221. ;
  222.         loop    bitloop
  223.         jmp    ReadLoop
  224.  
  225.  
  226. ready:    call    FlushBuffer
  227.  
  228.         mov    ax, 4c00h
  229.         int    21h
  230.  
  231. fail:
  232.         mov    dx, offset FailString
  233.         mov    ah, 9
  234.         int    21h
  235.         mov    ax, 4c01h
  236.         int    21h
  237.  
  238. code ends
  239.  
  240.     end start
  241.  
  242.