-
Notifications
You must be signed in to change notification settings - Fork 2
/
LZWC2.ASM
260 lines (218 loc) · 7.59 KB
/
LZWC2.ASM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;
; Lempel-Ziv Welch Compression routine
;
; Copyright (C) 1994 by Roman Cramar
;
ideal
jumps
segment DATA 'DATA'
extrn MaxCode : word
extrn RunCode : word
extrn RunningBits : word
extrn CodeBuffer : dword
extrn CurCode : word
extrn OldCode : word
extrn CurBuffShift : word
extrn InputOffs : word
extrn InputSeg : word
extrn OutPutOffs : word
extrn OutPutSeg : word
extrn Temp : word
extrn DataSize : word
extrn OutPutOffs2 : word
extrn CodeTable : dword
extrn PrefixTable : dword
extrn SymbolTable : dword
ends DATA
segment CODE
assume cs:CODE, ds:DATA
public LZWCompr
p386
macro _GetByte
dec [DataSize]
jz LZWComprEnd
les si, [dword ptr InputOffs]
mov al, [es:si]
mov ah, 0
inc [InputOffs]
endm
macro _ComprCode
local Rotate2,NoRotate2,CheckFull,NoIncBits
xor bx,bx
mov cx,[CurBuffShift]
or cx,cx
jz NoRotate2
Rotate2: shl ax,1
rcl bx,1
loop Rotate2
NoRotate2: or [word ptr CodeBuffer+0],ax
or [word ptr CodeBuffer+2],bx
mov ax,[RunningBits]
add [CurBuffShift],ax
CheckFull: cmp [CurBuffShift],8
jc NoIncBits
les di,[dword ptr OutputOffs]
mov ax,[word ptr CodeBuffer]
mov [es:di],al
inc [OutPutOffs]
rept 8
shr [word ptr CodeBuffer+2],1
rcr [word ptr CodeBuffer+0],1
endm
sub [CurBuffShift],8
jmp CheckFull
NoIncBits:
endm
macro _InitCodeTable
les di,[CodeTable]
mov cx,LZMax
xor ax,ax
cld
rep stosw
endm
macro _ResetCodeBuffer
local Again
Again:
les di,[dword ptr OutPutOffs]
mov ax,[word ptr CodeBuffer]
mov [es:di],al
inc [OutPutOffs]
rept 8
shr [word ptr CodeBuffer+2],1
rcr [word ptr CodeBuffer+0],1
endm
sub [CurBuffShift],8
jnc Again
ResetBuffEnd: mov [CurBuffShift],0
endm
;Constants
ClearCode equ 100h
EOICode equ 101h
LZMax equ 4096
;Pointers
cCodeTable equ 20h
cPrefixTable equ 2020h
cSymbolTable equ 4020h
;Compress - compresses data using Lempel-Ziv Welch method.
; BX:AX - Pointer to temporary buffer (21K min.)
; DX:CX - Pointer to compressed data.
; DI:SI - Pointer to buffer to decompress.
; Exit: AX - size of compressed data.
proc LZWCompr near
push bp
mov [InputOffs], cx
mov [InputSeg], dx
mov [OutPutOffs], si
mov [OutPutOffs2],si
mov [OutputSeg], di
mov [word ptr CodeTable+2],bx
mov [word ptr PrefixTable+2],bx
mov [word ptr SymbolTable+2],bx
mov bx, ax
lea ax, [cCodeTable+bx]
mov [word ptr CodeTable], ax
lea ax, [cPrefixTable+bx]
mov [word ptr PrefixTable], ax
lea ax, [cSymbolTable+bx]
mov [word ptr SymbolTable], ax
inc [DataSize]
call LZWCompress
mov ax,[OutPutOffs]
sub ax,[OutPutOffs2]
pop bp
ret
endp LZWCompr
proc LZWCompress near
_InitCodeTable
mov [RunCode],102h
mov [RunningBits],9
mov [MaxCode],200h
xor ax,ax
mov [CurBuffShift],ax
mov [CurCode],ax
mov [word ptr CodeBuffer+0],ax
mov [word ptr CodeBuffer+2],ax
mov ax,ClearCode
_ComprCode
_GetByte
mov [OldCode],ax
NextByte: _GetByte
mov [CurCode],ax
mov cl,5
shl ax,cl
xor ax,[OldCode]
and ax,0FFFh
mov si,ax
mov [Temp],1
SearchLoop: mov bx,si
shl bx,1
les bp, [CodeTable]
add bp,bx
cmp [word ptr es:bp],0
jnz IsInTable
mov ax,[OldCode]
_ComprCode
mov bx,si
shl bx,1
mov ax,[RunCode]
mov [Temp],ax
cmp ax,LZMax
jnc CheckOverflow
les bp, [CodeTable]
add bp,bx
mov [es:bp],ax
mov ax,[OldCode]
les bp,[PrefixTable]
add bp,bx
mov [es:bp],ax
mov al,[byte ptr ds:CurCode]
les bp, [SymbolTable]
mov [byte ptr es:bp+si],al
inc [RunCode]
CheckOverflow: mov ax,[Temp]
cmp ax,[MaxCode]
jnz ChangeOldCode
cmp [byte ptr ds:RunningBits],12
jnc SendClearCode
inc [byte ptr ds:RunningBits]
shl [MaxCode],1
jmp ChangeOldCode
SendClearCode: mov ax,ClearCode
_ComprCode
mov [RunCode],102h
mov [byte ptr ds:RunningBits],9
mov [MaxCode],200h
_InitCodeTable
ChangeOldCode: mov al,[byte ptr ds:CurCode]
mov ah,0
mov [OldCode],ax
jmp NextByte
IsInTable: mov ax,[OldCode]
les bp,[PrefixTable]
add bp,bx
cmp ax,[es:bp]
jnz NotTheSame
les bp, [SymbolTable]
mov al,[byte ptr es:bp+si]
cmp al,[byte ptr ds:CurCode]
jnz NotTheSame
les bp, [CodeTable]
add bp,bx
mov ax,[es:bp]
mov [OldCode],ax
jmp NextByte
NotTheSame: add si,[Temp]
add [Temp],2
cmp si,LZMax
jc NoOverflow
sub si,LZMax
NoOverflow: jmp SearchLoop
LZWComprEnd: mov ax,[OldCode]
_ComprCode
mov ax,EOICode
_ComprCode
_ResetCodeBuffer
ret
endp LZWCompress
ends CODE
end