-
Notifications
You must be signed in to change notification settings - Fork 2
/
perversion.asm
426 lines (384 loc) · 6.81 KB
/
perversion.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#if 0
File name: perversi.asm, switch execution to native CPU and handle
returns into emulated environment.
x86 Emulating Debugger (EDB)
Copyright (C) 1991-1993 Serge Pachkovsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
If you need to contact me, feel free to write to ps@oci.unizh.ch
or to Serge Pachkovsky, Erligatterweg 61, Zuerich CH 8038, Switzerland
#endif
.8086
.MODEL TINY
;
; void
; perverted_call( void far *return_addr ) ;
;
.DATA
extrn _ax:word, _bx:word, _cx:word, _dx:word
extrn _si:word, _di:word, _bp:word, _sp:word
extrn _cs:word, _ds:word, _es:word, _ss:word
extrn _ip:word, _flags:word
.CODE
public _start_program
public _break_point_interrupt
public _huge_write
public _setup_vector_monitor
public _fill_IDT
extrn __IOERROR:near
extrn _interrupt_monitor:near
extrn _IDT_descriptor:qword
_start_program proc near
mov ax, _ax
mov bx, _bx
mov cx, _cx
mov dx, _dx
mov si, _si
mov di, _di
mov bp, _bp
mov es, _es
mov ss, _ss
mov sp, _sp
push _flags
push _cs
push _ip
push _ds
pop ds
iret
_start_program endp
;
; WORD
; huge_write( int handle, void far *mem, WORD cnt ) ;
;
_huge_write proc near
push bp
mov bp, sp
push ds
mov bx, [bp+4]
lds dx, [bp+6]
mov cx, [bp+10]
mov ah, 40h
int 21h
pop ds
jnc write_Ok
push ax
call __IOERROR
write_Ok:
pop bp
ret
_huge_write endp
;
; void
; setup_vector_monitor( WORD vec, char *area ) ;
;
; area will contain:
; call cs:[common_loc] 02eh, 0ffg, 016h, offset common_loc
; dw code dw code
; dd old_vector dd old_vector
; Total 10 bytes
;
common_loc dw [common_code]
common_code proc near
push ax ; 8
push bx ; 6
push cx ; 4
push dx ; 2
push ds ; 0
mov ax, cs
mov ds, ax
mov bx, sp
mov ax, word ptr ss:[bx+10]
sub ax, 5
push ax
call _interrupt_monitor
pop ax
pop ds
pop dx
pop cx
pop bx
pop ax
add sp, 2
iret
common_code endp
_setup_vector_monitor proc near
push bp
mov bp, sp
push di
mov ax, word ptr [bp+4]
mov ah, 35h
int 21h
mov dx, es
mov di, word ptr [bp+6]
push ds
pop es
cld
mov al, 02eh
stosb
mov ax, 016ffh
stosw
lea ax, common_loc
stosw
inc di
mov ax, word ptr [bp+4]
stosw
mov ax, bx
stosw
mov ax, dx
stosw
mov dx, word ptr [bp+6]
mov ax, word ptr [bp+4]
mov ah, 25h
int 21h
pop di
pop bp
ret
_setup_vector_monitor endp
.286p
cs_base dw ?
temp_cs dw ?
temp_addr dd ?
break_point dw ?
interrupt_vector proc near
push cs
db 0EAh
dw interrupt_proc, 0
interrupt_seg EQU word ptr $-2
interrupt_proc label near
pop cs:temp_cs
push ax
push bx
push ds
xor ax, ax
mov ds, ax
mov bx, cs:temp_cs
sub bx, cs:cs_base
shl bx, 2
mov ax, ds:[bx]
mov word ptr cs:temp_addr, ax
mov ax, ds:[bx+2]
mov word ptr cs:temp_addr + 2, ax
pop ds
pop bx
pop ax
jmp cs:[temp_addr]
interrupt_vector endp
save_interrupt_vector proc near
cli
xor ax, ax
mov ds, ax
mov ax, ds:[bx]
mov word ptr cs:temp_addr, ax
mov ax, ds:[bx+2]
mov word ptr cs:temp_addr + 2, ax
ret
save_interrupt_vector endp
AT_extended_service proc near
push ax
push bx
push ds
mov bx, 15h*4
call save_interrupt_vector
pop ds
pop bx
pop ax
pushf
call cs:[temp_addr]
lidt cs:_IDT_descriptor
retf 2
AT_extended_service endp
.8086
_break_point_interrupt proc near
push ax
push ds
push cs
pop ds
pop ax
mov _ds, ax
pop ax
mov _ax, ax
pop ax
dec ax
mov _ip, ax
pop ax
mov _cs, ax
pop ax
mov _flags, ax
mov _sp, sp
mov _bx, bx
mov _cx, cx
mov _dx, dx
mov _bp, bp
mov _si, si
mov _di, di
mov _es, es
mov _ss, ss
jmp cs:[break_point]
_break_point_interrupt endp
_fill_IDT proc near
push bp
mov bp, sp
push di
mov ax, cs
mov es, ax
mov interrupt_seg, ax
sub ax, 256
mov cs:cs_base, ax
mov bx, offset interrupt_vector + 256 * 16
mov cx, 256
mov di, [bp+4]
cld
prepare_loop:
xchg ax, bx
stosw
xchg ax, bx
stosw
inc ax
sub bx, 16
loop prepare_loop
mov di, [bp+4]
mov word ptr [di+15h*4], offset AT_extended_service
mov word ptr [di+15h*4+2], cs
mov word ptr [di+03h*4], offset _break_point_interrupt
mov word ptr [di+03h*4+2], cs
mov ax, [bp+6]
mov cs:break_point, ax
pop di
pop bp
ret
_fill_IDT endp
public _copy_RAM
DEST EQU word ptr [bp+4]
SRC EQU word ptr [bp+6]
SIZEX EQU word ptr [bp+8]
_copy_RAM proc near
push bp
mov bp, sp
push si
push di
push ds
cld
mov es, DEST
mov ds, SRC
mov cx, SIZEX
xor dx, dx
shl cx, 1
rcl dx, 1
shl cx, 1
rcl dx, 1
shl cx, 1
rcl dx, 1
shl cx, 1
rcl dx, 1
jcxz no_small_part
xor si, si
xor di, di
shr cx, 1
rep movsw
mov cl, 4
shr si, cl
mov ax, ds
add ax, si
mov ds, ax
mov ax, es
add ax, si
mov es, ax
no_small_part:
or dx, dx
jz done_moving
xor si, si
xor di, di
mov cx, 8000h
rep movsw
mov ax, ds
add ax, 800h
mov ds, ax
mov ax, es
add ax, 800h
mov es, ax
dec dx
jmp no_small_part
done_moving:
pop ds
pop di
pop si
pop bp
ret
_copy_RAM endp
public _swap_RAM
LOC_A EQU dword ptr [bp+4]
LOC_B EQU dword ptr [bp+8]
SIZEY EQU dword ptr [bp+12]
large_move proc near
push cx
next_byte:
mov ax, es:[di]
movsw
mov ds:[si-2], ax
loop next_byte
pop cx
shl cx, 1
sub si, cx
sub di, cx
shr cx, 1
shr cx, 1
shr cx, 1
shr cx, 1
mov ax, ds
add ax, cx
mov ds, ax
mov ax, es
add ax, cx
mov es, ax
ret
large_move endp
_swap_RAM proc near
push bp
mov bp, sp
push si
push di
push ds
lds si, LOC_A
les di, LOC_B
cld
next_huge_chunk:
cmp word ptr SIZEY + 2, 0
je try_small_chunk
dec word ptr SIZEY + 2
mov cx, 4000h
call large_move
mov cx, 4000h
call large_move
jmp next_huge_chunk
try_small_chunk:
mov cx, word ptr SIZEY
and cx, 0FFF0h
shr cx, 1
push cx
call large_move
mov cx, word ptr SIZEY
pop ax
shl ax, 1
sub cx, ax
jcxz done_swapping
next_byte_rest:
mov al, es:[di]
movsb
mov ds:[si-1], al
loop next_byte_rest
done_swapping:
pop ds
pop di
pop si
pop bp
ret
_swap_RAM endp
END