home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZIP19P1.ZIP / msdos / match.asm < prev    next >
Assembly Source File  |  1993-01-23  |  8KB  |  222 lines

  1. ;
  2. ; Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, and Jean-loup Gailly.
  3. ; Permission is granted to any individual or institution to use, copy, or
  4. ; redistribute this software so long as all of the original files are included
  5. ; unmodified, that it is not sold for profit, and that this copyright notice
  6. ; is retained.
  7. ;
  8. ; match.asm by Jean-loup Gailly.
  9.  
  10. ; match.asm, optimized version of longest_match() in deflate.c
  11. ; Must be assembled with masm -ml. To be used only with C compact model.
  12. ; (For large model, follow the instructions given below.)
  13. ; This file is only optional. If you don't have masm or tasm, use the
  14. ; C version (add -DNO_ASM to CFLAGS in makefile.msc and remove match.obj
  15. ; from OBJI). If you have reduced WSIZE in zip.h, then change its value
  16. ; below.
  17. ;
  18. ; Turbo C 2.0 does not support static allocation of more than 64K bytes per
  19. ; file, and does not have SS == DS. So TC and BC++ users must use:
  20. ;   tasm -ml -DDYN_ALLOC -DSS_NEQ_DS match;
  21. ;
  22. ; To simplify the code, the option -DDYN_ALLOC is supported for OS/2
  23. ; only if the arrays are guaranteed to have zero offset (allocated by
  24. ; halloc). We also require SS==DS. This is satisfied for MSC but not Turbo C.
  25.  
  26.         name    match
  27.  
  28. ifndef DYN_ALLOC
  29.         extrn   _prev         : word
  30.         extrn   _window       : byte
  31.         prev    equ  _prev    ; offset part
  32.         window  equ  _window
  33. endif
  34.  
  35. _DATA    segment  word public 'DATA'
  36.         extrn   _match_start  : word
  37.         extrn   _prev_length  : word
  38.         extrn   _good_match   : word
  39.         extrn   _strstart     : word
  40.         extrn   _max_chain_length : word
  41. ifdef DYN_ALLOC
  42.         extrn   _prev         : word
  43.         extrn   _window       : word
  44.         prev    equ 0         ; offset forced to zero
  45.         window  equ 0
  46.         window_seg equ _window[2]
  47.     window_off equ 0
  48. else
  49.     wseg    dw seg _window
  50.         window_seg equ wseg
  51.     window_off equ offset _window
  52. endif
  53. _DATA    ends
  54.  
  55. DGROUP  group _DATA
  56.  
  57. _TEXT   segment word public 'CODE'
  58.         assume  cs: _TEXT, ds: DGROUP
  59.  
  60.     public _match_init
  61.         public _longest_match
  62.  
  63.     MIN_MATCH     equ 3
  64.         MAX_MATCH     equ 258
  65.     WSIZE         equ 32768        ; keep in sync with zip.h !
  66.     MIN_LOOKAHEAD equ (MAX_MATCH+MIN_MATCH+1)
  67.     MAX_DIST      equ (WSIZE-MIN_LOOKAHEAD)
  68.  
  69. prev_ptr    dw  seg _prev        ; pointer to the prev array
  70. ifdef SS_NEQ_DS
  71.     match_start dw  0            ; copy of _match_start if SS != DS
  72. endif
  73.  
  74. ; initialize or check the variables used in match.asm.
  75.  
  76. _match_init proc near            ; 'proc far' for large model
  77. ifdef SS_NEQ_DS
  78.         ma_start equ cs:match_start    ; does not work on OS/2
  79. else
  80.     assume ss: DGROUP
  81.         ma_start equ ss:_match_start
  82.         mov     ax,ds
  83.         mov     bx,ss
  84.         cmp     ax,bx                   ; SS == DS?
  85.         jne     error
  86. endif
  87. ifdef DYN_ALLOC
  88.     cmp    _prev[0],0        ; verify zero offset
  89.     jne    error
  90.     cmp    _window[0],0
  91.     jne    error
  92.   ifdef SS_NEQ_DS
  93.     mov    ax,_prev[2]        ; segment value
  94.     mov     cs:prev_ptr,ax        ; ugly write to code, crash on OS/2
  95.         prev_seg  equ cs:prev_ptr
  96.   else
  97.         prev_seg  equ ss:_prev[2]    ; works on OS/2 if SS == DS
  98.   endif
  99. else
  100.         prev_seg  equ cs:prev_ptr
  101. endif
  102.     ret
  103.     extrn   _exit : near        ; 'far' for large model
  104. error:  call    _exit
  105.  
  106. _match_init endp
  107.  
  108. ; -----------------------------------------------------------------------
  109. ; Set match_start to the longest match starting at the given string and
  110. ; return its length. Matches shorter or equal to prev_length are discarded,
  111. ; in which case the result is equal to prev_length and match_start is
  112. ; garbage.
  113. ; IN assertions: cur_match is the head of the hash chain for the current
  114. ;   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  115.  
  116. ; int longest_match(cur_match)
  117.  
  118. _longest_match  proc near         ; 'proc far' for large model
  119.         push    bp
  120.         mov     bp,sp
  121.         push    di
  122.     push    si
  123.     push    ds
  124.  
  125.         cur_match    equ word ptr [bp+4] ; [bp+6] for large model
  126.  
  127. ;       window         equ es:window (es:0 for DYN_ALLOC)
  128. ;       prev         equ ds:prev
  129. ;       match        equ es:si
  130. ;       scan         equ es:di
  131. ;       chain_length equ bp
  132. ;       best_len     equ bx
  133. ;       limit        equ dx
  134.  
  135.     mov    si,cur_match            ; use bp before it is destroyed
  136.         mov     bp,_max_chain_length    ; chain_length = max_chain_length
  137.     mov    di,_strstart
  138.     mov    dx,di
  139.     sub    dx,MAX_DIST             ; limit = strstart-MAX_DIST
  140.     jae    limit_ok
  141.     sub    dx,dx            ; limit = NIL
  142. limit_ok:
  143.         add     di,2+window_off         ; di = offset(window + strstart + 2)
  144.         mov     bx,_prev_length         ; best_len = prev_length
  145.     mov     es,window_seg
  146.         mov     ax,es:[bx+di-3]         ; ax = scan[best_len-1..best_len]
  147.         mov     cx,es:[di-2]            ; cx = scan[0..1]
  148.     cmp    bx,_good_match        ; do we have a good match already?
  149.         mov     ds,prev_seg            ; (does not destroy the flags)
  150.         assume  ds: nothing
  151.         jb      do_scan            ; good match?
  152.     shr    bp,1            ; chain_length >>= 2
  153.     shr    bp,1
  154.         jmp     short do_scan
  155.  
  156.         even                            ; align destination of branch
  157. long_loop:
  158. ; at this point, ds:di == scan+2, ds:si == cur_match
  159.         mov     ax,[bx+di-3]            ; ax = scan[best_len-1..best_len]
  160.         mov     cx,[di-2]               ; cx = scan[0..1]
  161.         mov     ds,prev_seg            ; reset ds to address the prev array
  162. short_loop:
  163.         dec     bp                      ; --chain_length
  164.         jz      the_end
  165. ; at this point, di == scan+2, si = cur_match,
  166. ; ax = scan[best_len-1..best_len] and cx = scan[0..1]
  167. if (WSIZE-32768)
  168.         and     si,WSIZE-1              ; not needed if WSIZE=32768
  169. endif
  170.         shl     si,1                    ; cur_match as word index
  171.         mov     si,prev[si]             ; cur_match = prev[cur_match]
  172.         cmp     si,dx            ; cur_match <= limit ?
  173.         jbe     the_end
  174. do_scan:
  175.         cmp     ax,word ptr es:window[bx+si-1] ; check match at best_len-1
  176.         jne     short_loop
  177.         cmp     cx,word ptr es:window[si]      ; check min_match_length match
  178.         jne     short_loop
  179.  
  180.         lea     si,window[si+2]         ; si = match
  181.         mov     ax,di                   ; ax = scan+2
  182.         mov     cx,es
  183.         mov     ds,cx            ; ds = es = window
  184.         mov     cx,(MAX_MATCH-2)/2      ; scan for at most MAX_MATCH bytes
  185.         repe    cmpsw                   ; loop until mismatch
  186.         je      maxmatch                ; match of length MAX_MATCH?
  187. mismatch:
  188.         mov     cl,[di-2]               ; mismatch on first or second byte?
  189.         sub     cl,[si-2]               ; cl = 0 if first bytes equal
  190.         xchg    ax,di                   ; di = scan+2, ax = end of scan
  191.         sub     ax,di                   ; ax = len
  192.     sub    si,ax            ; si = cur_match + 2 + offset(window)
  193.     sub    si,2+window_off         ; si = cur_match
  194.         sub     cl,1                    ; set carry if cl == 0 (can't use DEC)
  195.         adc     ax,0                    ; ax = carry ? len+1 : len
  196.         cmp     ax,bx                   ; len > best_len ?
  197.         jle     long_loop
  198.         mov     ma_start,si             ; match_start = cur_match
  199.         mov     bx,ax                   ; bx = best_len = len
  200.         cmp     ax,MAX_MATCH            ; len >= MAX_MATCH ?
  201.         jl      long_loop
  202. the_end:
  203.     pop    ds
  204.         assume  ds: DGROUP
  205. ifdef SS_NEQ_DS
  206.     mov    ax,ma_start        ; garbage if no match found
  207.     mov    ds:_match_start,ax
  208. endif
  209.         pop     si
  210.         pop     di
  211.         pop     bp
  212.         mov     ax,bx                   ; result = ax = best_len
  213.         ret
  214. maxmatch:                               ; come here if maximum match
  215.         cmpsb                           ; increment si and di
  216.         jmp     mismatch                ; force match_length = MAX_LENGTH
  217.         
  218. _longest_match  endp
  219.  
  220. _TEXT   ends
  221. end
  222.