1 ; match.asm -- optional optimized asm version of longest match in deflate.c
2 ; Copyright (C) 1992-1993 Jean-loup Gailly
3 ; This is free software; you can redistribute it and/or modify it under the
4 ; terms of the GNU General Public License, see the file COPYING.
6 ; Must be assembled with masm -ml. To be used only with C compact model
7 ; or large model. (For large model, assemble with -D__LARGE__).
8 ; This file is only optional. If you don't have masm or tasm, use the
9 ; C version (add -DNO_ASM to CFLAGS in makefile.msc and remove match.obj
10 ; from OBJI). If you have reduced WSIZE in zip.h, then change its value
13 ; Turbo C 2.0 does not support static allocation of more than 64K bytes per
14 ; file, and does not have SS == DS. So TC and BC++ users must use:
15 ; tasm -ml -DDYN_ALLOC -DSS_NEQ_DS match;
17 ; To simplify the code, the option -DDYN_ALLOC is supported for OS/2
18 ; only if the arrays are guaranteed to have zero offset (allocated by
19 ; halloc). We also require SS==DS. This is satisfied for MSC but not Turbo C.
26 prev
equ _prev
; offset part
30 _DATA
segment word public 'DATA'
31 extrn _nice_match
: word
32 extrn _match_start
: word
33 extrn _prev_length
: word
34 extrn _good_match
: word
35 extrn _strstart
: word
36 extrn _max_chain_length
: word
40 prev
equ 0 ; offset forced to zero
42 window_seg
equ _window
[2]
47 window_off
equ offset _window
53 _TEXT
segment word public 'CODE'
54 assume cs: _TEXT
, ds: DGROUP
61 WSIZE
equ 32768 ; keep in sync with zip.h !
62 MIN_LOOKAHEAD
equ (MAX_MATCH
+MIN_MATCH
+1)
63 MAX_DIST
equ (WSIZE
-MIN_LOOKAHEAD
)
65 prev_ptr
dw seg _prev
; pointer to the prev array
67 match_start
dw 0 ; copy of _match_start if SS != DS
68 nice_match
dw 0 ; copy of _nice_match if SS != DS
71 ; initialize or check the variables used in match.asm.
74 _match_init
proc far ; 'proc far' for large model
76 _match_init
proc near ; 'proc near' for compact model
79 ma_start
equ cs:match_start
; does not work on OS/2
80 nice
equ cs:nice_match
82 mov cs:nice_match
,ax ; ugly write to code, crash on OS/2
85 ma_start
equ ss:_match_start
86 nice
equ ss:_nice_match
93 cmp _prev
[0],0 ; verify zero offset
98 mov ax,_prev
[2] ; segment value
99 mov cs:prev_ptr
,ax ; ugly write to code, crash on OS/2
100 prev_seg
equ cs:prev_ptr
102 prev_seg
equ ss:_prev
[2] ; works on OS/2 if SS == DS
105 prev_seg
equ cs:prev_ptr
109 extrn _exit
: far ; 'far' for large model
111 extrn _exit
: near ; 'near' for compact model
117 ; -----------------------------------------------------------------------
118 ; Set match_start to the longest match starting at the given string and
119 ; return its length. Matches shorter or equal to prev_length are discarded,
120 ; in which case the result is equal to prev_length and match_start is
122 ; IN assertions: cur_match is the head of the hash chain for the current
123 ; string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
125 ; int longest_match(cur_match)
128 _longest_match
proc far ; 'proc far' for large model
130 _longest_match
proc near ; 'proc near' for compact model
139 cur_match
equ word ptr [bp+6] ; [bp+6] for large model
141 cur_match
equ word ptr [bp+4] ; [bp+4] for compact model
144 ; window equ es:window (es:0 for DYN_ALLOC)
148 ; chain_length equ bp
152 mov si,cur_match
; use bp before it is destroyed
153 mov bp,_max_chain_length
; chain_length = max_chain_length
156 sub dx,MAX_DIST
; limit = strstart-MAX_DIST
158 sub dx,dx ; limit = NIL
160 add di,2+window_off
; di = offset(window + strstart + 2)
161 mov bx,_prev_length
; best_len = prev_length
163 mov ax,es:[bx+di-3] ; ax = scan[best_len-1..best_len]
164 mov cx,es:[di-2] ; cx = scan[0..1]
165 cmp bx,_good_match
; do we have a good match already?
166 mov ds,prev_seg
; (does not destroy the flags)
168 jb do_scan
; good match?
169 shr bp,1 ; chain_length >>= 2
173 even
; align destination of branch
175 ; at this point, ds:di == scan+2, ds:si == cur_match
176 mov ax,[bx+di-3] ; ax = scan[best_len-1..best_len]
177 mov cx,[di-2] ; cx = scan[0..1]
178 mov ds,prev_seg
; reset ds to address the prev array
180 ; at this point, di == scan+2, si = cur_match,
181 ; ax = scan[best_len-1..best_len] and cx = scan[0..1]
183 and si,WSIZE
-1 ; not needed if WSIZE=32768
185 shl si,1 ; cur_match as word index
186 mov si,prev
[si] ; cur_match = prev[cur_match]
187 cmp si,dx ; cur_match <= limit ?
189 dec bp ; --chain_length
192 cmp ax,word ptr es:window
[bx+si-1] ; check match at best_len-1
194 cmp cx,word ptr es:window
[si] ; check min_match_length match
197 lea si,window
[si+2] ; si = match
198 mov ax,di ; ax = scan+2
200 mov ds,cx ; ds = es = window
201 mov cx,(MAX_MATCH
-2)/2 ; scan for at most MAX_MATCH bytes
202 repe cmpsw ; loop until mismatch
203 je maxmatch
; match of length MAX_MATCH?
205 mov cl,[di-2] ; mismatch on first or second byte?
206 sub cl,[si-2] ; cl = 0 if first bytes equal
207 xchg ax,di ; di = scan+2, ax = end of scan
209 sub si,ax ; si = cur_match + 2 + offset(window)
210 sub si,2+window_off
; si = cur_match
211 sub cl,1 ; set carry if cl == 0 (can't use DEC)
212 adc ax,0 ; ax = carry ? len+1 : len
213 cmp ax,bx ; len > best_len ?
215 mov ma_start
,si ; match_start = cur_match
216 mov bx,ax ; bx = best_len = len
217 cmp ax,nice
; len >= nice_match ?
223 mov ax,ma_start
; garbage if no match found
224 mov ds:_match_start
,ax
229 mov ax,bx ; result = ax = best_len
231 maxmatch: ; come here if maximum match
232 cmpsb ; increment si and di
233 jmp mismatch
; force match_length = MAX_LENGTH