…Show last 240 lines
61 mov w[old_int_11+2],es
62 mov w[old_int_11+0],bx
63
64 ;; test it is ourself or someone else
65 mov di,bx
66 mov si,my_interrupt
67 mov cx,setup_isr
68 sub cx,si
69 repe cmpsb
70 jnz .go_resident
71
72 ;; its us, so detach int 11
73 mov dx,w[es: old_int_11+0]
74 mov ds,w[es: old_int_11+2]
75 mov ax,0x2511
76 int 0x21
77
78 ;; free tsr memory
79 mov ah,0x49
80 int 0x21
81
82 mov ax,cs
83 mov ds,ax
84
85 mov dx,msg_unhook
86 mov ah,9
87 int 0x21
88
89 ;; quit
90 mov ah,0x4C
91 int 0x21
92
93.go_resident:
94 call setup_tsr_memory_block
95 ;; tsr code block is in BP
96
97 mov ds,bp
98
99 ;; setup insdos buffer
100 mov ah,0x34
101 int 0x21
102 ;; dos version dependant, 3.1+ lets hope we dont wrap segments...
103 dec bx
104 mov w[indos_buffer+2],es
105 mov w[indos_buffer+0],bx
106
107 mov ax,0x2511
108 mov dx,my_interrupt
109 int 0x21
110
111 ;; free environment
112 mov ax,cs
113 mov ds,ax
114
115 mov es,w[0x2C]
116 mov ah,0x49
117 int 0x21
118
119 mov dx,msg_hook
120 mov ah,9
121 int 0x21
122
123 mov ax,cs
124 cmp ax,bp
125 jz .tsr_low_memory
126
127 ;; only free ourself if we are not ourselves (tsr in umb)
128 mov ah,0x49
129 int 0x21
130
131 mov ah,0x4C
132 int 0x21
133
134.tsr_low_memory:
135 ;; go tsr
136 mov dx,setup_isr ;; already aligned correctly for tsr
137 mov ah,0x31
138 int 0x21
139
140proc16 setup_tsr_memory_block
141 push ds
142 pop es
143
144 mov bp,ds
145
146 mov ax,0x3000
147 int 0x21
148 cmp al,5
149 jb .nd0
150
151 push ax
152
153 ;; chain umb
154 mov ax,0x5803
155 mov bx,1
156 int 0x21
157
158 pop ax
159
160.nd0:
161 cmp al,3
162 jb .alloc_low
163
164 ;; last fit allocation strategy
165 mov ax,0x5801
166 mov bx,2
167 int 0x21
168
169 call alloc_block
170 jc .nd90
171
172 mov es,ax
173
174 ;; if its not umb, dont use it!
175 cmp ax,0xA000
176 jae .alloc_is_good
177
178 ;; free
179 mov ah,0x49
180 int 0x21
181
182 mov ax,cs
183 mov es,ax
184
185.alloc_low:
186 ;; reset mem strategy to low
187 call reset_alloc_low
188 call alloc_block
189 jc .nd90
190
191 ;; is our block below our cs
192 mov bx,cs
193 cmp ax,bx
194 jb .alloc_is_good
195
196 mov es,ax
197 mov ah,0x49
198 int 0x21
199 mov ax,cs
200 mov es,ax
201 jmp .nd90
202
203.alloc_is_good:
204 mov bp,ax
205
206.nd90:
207 ;; fail!
208 call reset_alloc_low
209
210 ;; free up old environment blocks
211 mov es,w[cs: 0x2c]
212 mov ah,0x49
213 int 0x21
214
215
216 ;; set owner to itself so dos does not free it up
217 mov ax,bp
218 dec ax
219 mov es,ax
220 inc ax
221 mov w[es: 1], ax
222
223 ;; own memory
224 mov ax,cs
225 mov ds,ax
226
227 ;; lets do a MCB fudge for naming
228 mov si,mytsr_name
229 mov di,8
230 mov cx,4
231 rep movsw
232
233 ;; copy down
234 mov ax,cs
235 mov ds,ax
236 mov es,bp
237 cmp ax,bp
238 jz .skip_move
239
240 xor si,si
241 xor di,di
242 mov cx,end_tsr_part
243 shr cx,1
244 rep movsw
245
246.skip_move:
247 mov ax,cs
248 mov ds,ax
249 mov es,ax
250 ret
251
252
253proc16 alloc_block
254 ;; try and allocate upper memory
255 mov ax,0x4800
256 mov bx,end_tsr_part ;- 0x100
257 shr bx,1
258 shr bx,1
259 shr bx,1
260 shr bx,1
261 int 0x21
262 ret
263
264proc16 reset_alloc_low
265 mov ax,0x3000
266 int 0x21
267 cmp al,5
268 jb .nd99
269
270 push ax
271 ;; unchain UMB's
272 mov ax,0x5803
273 xor bx,bx
274 int 0x21
275 pop ax
276
277.nd99:
278 ;; reset allocation strategy
279 cmp al,3
280 jb .nd100
281 mov ax,0x5801
282 xor bx,bx
283 int 0x21
284
285.nd100:
286 ret
287
288[section .data]
289 ;; MCB name, must be 8 characters here
290mytsr_name: db 'myTSR',0,0,0
291
292msg_hook: db 'We have hooked int 0x11. Run again to remove',0x0d,0x0a,'$'
293msg_unhook: db 'We have unhooked int 0x11 and removed TSR',0x0d,0x0a,'$'
294
295[section .bss]
296align 2
297 resw 128
298end_stack:
299