home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / ASM / ALIB30A / RESTOR.ASM < prev    next >
Assembly Source File  |  1994-11-15  |  7KB  |  270 lines

  1. page    59,132
  2. comment 
  3.                              COMPRES & RESTOR
  4.                              ----------------
  5.  
  6.      Purpose:
  7.      --------
  8.  
  9.      COMPRES and RESTOR are sample program to show how huffman
  10.      compression works.  Single files can be compressed by the
  11.      COMPRES program, then decompressed by the RESTOR program.
  12.  
  13.      Using COMPRES & RESTOR
  14.      ----------------------
  15.  
  16.      To compress a file type "COMPRES file1 file2".  The first
  17.      file (file1) is the file to be compressed and the
  18.      second file is what to call the result.  RESTOR works
  19.      in the reverse.  "RESTOR file1 file2" will decompress
  20.      file1 and put at file2.
  21.  
  22.      Compression Overview
  23.      --------------------
  24.  
  25.      The compression/decompression engines are initiated with a
  26.      pointer to two routines.  One routine will feed data to the
  27.      engine and the other will store the output.  Next, one call
  28.      to the compression entry point will continue compressing
  29.      until the input (feed) routine runs out of data.
  30.      
  31.      Performance
  32.      -----------
  33.  
  34.      Compression speed and size is respectable, but not competative
  35.      with programs such as PKZIP and ARJ.  For most compression jobs
  36.      of less than a megabyte the speed will be acceptable.
  37. 
  38.  
  39.      
  40.     extrn    shrink:far
  41.     extrn    expand:far
  42.     extrn    DWORD_TO_HEX_CRT:far
  43.     extrn    library_setup:far
  44.     extrn    library_terminate:far
  45. ;;    extrn    lib_error_handler:far
  46.     extrn    parse_first:far
  47.     extrn    parse_next:far
  48.     extrn    SHOW_CURSOR:far
  49.     extrn    key_read:far
  50.     extrn    lib_info:byte    
  51.     
  52. .xlist
  53.     include  mac.inc
  54.     include    common.inc
  55. .list    
  56.   
  57. code    segment
  58.     assume    cs:code,ds:nothing
  59. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  60. ; FEED - fill buffer for shrink/expand
  61. ;  inputs: ds:dx = buffer ptr of size (disk_buf_size)
  62. ;             ax = requested amount
  63. ;  output:    ax = number of bytes placed in buffer, 0=end of input
  64. ;-------------------
  65. feed_handle    dw    0
  66. feed_count    dd    0
  67. ;-------------------
  68. feed    proc    far
  69.     apush    cx,bx
  70.     mov    cx,ax            ;amount of data to read
  71.     mov    bx,cs:feed_handle    ;file handle
  72.     mov    ah,3fh            ;read code
  73.     int    21h
  74.     add    word ptr cs:feed_count,ax
  75.     adc    word ptr cs:feed_count+2,0
  76.     apush    ax,dx
  77.     mov    ah,3
  78.     mov    bh,0
  79.     int    10h            ;read cursor posn -> dx
  80.     mov    dl,8
  81.     mov    ah,2
  82.     int    10h            ;set cursor posn
  83.     mov    ax,word ptr cs:feed_count    
  84.     mov    dx,word ptr cs:feed_count+2
  85.     call    DWORD_TO_HEX_CRT
  86.     apop    dx,ax
  87.     apop    bx,cx
  88.     retf
  89. feed    endp
  90.  
  91. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  92. ; STORE - save buffer data somewhere
  93. ;  inputs:  ds:dx = buffer ptr
  94. ;           ax = amount of data present in buffer
  95. ;  output:  none
  96. ;-------------------
  97. store_handle    dw    0
  98. store_count    dd    0
  99. ;-------------------
  100. store    proc    far
  101.     apush    ax,bx,cx
  102.     add    word ptr cs:store_count,ax
  103.     adc    word ptr cs:store_count+2,0
  104.     mov    cx,ax            ;set write length
  105.     mov    bx,cs:store_handle
  106.     mov    ah,40h
  107.     int    21h
  108.     push    dx
  109.     mov    ah,3
  110.     mov    bh,0
  111.     int    10h            ;read cursor posn -> dx
  112.     mov    dl,27
  113.     mov    ah,2
  114.     int    10h            ;set cursor posn
  115.     mov    ax,word ptr cs:store_count    
  116.     mov    dx,word ptr cs:store_count+2
  117.     call    DWORD_TO_HEX_CRT
  118.     pop    dx
  119.     apop    cx,bx,ax
  120.     retf
  121. store    endp
  122.  
  123.  
  124. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  125.  
  126.     assume    cs:code,ds:code
  127.  
  128. ;██████████████████████████████████████████████████████████████████████████
  129.     
  130. infile_asciiz    db    40 dup (0)
  131. outfile_asciiz    db    40 dup (0)
  132. help_msg      db 0dh,0ah
  133.           db  'RESTOR uncompresses a file which has been COMPRESSed.',0dh,0Ah
  134.               db  'usage: ',0dh,0Ah
  135.               db  '   RESTOR <file1> <file2> ',0dh,0ah
  136.               db 0dh,0ah
  137.               db  ' <file1> is restored to <file2>',0dh,0ah
  138.               db  'Press any key to continue.$'
  139. info_msg    db    'Reading            Writing$'              
  140. ;--------------------------
  141. start           proc    far
  142.         cld
  143.         mov    bp,ds            ;save PSP seg
  144.         mov    bx,cs
  145.         mov    ds,bx
  146.         mov    es,bx
  147.  
  148. ; next, release memory beyond the end of the program
  149. ; The  definition for ZSEG marks the
  150. ; end of the program's code, data and stack area.
  151.  
  152.     mov    ax,zseg
  153.  
  154.     mov    bx,bp               ; first segment (ES = seg PSP)
  155.     mov    es,bp
  156.     sub    bx,ax
  157.     neg    bx            ; size of program in paragraphs
  158.     mov    ah,4Ah            ; resize memory block
  159.     int    21h
  160.     push    ds
  161.     pop    es
  162. ;
  163. ; check if enough memory free to run program
  164. ;
  165.     mov    ax,bp            ;pass psp segment to setup
  166.     mov    bx,0            ;number of floating point variables
  167.     call    library_setup
  168.     cmp    ax,128
  169.     jae    got_enough_mem        ;jmp if 128k of memory avail
  170. ;;    mov    al,7
  171. ;;    mov    ah,fatal_return
  172. ;;    call    lib_error_handler
  173.     jmp    exitx
  174.     
  175. got_enough_mem:
  176.     push    ds
  177.     pop    es
  178.     mov    di,offset infile_asciiz
  179.     call    parse_first
  180.     cmp    bh,7
  181.     jne    parse_err
  182.     mov    di,offset outfile_asciiz
  183.     call    parse_next
  184.     cmp    bh,7
  185.     jne    parse_err
  186. ;
  187. ; open the input file
  188. ;
  189.     mov    ax,3d00h
  190.     mov    dx,offset infile_asciiz
  191.     int    21h
  192.     jc    parse_err
  193.     mov    feed_handle,ax
  194.     mov    ax,3c00h
  195.     mov    cx,0                ;file attributes
  196.     mov    dx,offset outfile_asciiz
  197.     int    21h
  198.     mov    store_handle,ax
  199. ;
  200. ; setup to display
  201. ;
  202.     mov    dx,1800h
  203.     call    SHOW_CURSOR
  204.     mov    ah,9
  205.     mov    dx,offset info_msg
  206.     int    21h    
  207. ;
  208. ; setup for shrink/expand call
  209. ;
  210.     mov    si,offset feed
  211.     mov    di,offset store
  212.     push    cs
  213.     pop    es
  214. ;
  215. ; decode operation type
  216. ;
  217.     call    expand
  218.     jmp    exitx
  219.  
  220. parse_err:
  221.     mov    dx,offset help_msg
  222.     mov    ah,9
  223.     int    21h
  224.     call    key_read
  225.  
  226. ; normal program exit
  227.  
  228. exitx:    mov    bx,feed_handle
  229.     cmp    bx,0
  230.     je    exitx1
  231.     mov    ah,3eh
  232.     int    21h            ;close infile
  233. exitx1:    mov    bx,store_handle
  234.     cmp    bx,0
  235.     je    exitx2
  236.     mov    ah,3eh
  237.     int    21h
  238.         
  239. exitx2:    mov    ax,1            ;do not clear screen
  240.     call    library_terminate
  241.     mov    ax,4C00h
  242.     int    21h
  243.     
  244. start           endp
  245.   
  246. ;▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  247.   
  248. code    ENDS
  249. ;--------------------------------------------------------- stack_seg_e  ---
  250. stack     segment para stack
  251.                 db      2048 dup (0)
  252. stack     ends
  253. ;-------------------------------------------------------------------------
  254. ;
  255. ; This segment definition is needed so linker will put the LIBSEG here
  256. ; before the ZSEG.  We want ZSEG to be last so memory allocation will
  257. ; work correctly.
  258. ;
  259. LIBSEG           segment byte public 'LIB'
  260. LIBSEG    ENDS
  261. ;-------------------------------------------------------------------------
  262. ; zseg must be at the end of the program for memory allocation from
  263. ; DOS.
  264. ;
  265. ;
  266. zseg    segment para public 'ZZ'
  267. zseg    ends
  268.  
  269.                 end     start
  270.