Subversion Repositories Kolibri OS

Rev

Rev 410 | Rev 542 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 heavyiron 1
format binary
485 heavyiron 2
include '..\..\macros.inc'
205 heavyiron 3
use32
4
	db	'MENUET01'
5
	dd	1
6
	dd	start
7
	dd	i_end
8
	dd	used_mem
9
	dd	used_mem
10
	dd	i_param
11
	dd	0
12
 
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GUI ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
17
data_width equ 80
18
data_x_pos equ 12
19
data_x_size equ data_width*6
20
 
21
title_x_pos equ 30
22
title_y_pos equ 32
23
title_y_size equ 10
24
 
25
registers_x_pos equ data_x_pos
26
registers_y_pos equ (title_y_pos + title_y_size)
27
registers_y_size equ 30
28
 
29
dump_y_pos equ (registers_y_pos + registers_y_size + 5)
30
dump_height equ 4
31
dump_y_size equ (dump_height*10)
32
 
33
disasm_y_pos equ (dump_y_pos + dump_y_size + 4)
34
disasm_height equ 16
35
disasm_y_size equ (disasm_height*10)
36
 
37
messages_width equ data_width
38
messages_height equ 12
39
messages_x_pos equ data_x_pos
40
messages_y_pos equ (disasm_y_pos + disasm_y_size + 4)
41
messages_x_size equ messages_width*6
42
messages_y_size equ messages_height*10
43
 
44
cmdline_width equ data_width
45
cmdline_x_pos equ data_x_pos
46
cmdline_y_pos equ (messages_y_pos + messages_y_size + 10)
47
cmdline_x_size equ messages_x_size
48
cmdline_y_size equ 10
49
 
50
wnd_x_size equ (data_x_pos + messages_x_size + data_x_pos)
51
wnd_y_size equ (cmdline_y_pos + cmdline_y_size + data_x_pos)
52
 
53
start:
54
	mov	edi, messages
55
	mov	ecx, messages_width*messages_height
56
	mov	al, ' '
57
	rep	stosb
58
	xor	eax, eax
59
	mov	[messages_pos], eax
60
	mov	[cmdline_len], eax
61
	mov	[cmdline_pos], eax
62
	mov	edi, needzerostart
63
	mov	ecx, (needzeroend-needzerostart+3)/4
64
	rep	stosd
65
	mov	esi, begin_str
66
	call	put_message_nodraw
67
; set event mask - default events and debugging events
68
	push	40
69
	pop	eax
70
	mov	ebx, 0x107
485 heavyiron 71
	mcall
205 heavyiron 72
; set debug messages buffer
73
	mov	ecx, dbgbufsize
74
	mov	dword [ecx], 256
75
	xor	ebx, ebx
76
	mov	[ecx+4], ebx
77
	mov	al, 69
485 heavyiron 78
	mcall
205 heavyiron 79
	mov	esi, i_param
80
	call	skip_spaces
81
	test	al, al
82
	jz	dodraw
83
	push	esi
84
	call	draw_window
85
	pop	esi
86
	call	OnLoadInit
87
	jmp	waitevent
88
dodraw:
89
	call	draw_window
90
waitevent:
91
	push	10
92
	pop	eax
485 heavyiron 93
	mcall
205 heavyiron 94
	cmp	al, 9
95
	jz	debugmsg
96
	dec	eax
97
	jz	dodraw
98
	dec	eax
99
	jz	keypressed
100
	dec	eax
101
	jnz	waitevent
102
; button pressed - we have only one button (close)
103
	push	-1
104
	pop	eax
485 heavyiron 105
	mcall
205 heavyiron 106
keypressed:
107
	mov	al, 2
485 heavyiron 108
	mcall
205 heavyiron 109
	shr	eax, 8
110
	cmp	al, 8
111
	jz	.backspace
112
	cmp	al, 0xB0
113
	jz	.left
114
	cmp	al, 0xB3
115
	jz	.right
116
	cmp	al, 0x0D
117
	jz	.enter
118
	cmp	al, 0xB6
119
	jz	.del
120
	cmp	al, 0xB4
121
	jz	.home
122
	cmp	al, 0xB5
123
	jz	.end
124
	cmp	al, 0xB1
125
	jz	.down
126
	cmp	al, 0xB2
127
	jz	.up
128
	cmp	al, 0xD8
129
	jz	CtrlF7
130
	cmp	al, 0xD9
131
	jz	CtrlF8
132
	cmp	[cmdline_len], cmdline_width
133
	jae	waitevent
134
	push	eax
135
	call	clear_cmdline_end
136
	pop	eax
137
	mov	edi, cmdline
138
	mov	ecx, [cmdline_len]
139
	add	edi, ecx
140
	lea	esi, [edi-1]
141
	sub	ecx, [cmdline_pos]
142
	std
143
	rep	movsb
144
	cld
145
	stosb
146
	inc	[cmdline_len]
147
	call	draw_cmdline_end
148
	inc	[cmdline_pos]
149
	call	draw_cursor
150
	jmp	waitevent
151
.backspace:
152
	cmp	[cmdline_pos], 0
153
	jz	waitevent
154
	dec	[cmdline_pos]
155
.delchar:
156
	call	clear_cmdline_end
157
	mov	edi, [cmdline_pos]
158
	dec	[cmdline_len]
159
	mov	ecx, [cmdline_len]
160
	sub	ecx, edi
161
	add	edi, cmdline
162
	lea	esi, [edi+1]
163
	rep	movsb
164
	call	draw_cmdline_end
165
	call	draw_cursor
166
	jmp	waitevent
167
.del:
168
	mov	eax, [cmdline_pos]
169
	cmp	eax, [cmdline_len]
170
	jae	waitevent
171
	jmp	.delchar
172
.left:
173
	cmp	[cmdline_pos], 0
174
	jz	waitevent
175
	call	hide_cursor
176
	dec	[cmdline_pos]
177
	call	draw_cursor
178
	jmp	waitevent
179
.right:
180
	mov	eax, [cmdline_pos]
181
	cmp	eax, [cmdline_len]
182
	jae	waitevent
183
	call	hide_cursor
184
	inc	[cmdline_pos]
185
	call	draw_cursor
186
	jmp	waitevent
187
.home:
188
	call	hide_cursor
189
	and	[cmdline_pos], 0
190
	call	draw_cursor
191
	jmp	waitevent
192
.end:
193
	call	hide_cursor
194
	mov	eax, [cmdline_len]
195
	mov	[cmdline_pos], eax
196
	call	draw_cursor
197
.up:
198
.down:
199
	jmp	waitevent
200
.enter:
201
	mov	ecx, [cmdline_len]
202
	test	ecx, ecx
203
	jz	waitevent
204
	mov	esi, cmdline
205
	mov	byte [esi+ecx], 0
206
	and	[cmdline_pos], 0
207
	push	esi
208
	call	clear_cmdline_end
209
	call	draw_cursor
210
	pop	esi
211
	and	[cmdline_len], 0
212
; skip leading spaces
213
	call	skip_spaces
214
	cmp	al, 0
215
	jz	waitevent
216
; now esi points to command
217
	push	esi
218
	mov	esi, prompt
219
	call	put_message_nodraw
220
	pop	esi
221
	push	esi
222
	call	put_message_nodraw
223
z1:	mov	esi, newline
224
	call	put_message
225
	pop	esi
226
	push	esi
227
	call	get_arg
228
	mov	[curarg], esi
229
	pop	edi
230
	mov	esi, commands
231
	call	find_cmd
232
	mov	eax, aUnknownCommand
233
	jc	.x11
234
; check command requirements
235
; flags field:
236
; &1: command may be called without parameters
237
; &2: command may be called with parameters
238
; &4: command may be called without loaded program
239
; &8: command may be called with loaded program
240
	mov	eax, [esi+8]
241
	mov	ecx, [curarg]
242
	cmp	byte [ecx], 0
243
	jz	.noargs
244
	test	byte [esi+16], 2
245
	jz	.x11
246
	jmp	@f
247
.noargs:
248
	test	byte [esi+16], 1
249
	jz	.x11
250
@@:
251
	cmp	[debuggee_pid], 0
252
	jz	.nodebuggee
253
	mov	eax, aAlreadyLoaded
254
	test	byte [esi+16], 8
255
	jz	.x11
256
	jmp	.x9
257
.nodebuggee:
258
	mov	eax, need_debuggee
259
	test	byte [esi+16], 4
260
	jnz	.x9
261
.x11:
262
	xchg	esi, eax
263
	call	put_message
264
.x10:
265
	jmp	waitevent
266
.x9:
267
	call	dword [esi+4]
268
	jmp	.x10
269
 
270
find_cmd:
271
; all commands are case-insensitive
272
	push	edi
273
.x4:
274
	mov	al, [edi]
275
	cmp	al, 0
276
	jz	.x5
277
	cmp	al, 'A'
278
	jb	@f
279
	cmp	al, 'Z'
280
	ja	@f
281
	or	al, 20h
282
@@:
283
	stosb
284
	jmp	.x4
285
.x5:
286
; find command
287
	pop	edi
288
.x6:
289
	cmp	dword [esi], 0
290
	jz	.x7
291
	push	esi
292
	mov	esi, [esi]
293
	lodsb
294
	movzx	ecx, al
295
	push	edi
296
	repz	cmpsb
297
	pop	edi
298
	pop	esi
299
	jz	.x8
300
	add	esi, 17
301
	jmp	.x6
302
.x7:
303
	stc
304
.x8:
305
	ret
306
 
307
get_arg:
308
	lodsb
309
	cmp	al, ' '
310
	ja	get_arg
311
	mov	byte [esi-1], 0
312
	cmp	al, 0
313
	jnz	skip_spaces
314
	dec	esi
315
skip_spaces:
316
	lodsb
317
	cmp	al, 0
318
	jz	@f
319
	cmp	al, ' '
320
	jbe	skip_spaces
321
@@:	dec	esi
322
	ret
323
 
324
clear_cmdline_end:
325
	mov	ebx, [cmdline_pos]
326
	mov	ecx, [cmdline_len]
327
	sub	ecx, ebx
328
	push	13
329
	pop	eax
330
	imul	ebx, 6
331
	imul	ecx, 6
332
	inc	ecx
333
	add	ebx, cmdline_x_pos
334
	shl	ebx, 16
335
	or	ebx, ecx
336
	mov	ecx, cmdline_y_pos*10000h + cmdline_y_size
337
	mov	edx, 0xFFFFFF
485 heavyiron 338
	mcall
205 heavyiron 339
	ret
340
 
341
draw_cmdline:
342
	xor	ebx, ebx
343
	jmp	@f
344
draw_cmdline_end:
345
	mov	ebx, [cmdline_pos]
346
@@:
347
	mov	esi, [cmdline_len]
348
	sub	esi, ebx
349
	push	4
350
	pop	eax
351
	xor	ecx, ecx
352
	lea	edx, [cmdline+ebx]
353
	imul	ebx, 6
354
	add	ebx, cmdline_x_pos
355
	shl	ebx, 16
356
	or	ebx, cmdline_y_pos+1
485 heavyiron 357
	mcall
205 heavyiron 358
	ret
359
 
360
put_message_nodraw:
361
; in: esi->ASCIZ message
362
	mov	edx, [messages_pos]
363
.m:
364
	lea	edi, [messages+edx]
365
.l:
366
	lodsb
367
	cmp	al, 0
368
	jz	.done
369
	call	test_scroll
370
	cmp	al, 10
371
	jz	.newline
372
	cmp	al, '%'
373
	jnz	@f
374
	cmp	dword [esp], z1
375
	jnz	.format
376
@@:
377
	stosb
378
	inc	edx
379
	jmp	.l
380
.newline:
381
	push	edx
382
	mov	ecx, messages_width
383
	xor	eax, eax
384
	xchg	eax, edx
385
	div	ecx
386
	xchg	eax, edx
387
	pop	edx
388
	test	eax, eax
389
	jz	.m
390
	sub	edx, eax
391
	add	edx, ecx
392
	jmp	.m
393
.done:
394
	mov	[messages_pos], edx
395
	ret
396
.format:
397
; at moment all format specs must be %X
398
	lodsb	; get 
399
	sub	al, '0'
400
	movzx	ecx, al
401
	lodsb
402
	pop	eax
403
	pop	ebp
404
	push	eax
405
; write number in ebp with ecx digits
406
	dec	ecx
407
	shl	ecx, 2
408
.writenibble:
409
	push	ecx
410
	call	test_scroll
411
	pop	ecx
412
	mov	eax, ebp
413
	shr	eax, cl
414
	and	al, 0xF
415
	cmp	al, 10
416
	sbb	al, 69h
417
	das
418
	stosb
419
	inc	edx
420
	sub	ecx, 4
421
	jns	.writenibble
422
	jmp	.l
423
 
424
test_scroll:
425
	cmp	edx, messages_width*messages_height
426
	jnz	.ret
427
	push	esi
428
	mov	edi, messages
429
	lea	esi, [edi+messages_width]
430
	mov	ecx, (messages_height-1)*messages_width/4
431
	rep	movsd
432
	push	eax
433
	mov	al, ' '
434
	push	edi
435
	push	messages_width
436
	pop	ecx
437
	sub	edx, ecx
438
	rep	stosb
439
	pop	edi
440
	pop	eax
441
	pop	esi
442
.ret:	ret
443
 
444
put_message:
445
	call	put_message_nodraw
446
 
447
draw_messages:
448
	push	13
449
	pop	eax
450
	mov	edx, 0xFFFFFF
451
	mov	ebx, messages_x_pos*10000h+messages_x_size
452
	mov	ecx, messages_y_pos*10000h+messages_y_size
485 heavyiron 453
	mcall
205 heavyiron 454
	mov	edx, messages
455
	push	messages_width
456
	pop	esi
457
	xor	ecx, ecx
458
	mov	al, 4
459
	mov	ebx, messages_x_pos*10000h+messages_y_pos
460
@@:
485 heavyiron 461
	mcall
205 heavyiron 462
	add	edx, esi
463
	add	ebx, 10
464
	cmp	edx, messages+messages_width*messages_height
465
	jb	@b
466
	ret
467
 
468
draw_cursor:
469
	push	38
470
	pop	eax
471
	mov	ecx, cmdline_y_pos*10001h+cmdline_y_size-1
472
	mov	ebx, [cmdline_pos]
473
	imul	ebx, 6
474
	add	ebx, cmdline_x_pos
475
	mov	edx, ebx
476
	shl	ebx, 16
477
	or	ebx, edx
478
	xor	edx, edx
485 heavyiron 479
	mcall
205 heavyiron 480
	ret
481
hide_cursor:
482
	mov	ebx, [cmdline_pos]
483
	push	13
484
	pop	eax
485
	imul	ebx, 6
486
	add	ebx, cmdline_x_pos
487
	shl	ebx, 16
488
	inc	ebx
489
	mov	ecx, cmdline_y_pos*10000h + cmdline_y_size
490
	mov	edx, 0xFFFFFF
485 heavyiron 491
	mcall
205 heavyiron 492
	mov	ebx, [cmdline_pos]
493
	cmp	ebx, [cmdline_len]
494
	jae	.ret
495
	mov	al, 4
496
	xor	ecx, ecx
497
	lea	edx, [cmdline+ebx]
498
	imul	ebx, 6
499
	add	ebx, cmdline_x_pos
500
	shl	ebx, 16
501
	or	ebx, cmdline_y_pos+1
502
	push	1
503
	pop	esi
485 heavyiron 504
	mcall
205 heavyiron 505
.ret:
506
	ret
507
 
508
redraw_title:
509
	push	13
510
	pop	eax
511
	mov	edx, 0xFFFFFF
512
	mov	ebx, title_x_pos*10000h + data_x_pos+data_x_size-title_x_pos
513
	mov	ecx, title_y_pos*10000h + title_y_size
485 heavyiron 514
	mcall
205 heavyiron 515
draw_title:
516
	mov	al, 38
517
	mov	ebx, (data_x_pos-2)*10000h + title_x_pos-5
518
	mov	ecx, (title_y_pos+5)*10001h
519
	xor	edx, edx
485 heavyiron 520
	mcall
205 heavyiron 521
	push	NoPrgLoaded_len
522
	pop	esi
523
	cmp	[debuggee_pid], 0
524
	jz	@f
525
	mov	esi, [prgname_len]
526
@@:	imul	ebx, esi, 6
527
	add	ebx, title_x_pos+4
528
	shl	ebx, 16
529
	mov	bx, data_x_pos+data_x_size-10-5-6*7
530
	cmp	[bSuspended], 0
531
	jz	@f
532
	add	ebx, 6
533
@@:
485 heavyiron 534
	mcall
205 heavyiron 535
	mov	ebx, (data_x_pos+data_x_size-10+4)*0x10000 + data_x_pos+data_x_size+2
485 heavyiron 536
	mcall
205 heavyiron 537
	mov	al, 4
538
	mov	ebx, title_x_pos*10000h+title_y_pos
539
	xor	ecx, ecx
540
	mov	edx, NoPrgLoaded_str
541
	cmp	[debuggee_pid], 0
542
	jz	@f
543
	mov	edx, [prgname_ptr]
544
@@:
485 heavyiron 545
	mcall
205 heavyiron 546
	cmp	[debuggee_pid], 0
547
	jz	.nodebuggee
548
	mov	ebx, (data_x_pos+data_x_size-10-6*7)*10000h + title_y_pos
549
	mov	edx, aRunning
550
	push	7
551
	pop	esi
552
	cmp	[bSuspended], 0
553
	jz	@f
554
	add	ebx, 6*10000h
555
	mov	edx, aPaused
556
	dec	esi
557
@@:
485 heavyiron 558
	mcall
205 heavyiron 559
	ret
560
.nodebuggee:
561
	mov	al, 38
562
	mov	ebx, (data_x_pos+data_x_size-10-6*7-5)*0x10000 + data_x_pos+data_x_size+2
563
	mov	ecx, (title_y_pos+5)*10001h
564
	xor	edx, edx
565
	jmp	@b
566
 
567
draw_register:
568
; in: esi->value, edx->string, ecx=string len, ebx=coord
569
	push	edx
570
	push	ecx
571
	push	esi
572
	mov	eax, esi
573
	mov	esi, ecx
574
; color
575
	mov	ecx, 808080h
576
	cmp	[debuggee_pid], 0
577
	jz	.cd
578
	cmp	[bSuspended], 0
579
	jz	.cd
580
	xor	ecx, ecx
581
	mov	edi, [eax]
582
	cmp	dword [eax+oldcontext-context], edi
583
	jz	.cd
584
	mov	ecx, 0x00AA00
585
.cd:
586
	push	4
587
	pop	eax
485 heavyiron 588
	mcall
205 heavyiron 589
	imul	esi, 60000h
590
	lea	edx, [ebx+esi]
591
	mov	al, 47
592
	mov	ebx, 80101h
593
	mov	esi, ecx
594
	pop	ecx
485 heavyiron 595
	mcall
205 heavyiron 596
	lea	ebx, [edx+60000h*18]
597
	mov	esi, ecx
598
	pop	ecx
599
	pop	edx
600
	add	edx, ecx
601
	ret
602
draw_flag:
603
	movzx	edi, byte [edx+7]
604
	bt	[_eflags], edi
605
	jc	.on
606
	or	byte [edx], 20h
607
	jmp	.onoff
608
.on:
609
	and	byte [edx], not 20h
610
.onoff:
611
	mov	ecx, 808080h
612
	cmp	[debuggee_pid], 0
613
	jz	.doit
614
	cmp	[bSuspended], 0
615
	jz	.doit
616
	xor	ecx, ecx
617
	bt	[_eflags], edi
618
	lahf
619
	bt	dword [_eflags + oldcontext - context], edi
620
	rcl	ah, 1
621
	test	ah, 3
622
	jp	.doit
623
	mov	ecx, 0x00AA00
624
.doit:
625
	mov	ah, 0
485 heavyiron 626
	mcall
205 heavyiron 627
	ret
628
 
629
redraw_registers:
630
	push	13
631
	pop	eax
632
	mov	edx, 0xFFFFFF
633
	mov	ebx, data_x_pos*10000h + data_x_size
634
	mov	ecx, registers_y_pos*10000h + registers_y_size
485 heavyiron 635
	mcall
205 heavyiron 636
draw_registers:
637
	mov	esi, _eax
638
	push	4
639
	pop	ecx
640
	mov	edx, regs_strs
641
	mov	ebx, registers_x_pos*10000h+registers_y_pos
642
	call	draw_register
643
	add	esi, _ebx-_eax
644
	call	draw_register
645
	add	esi, _ecx-_ebx
646
	call	draw_register
647
	add	esi, _edx-_ecx
648
	call	draw_register
649
	mov	ebx, registers_x_pos*10000h+registers_y_pos+10
650
	add	esi, _esi-_edx
651
	call	draw_register
652
	add	esi, _edi-_esi
653
	call	draw_register
654
	add	esi, _ebp-_edi
655
	call	draw_register
656
	add	esi, _esp-_ebp
657
	call	draw_register
658
	mov	ebx, registers_x_pos*10000h+registers_y_pos+20
659
	add	esi, _eip-_esp
660
	call	draw_register
661
	mov	cl, 7
662
	add	esi, _eflags-_eip
663
	call	draw_register
664
	mov	al, 4
665
	mov	ecx, 808080h
666
	cmp	[debuggee_pid], 0
667
	jz	@f
668
	cmp	[bSuspended], 0
669
	jz	@f
670
	xor	ecx, ecx
671
@@:
672
	mov	edx, aColon
673
	xor	esi, esi
674
	inc	esi
675
	mov	ebx, (registers_x_pos+37*6)*10000h + registers_y_pos+20
485 heavyiron 676
	mcall
205 heavyiron 677
	mov	edx, flags
678
@@:
679
	add	ebx, 2*6*10000h
680
	call	draw_flag
681
	inc	edx
682
	cmp	dl, flags_bits and 0xFF
683
	jnz	@b
684
	ret
685
 
686
redraw_dump:
687
	push	13
688
	pop	eax
689
	mov	edx, 0xFFFFFF
690
	mov	ebx, data_x_pos*10000h + data_x_size
691
	mov	ecx, dump_y_pos*10000h + dump_y_size
485 heavyiron 692
	mcall
205 heavyiron 693
draw_dump:
694
; addresses
695
	mov	al, 47
696
	mov	ebx, 80100h
697
	mov	edx, data_x_pos*10000h + dump_y_pos
698
	mov	ecx, [dumppos]
699
	mov	esi, 808080h
700
	cmp	[debuggee_pid], 0
701
	jz	@f
702
	cmp	[bSuspended], 0
703
	jz	@f
704
	xor	esi, esi
705
@@:
485 heavyiron 706
	mcall
205 heavyiron 707
	add	ecx, 10h
708
	add	edx, 10
709
	cmp	dl, dump_y_pos + dump_y_size
710
	jb	@b
711
; hex dump of data
712
	mov	ebx, 20101h
713
	mov	ecx, dumpdata
714
	push	ecx
715
	xor	edi, edi
716
	mov	edx, (data_x_pos+12*6)*10000h + dump_y_pos
717
	cmp	[dumpread], edi
718
	jz	.hexdumpdone1
719
.hexdumploop1:
485 heavyiron 720
	mcall
205 heavyiron 721
	add	edx, 3*6*10000h
722
	inc	ecx
723
	inc	edi
724
	test	edi, 15
725
	jz	.16
726
	test	edi, 7
727
	jnz	@f
728
	add	edx, 2*6*10000h - 10 + 6*(3*10h+2)*10000h
729
.16:
730
	add	edx, 10 - 6*(3*10h+2)*10000h
731
@@:
732
	cmp	edi, [dumpread]
733
	jb	.hexdumploop1
734
.hexdumpdone1:
735
	mov	al, 4
736
	mov	ecx, esi
737
	mov	ebx, edx
738
	push	2
739
	pop	esi
740
	mov	edx, aQuests
741
.hexdumploop2:
742
	cmp	edi, dump_height*10h
743
	jae	.hexdumpdone2
485 heavyiron 744
	mcall
205 heavyiron 745
	add	ebx, 3*6*10000h
746
	inc	edi
747
	test	edi, 15
748
	jz	.16x
749
	test	edi, 7
750
	jnz	.hexdumploop2
751
	add	ebx, 2*6*10000h - 10 + 6*(3*10h+2)*10000h
752
.16x:
753
	add	ebx, 10 - 6*(3*10h+2)*10000h
754
	jmp	.hexdumploop2
755
.hexdumpdone2:
756
	dec	esi
757
; colon, minus signs
758
	mov	ebx, (data_x_pos+8*6)*10000h + dump_y_pos
759
	mov	edx, aColon
760
@@:
485 heavyiron 761
	mcall
205 heavyiron 762
	add	ebx, 10
763
	cmp	bl, dump_y_pos+dump_height*10
764
	jb	@b
765
	mov	ebx, (data_x_pos+(12+3*8)*6)*10000h + dump_y_pos
766
	mov	edx, aMinus
767
@@:
485 heavyiron 768
	mcall
205 heavyiron 769
	add	ebx, 10
770
	cmp	bl, dump_y_pos+dump_height*10
771
	jb	@b
772
; ASCII data
773
	mov	ebx, (data_x_pos+(12+3*10h+2+2)*6)*10000h + dump_y_pos
774
	mov	edi, dump_height*10h
775
	pop	edx
776
.asciiloop:
777
	push	edx
778
	cmp	byte [edx], 20h
779
	jae	@f
780
	mov	edx, aPoint
781
@@:
485 heavyiron 782
	mcall
205 heavyiron 783
	pop	edx
784
	inc	edx
785
	add	ebx, 6*10000h
786
	dec	edi
787
	jz	.asciidone
788
	test	edi, 15
789
	jnz	.asciiloop
790
	add	ebx, 10 - 6*10h*10000h
791
	jmp	.asciiloop
792
.asciidone:
793
	ret
794
 
795
redraw_disasm:
796
	push	13
797
	pop	eax
798
	mov	edx, 0xFFFFFF
799
	mov	ebx, data_x_pos*10000h + data_x_size
800
	mov	ecx, (disasm_y_pos-1)*10000h + (disasm_y_size+1)
485 heavyiron 801
	mcall
205 heavyiron 802
draw_disasm:
803
	mov	eax, [disasm_start_pos]
804
	mov	[disasm_cur_pos], eax
805
	and	[disasm_cur_str], 0
806
.loop:
807
	push	[disasm_cur_pos]
808
	call	disasm_instr
809
	pop	ebp
810
	jc	.loopend
811
	xor	esi, esi	; default color: black
812
	mov	ebx, data_x_pos*10000h + data_x_size
813
	mov	ecx, [disasm_cur_str]
814
	imul	ecx, 10*10000h
815
	add	ecx, (disasm_y_pos-1)*10000h + 10
816
	mov	eax, ebp
817
	pushad
818
	call	find_enabled_breakpoint
819
	popad
820
	jnz	.nored
821
	push	13
822
	pop	eax
823
	mov	edx, 0xFF0000
485 heavyiron 824
	mcall
205 heavyiron 825
.nored:
826
	mov	eax, [_eip]
827
	cmp	eax, ebp
828
	jnz	.noblue
829
	push	13
830
	pop	eax
831
	mov	edx, 0x0000FF
485 heavyiron 832
	mcall
205 heavyiron 833
	mov	esi, 0xFFFFFF	; on blue bgr, use white color
834
.noblue:
835
	push	47
836
	pop	eax
837
	mov	ebx, 80100h
838
	mov	edx, [disasm_cur_str]
839
	imul	edx, 10
840
	add	edx, data_x_pos*10000h + disasm_y_pos
841
	mov	ecx, ebp
485 heavyiron 842
	mcall
205 heavyiron 843
	mov	al, 4
844
	lea	ebx, [edx+8*6*10000h]
845
	mov	ecx, esi
846
	push	1
847
	pop	esi
848
	mov	edx, aColon
485 heavyiron 849
	mcall
205 heavyiron 850
	push	9
851
	pop	edi
852
	lea	edx, [ebx+2*6*10000h]
853
	mov	esi, ecx
854
	mov	al, 47
855
	mov	ebx, 20101h
856
	mov	ecx, ebp
857
	sub	ecx, [disasm_start_pos]
858
	add	ecx, disasm_buffer
859
.drawhex:
485 heavyiron 860
	mcall
205 heavyiron 861
	add	edx, 6*3*10000h
862
	inc	ecx
863
	inc	ebp
864
	cmp	ebp, [disasm_cur_pos]
865
	jae	.hexdone
866
	dec	edi
867
	jnz	.drawhex
868
	push	esi
869
	mov	esi, [disasm_cur_pos]
870
	dec	esi
871
	cmp	esi, ebp
872
	pop	esi
873
	jbe	.drawhex
874
	mov	al, 4
875
	lea	ebx, [edx-6*10000h]
876
	mov	ecx, esi
877
	push	3
878
	pop	esi
879
	mov	edx, aDots
485 heavyiron 880
	mcall
205 heavyiron 881
	mov	esi, ecx
882
.hexdone:
883
	xor	eax, eax
884
	mov	edi, disasm_string
885
	mov	edx, edi
886
	or	ecx, -1
887
	repnz	scasb
888
	not	ecx
889
	dec	ecx
890
	xchg	ecx, esi
891
	mov	ebx, [disasm_cur_str]
892
	imul	ebx, 10
893
	add	ebx, (data_x_pos+6*40)*10000h+disasm_y_pos
894
	mov	al, 4
485 heavyiron 895
	mcall
205 heavyiron 896
	inc	[disasm_cur_str]
897
	cmp	[disasm_cur_str], disasm_height
898
	jb	.loop
899
.loopend:
900
	ret
901
 
902
update_disasm_eip:
903
; test if instruction at eip is showed
904
	mov	ecx, disasm_height
905
	mov	eax, [disasm_start_pos]
906
	mov	[disasm_cur_pos], eax
907
@@:
908
	mov	eax, [_eip]
909
	cmp	[disasm_cur_pos], eax
910
	jz	redraw_disasm
911
	push	ecx
912
	call	disasm_instr
913
	pop	ecx
914
	jc	@f
915
	loop	@b
916
@@:
917
update_disasm_eip_force:
918
	mov	eax, [_eip]
919
	mov	[disasm_start_pos], eax
920
update_disasm:
921
	cmp	[debuggee_pid], 0
922
	jz	.no
923
	push	69
924
	pop	eax
925
	push	6
926
	pop	ebx
927
	mov	ecx, [debuggee_pid]
928
	mov	edi, disasm_buffer
929
	mov	edx, 256
930
	mov	esi, [disasm_start_pos]
485 heavyiron 931
	mcall
205 heavyiron 932
	cmp	eax, -1
933
	jnz	@f
934
	mov	esi, read_mem_err
935
	call	put_message
936
.no:
937
	xor	eax, eax
938
@@:
939
	mov	[disasm_buf_size], eax
940
	call	restore_from_breaks
941
	jmp	redraw_disasm
942
 
943
draw_window:
944
; start redraw
945
	push	12
946
	pop	eax
947
	push	1
948
	pop	ebx
485 heavyiron 949
	mcall
205 heavyiron 950
; define window
951
	xor	eax, eax
952
	mov	ebx, wnd_x_size
953
	mov	ecx, wnd_y_size
954
	mov	edx, 3FFFFFFh
485 heavyiron 955
	mcall
205 heavyiron 956
; caption
957
	mov	al, 4
958
	mov	ecx, 0xFFFFFF
959
	mov	ebx, 80008h
960
	mov	edx, caption_str
961
	push	caption_len
962
	pop	esi
485 heavyiron 963
	mcall
205 heavyiron 964
; messages frame
965
	mov	al, 38
966
	mov	ebx, (messages_x_pos-2)*10000h + (messages_x_pos+messages_x_size+2)
967
	push	ebx
968
	mov	ecx, (messages_y_pos-2)*10001h
969
	xor	edx, edx
485 heavyiron 970
	mcall
205 heavyiron 971
	mov	ecx, (messages_y_pos+messages_y_size+2)*10001h
485 heavyiron 972
	mcall
205 heavyiron 973
	mov	ebx, (messages_x_pos-2)*10001h
974
	push	ebx
975
	mov	ecx, (messages_y_pos-2)*10000h + (messages_y_pos+messages_y_size+2)
485 heavyiron 976
	mcall
205 heavyiron 977
	mov	ebx, (messages_x_pos+messages_x_size+2)*10001h
978
	push	ebx
485 heavyiron 979
	mcall
205 heavyiron 980
; command line frame
981
	mov	ecx, (cmdline_y_pos-2)*10000h + (cmdline_y_pos+cmdline_y_size+2)
982
	pop	ebx
485 heavyiron 983
	mcall
205 heavyiron 984
	pop	ebx
485 heavyiron 985
	mcall
205 heavyiron 986
	pop	ebx
987
	mov	ecx, (cmdline_y_pos+cmdline_y_size+2)*10001h
485 heavyiron 988
	mcall
205 heavyiron 989
	mov	ecx, (cmdline_y_pos-2)*10001h
485 heavyiron 990
	mcall
205 heavyiron 991
; messages
992
	call	draw_messages
993
; command line & cursor
994
	call	draw_cmdline
995
	call	draw_cursor
996
; title & registers & dump & disasm
997
	mov	al, 38
998
	mov	ebx, (data_x_pos-2)*10001h
999
	mov	ecx, (title_y_pos+5)*10000h + (messages_y_pos-2)
485 heavyiron 1000
	mcall
205 heavyiron 1001
	mov	ebx, (data_x_pos+data_x_size+2)*10001h
485 heavyiron 1002
	mcall
205 heavyiron 1003
	mov	ebx, (data_x_pos-2)*10000h + (data_x_pos+data_x_size+2)
1004
	mov	ecx, (dump_y_pos-3)*10001h
485 heavyiron 1005
	mcall
205 heavyiron 1006
	mov	ecx, (disasm_y_pos-4)*10001h
485 heavyiron 1007
	mcall
205 heavyiron 1008
	call	draw_title
1009
	call	draw_registers
1010
	call	draw_dump
1011
	call	draw_disasm
1012
; end redraw
1013
	mov	al, 12
1014
	push	2
1015
	pop	ebx
485 heavyiron 1016
	mcall
205 heavyiron 1017
	ret
1018
 
1019
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1020
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DEBUGGING ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1021
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1022
 
1023
OnHelp:
1024
	mov	esi, help_msg
1025
	mov	edi, [curarg]
1026
	cmp	byte [edi], 0
1027
	jz	.x
1028
	mov	esi, help_groups
1029
	call	find_cmd
1030
	jc	.nocmd
1031
	mov	esi, [esi+12]
1032
.x:
1033
	jmp	put_message
1034
.nocmd:
1035
	mov	esi, aUnknownCommand
1036
	jmp	.x
1037
 
1038
OnQuit:
1039
	xor	eax, eax
1040
	dec	eax
485 heavyiron 1041
	mcall
205 heavyiron 1042
 
1043
get_new_context:
1044
	mov	esi, context
1045
	mov	edi, oldcontext
1046
	mov	ecx, 10
1047
	rep	movsd
1048
get_context:
1049
	push	1
1050
	pop	ebx
1051
	push	69
1052
	pop	eax
1053
	mov	ecx, [debuggee_pid]
1054
	mov	esi, context
1055
	push	28h
1056
	pop	edx
485 heavyiron 1057
	mcall
205 heavyiron 1058
	ret
1059
set_context:
1060
	push	2
1061
	pop	ebx
1062
	push	69
1063
	pop	eax
1064
	mov	ecx, [debuggee_pid]
1065
	mov	esi, context
1066
	push	28h
1067
	pop	edx
485 heavyiron 1068
	mcall
205 heavyiron 1069
	ret
1070
 
1071
get_dump:
1072
	mov	edi, dumpdata
1073
	mov	esi, [edi-4]
1074
	mov	edx, dump_height*10h
1075
	mov	ecx, edx
1076
	xor	eax, eax
1077
	push	edi
1078
	rep	stosb
1079
	pop	edi
1080
	mov	ecx, [debuggee_pid]
1081
	mov	al, 69
1082
	push	6
1083
	pop	ebx
485 heavyiron 1084
	mcall
205 heavyiron 1085
	cmp	eax, -1
1086
	jnz	@f
1087
	mov	esi, read_mem_err
1088
	call	put_message
1089
	xor	eax, eax
1090
@@:
1091
	mov	[edi-8], eax
1092
;	call	restore_from_breaks
1093
;	ret
1094
restore_from_breaks:
1095
; in: edi=buffer,eax=size,esi=address
1096
	mov	ebx, breakpoints
1097
@@:
1098
	test	byte [ebx+4], 1
1099
	jz	.cont		; ignore invalid
1100
	test	byte [ebx+4], 2 or 8
1101
	jnz	.cont		; ignore disabled and memory breaks
1102
	mov	ecx, [ebx]
1103
	sub	ecx, esi
1104
	cmp	ecx, eax
1105
	jae	.cont
1106
	mov	dl, [ebx+5]
1107
	mov	[edi+ecx], dl
1108
.cont:
1109
	add	ebx, 6
1110
	cmp	ebx, breakpoints+breakpoints_n*6
1111
	jb	@b
1112
	ret
1113
 
1114
OnLoad:
1115
	mov	esi, [curarg]
1116
OnLoadInit:
1117
	mov	edi, loadname
1118
	or	[prgname_len], -1
1119
	mov	[prgname_ptr], edi
1120
.copyname:
1121
	lodsb
1122
	stosb
1123
	inc	[prgname_len]
1124
	cmp	al, '/'
1125
	jnz	@f
1126
	or	[prgname_len], -1
1127
	mov	[prgname_ptr], edi
1128
@@:
1129
	cmp	al, ' '
1130
	ja	.copyname
1131
	mov	byte [edi-1], 0
1132
	and	[load_params], 0
1133
	dec	esi
1134
	call	skip_spaces
1135
	cmp	al, 0
1136
	jz	@f
1137
	mov	[load_params], esi
1138
@@:
1139
	and	[dumppos], 0
1140
do_reload:
1141
	push	18
1142
	pop	eax
1143
	push	7
1144
	pop	ebx
485 heavyiron 1145
	mcall
205 heavyiron 1146
	mov	[dbgwnd], eax
1147
	xchg	ecx, eax
1148
	push	70
1149
	pop	eax
1150
	mov	ebx, fn70_load_block
485 heavyiron 1151
	mcall
205 heavyiron 1152
	test	eax, eax
1153
	jns	.load_ok
1154
.load_err:
1155
	push	eax
1156
	mov	esi, load_err_msg
1157
	call	put_message
1158
	pop	eax
1159
	not	eax
1160
	cmp	eax, 0x20
1161
	jae	.unk_err
1162
	mov	esi, [load_err_msgs+eax*4]
1163
	test	esi, esi
1164
	jnz	put_message
1165
.unk_err:
1166
	mov	esi, unk_err_msg
1167
	inc	eax
1168
	push	eax
1169
	call	put_message_nodraw
1170
	jmp	draw_messages
1171
.load_ok:
1172
	mov	[debuggee_pid], eax
1173
	mov	[bSuspended], 1
1174
	push	ecx
1175
	call	get_context
1176
	mov	edi, oldcontext
1177
	mov	ecx, 10
1178
	rep	movsd
1179
; activate debugger window
1180
	pop	ecx
1181
	mov	bl, 3
1182
	push	18
1183
	pop	eax
485 heavyiron 1184
	mcall
205 heavyiron 1185
	call	redraw_title
1186
	call	redraw_registers
1187
	call	get_dump
1188
	call	redraw_dump
1189
	call	update_disasm_eip_force
1190
	mov	esi, load_succ_msg
1191
	push	[debuggee_pid]
1192
	call	put_message_nodraw
1193
	call	draw_messages
1194
; now test for packed progs
1195
	cmp	[disasm_buf_size], 100h
1196
	jz	@f
1197
	ret
1198
@@:
1199
	mov	esi, mxp_nrv_sig
1200
	mov	ebp, disasm_buffer
1201
	mov	edi, ebp
1202
	push	3
1203
	pop	ecx
1204
	repz	cmpsb
1205
	jnz	.not_mxp_nrv
1206
	cmpsb
1207
	mov	cl, mxp_nrv_sig_size-4
1208
	repz	cmpsb
1209
	mov	esi, mxp_nrv_name
1210
	jz	.packed
1211
.not_mxp_nrv:
1212
	mov	esi, mxp_sig
1213
	mov	edi, ebp
1214
	mov	cl, mxp_sig_size
1215
	repz	cmpsb
1216
	mov	esi, mxp_name
1217
	jz	.packed
1218
.not_mxp:
1219
	mov	esi, mxp_lzo_sig1
1220
	mov	edi, ebp
1221
	mov	cl, mxp_lzo_sig1_size
1222
	repz	cmpsb
1223
	mov	esi, mxp_lzo_name
1224
	jz	.packed
1225
	mov	esi, mxp_lzo_sig2
1226
	mov	edi, ebp
1227
	mov	cl, 8
1228
	repz	cmpsb
1229
	jnz	.not_mxp_lzo
1230
	cmpsb
1231
	mov	cl, mxp_lzo_sig2_size - 9
1232
	repz	cmpsb
1233
	mov	esi, mxp_lzo_name
1234
	jz	.packed
1235
.not_mxp_lzo:
1236
	mov	esi, mtappack_name
1237
	cmp	dword [ebp], 0xBF5E246A
1238
	jnz	.not_mtappack
1239
	cmp	dword [ebp+8], 0xEC4E8B57
1240
	jnz	.not_mtappack1
1241
	cmp	dword [ebp+12], 0x8D5EA4F3
1242
	jnz	.not_mtappack1
1243
	cmp	byte [ebp+12h], 0xE9
1244
	jz	.packed
1245
.not_mtappack1:
1246
	cmp	word [ebp+8], 0xB957
1247
	jnz	.not_mtappack
1248
	cmp	dword [ebp+14], 0x575EA4F3
1249
	jnz	.not_mtappack2
1250
	cmp	byte [ebp+17h], 0xE9
1251
	jz	.packed
1252
.not_mtappack2:
1253
	cmp	dword [ebp+14], 0x5F8DA4F3
1254
	jnz	.not_mtappack3
1255
	cmp	word [ebp+18], 0xE9FC
1256
	jz	.packed
1257
.not_mtappack3:
1258
	cmp	word [ebp+14], 0xA4F3
1259
	jnz	.not_mtappack
1260
	cmp	byte [ebp+15h], 0xE9
1261
	jz	.packed
1262
.not_mtappack:
1263
	ret
1264
.packed:
1265
	push	esi
1266
	mov	esi, aPacked1
1267
	call	put_message_nodraw
1268
	pop	esi
1269
	call	put_message_nodraw
1270
	mov	esi, aPacked2
1271
	call	put_message
1272
	call	hide_cursor
1273
	push	40
1274
	pop	eax
1275
	push	7
1276
	pop	ebx
485 heavyiron 1277
	mcall
205 heavyiron 1278
.wait:
1279
	push	10
1280
	pop	eax
485 heavyiron 1281
	mcall
205 heavyiron 1282
	dec	eax
1283
	jz	.redraw
1284
	dec	eax
1285
	jz	.key
1286
	or	eax, -1
485 heavyiron 1287
	mcall
205 heavyiron 1288
.redraw:
1289
	call	draw_window
1290
	call	hide_cursor
1291
	jmp	.wait
1292
.key:
1293
	mov	al, 2
485 heavyiron 1294
	mcall
205 heavyiron 1295
	cmp	ah, 'y'
1296
	jz	.yes
1297
	cmp	ah, 'Y'
1298
	jz	.yes
1299
	cmp	ah, 0xD
1300
	jz	.yes
1301
	cmp	ah, 'n'
1302
	jz	.no
1303
	cmp	ah, 'N'
1304
	jnz	.wait
1305
.no:
1306
	push	40
1307
	pop	eax
1308
	mov	ebx, 0x107
485 heavyiron 1309
	mcall
205 heavyiron 1310
	call	draw_cursor
1311
	mov	esi, aN_str
1312
	jmp	put_message
1313
.yes:
1314
	push	40
1315
	pop	eax
1316
	mov	ebx, 0x107
485 heavyiron 1317
	mcall
205 heavyiron 1318
	call	draw_cursor
1319
	mov	esi, aY_str
1320
	call	put_message
1321
	call	OnUnpack
1322
	ret
1323
 
1324
mxp_nrv_sig:
1325
	xor	eax, eax
1326
	mov	ecx, 0x95	; 0xA1 for programs with parameters
1327
	mov	[eax], ecx
1328
	add	ecx, [eax+24h]
1329
	push	40h
1330
	pop	esi
1331
	mov	edi, [eax+20h]
1332
	push	edi
1333
	rep	movsb
1334
	jmp	dword [esp]
1335
	pop	esi
1336
	add	esi, [eax]
1337
	xor	edi, edi
1338
mxp_nrv_sig_size = $ - mxp_nrv_sig
1339
 
1340
mxp_sig:
1341
	mov	ecx, 1CBh
1342
	push	46h
1343
	pop	esi
1344
	mov	edi, [20h]
1345
	rep	movsb
1346
	mov	ecx, [24h]
1347
	rep	movsb
1348
	jmp	dword [20h]
1349
	mov	eax, [20h]
1350
	add	eax, 1CBh
1351
	push	eax
1352
	push	dword [24h]
1353
	push	0
1354
	push	8
1355
	call	$+0x25
1356
mxp_sig_size = $ - mxp_sig
1357
 
1358
mxp_lzo_sig1:
1359
	xor	eax, eax
1360
	mov	ebp, 0FFh
1361
	mov	ecx, 175h
1362
	mov	[eax], ecx
1363
	add	ecx, [eax+24h]
1364
	push	45h
1365
	pop	esi
1366
	mov	edi, [eax+20h]
1367
	push	edi
1368
	rep	movsb
1369
	jmp	dword [esp]
1370
	pop	ebx
1371
	add	ebx, [eax]
1372
	xor	edi, edi
1373
	cmp	byte [ebx], 11h
1374
	jbe	$+0x1A
1375
mxp_lzo_sig1_size = $ - mxp_lzo_sig1
1376
mxp_lzo_sig2:
1377
	xor	eax, eax
1378
	mov	ebp, 0FFh
1379
	mov	ecx, 188h	; or 177h
1380
	mov	[eax], ecx
1381
	add	ecx, [eax+24h]
1382
	push	44h
1383
	pop	esi
1384
	mov	edi, [eax+20h]
1385
	rep	movsb
1386
	jmp	dword [eax+20h]
1387
	mov	ebx, [eax+20h]
1388
	add	ebx, [eax]
1389
mxp_lzo_sig2_size = $ - mxp_lzo_sig2
1390
 
1391
OnReload:
1392
	cmp	[debuggee_pid], 0
1393
	jnz	terminate_reload
1394
	mov	esi, need_debuggee
1395
	cmp	byte [loadname], 0
1396
	jnz	do_reload
1397
	jz	put_message
1398
terminate_reload:
1399
	mov	[bReload], 1
1400
OnTerminate:
1401
	mov	ecx, [debuggee_pid]
1402
	push	8
1403
	pop	ebx
1404
	push	69
1405
	pop	eax
485 heavyiron 1406
	mcall
205 heavyiron 1407
	ret
1408
 
1409
AfterSuspend:
1410
	mov	[bSuspended], 1
1411
	call	get_new_context
1412
	call	get_dump
1413
	call	redraw_title
1414
	call	redraw_registers
1415
	call	redraw_dump
1416
	call	update_disasm_eip
1417
	ret
1418
 
1419
OnSuspend:
1420
	mov	ecx, [debuggee_pid]
1421
	push	4
1422
	pop	ebx
1423
	push	69
1424
	pop	eax
485 heavyiron 1425
	mcall
205 heavyiron 1426
	call	AfterSuspend
1427
	mov	esi, aSuspended
1428
	jmp	put_message
1429
DoResume:
1430
	mov	ecx, [debuggee_pid]
1431
	push	5
1432
	pop	ebx
1433
	push	69
1434
	pop	eax
485 heavyiron 1435
	mcall
205 heavyiron 1436
	mov	[bSuspended], 0
1437
	ret
1438
OnResume:
1439
	mov	esi, [curarg]
1440
	cmp	byte [esi], 0
1441
	jz	GoOn
1442
	call	calc_expression
1443
	jc	.ret
1444
	mov	eax, ebp
1445
	push	eax
1446
	call	find_enabled_breakpoint
1447
	pop	eax
1448
	jz	GoOn
1449
	mov	bl, 5	; valid enabled one-shot
1450
	call	add_breakpoint
1451
	jnc	GoOn
1452
	mov	esi, aBreakpointLimitExceeded
1453
	call	put_message
1454
.ret:
1455
	ret
1456
GoOn:
1457
; test for enabled breakpoint at eip
1458
	mov	eax, [_eip]
1459
	call	find_enabled_breakpoint
1460
	jnz	.nobreak
1461
; temporarily disable breakpoint, make step, enable breakpoint, continue
1462
	inc	eax
1463
	mov	[temp_break], eax
1464
	mov	[bAfterGo], 1
1465
	dec	eax
1466
	call	disable_breakpoint
1467
	call	get_context
1468
	or	byte [_eflags+1], 1		; set TF
1469
	call	set_context
1470
	and	byte [_eflags+1], not 1
1471
	call	DoResume
1472
	ret
1473
.nobreak:
1474
	call	DoResume
1475
	call	redraw_title
1476
	call	redraw_registers
1477
	call	redraw_dump
1478
	ret
1479
OnDetach:
1480
	mov	ecx, [debuggee_pid]
1481
	push	3
1482
	pop	ebx
1483
	push	69
1484
	pop	eax
485 heavyiron 1485
	mcall
205 heavyiron 1486
	and	[debuggee_pid], 0
1487
	call	redraw_title
1488
	call	redraw_registers
1489
	call	redraw_dump
1490
	mov	esi, aContinued
1491
	jmp	put_message
1492
 
1493
after_go_exception:
1494
	push	eax
1495
	mov	eax, [temp_break]
1496
	dec	eax
1497
	push	esi
1498
	call	enable_breakpoint
1499
; in any case, clear TF and RF
1500
	call	get_new_context
1501
	and	[_eflags], not 10100h		; clear TF,RF
1502
	call	set_context
1503
	xor	edx, edx
1504
	mov	[temp_break], edx
1505
	xchg	dl, [bAfterGo]
1506
	pop	esi
1507
	pop	eax
1508
	cmp	dl, 2
1509
	jnz	@f
1510
	lodsd
1511
	push	esi
1512
	call	get_dump
1513
	jmp	exception.done
1514
@@:	test	eax, eax
1515
	jz	.notint1
1516
; if exception is result of single step, simply ignore it and continue
1517
	test	dword [esi], 0xF
1518
	jnz	dbgmsgstart.5
1519
	lodsd
1520
	push	esi
1521
	mov	esi, oldcontext
1522
	mov	edi, context
1523
	mov	ecx, 28h/4
1524
	rep	movsd
1525
	call	DoResume
1526
	jmp	dbgmsgend
1527
.notint1:
1528
; in other case, work as without temp_break
1529
	lodsd
1530
	push	esi
1531
	push	eax
1532
	jmp	exception.4
1533
.notour:
1534
 
1535
debugmsg:
1536
	neg	[dbgbufsize]
1537
	mov	esi, dbgbuf
1538
dbgmsgstart:
1539
	lodsd
1540
;	push	eax esi
1541
;	push	dword [esi]
1542
;	mov	esi, dbgmsg_str
1543
;	call	put_message_nodraw
1544
;	pop	esi eax
1545
	add	esi, 4
1546
	dec	eax
1547
	jz	exception
1548
	dec	eax
1549
	jz	terminated
1550
	mov	[bSuspended], 1
1551
	cmp	[bAfterGo], 0
1552
	jnz	after_go_exception
1553
	push	esi
1554
	call	get_new_context
1555
	and	[_eflags], not 10100h		; clear TF,RF
1556
	call	set_context
1557
	pop	esi
1558
.5:
1559
	push	esi
1560
	call	get_dump
1561
	pop	esi
1562
	lodsd
1563
	xor	ecx, ecx
1564
.6:
1565
	bt	eax, ecx
1566
	jnc	.7
1567
	mov	ebx, [drx_break+ecx*4]
1568
	test	ebx, ebx
1569
	jz	.7
1570
	pushad
1571
	dec	ebx
1572
	push	ebx
1573
	mov	esi, aBreakStop
1574
	call	put_message_nodraw
1575
	popad
1576
.7:
1577
	inc	ecx
1578
	cmp	cl, 4
1579
	jb	.6
1580
	push	esi
1581
	jmp	exception.done_draw
1582
terminated:
1583
	push	esi
1584
	mov	esi, terminated_msg
1585
	call	put_message
1586
	and	[debuggee_pid], 0
1587
	and	[temp_break], 0
1588
	mov	[bAfterGo], 0
1589
	xor	eax, eax
1590
	mov	ecx, breakpoints_n*6/4+4
1591
	mov	edi, breakpoints
1592
	rep	stosd
1593
	cmp	[bReload], 1
1594
	sbb	[bReload], -1
1595
	jmp	exception.done
1596
exception:
1597
	mov	[bSuspended], 1
1598
	cmp	[bAfterGo], 0
1599
	jnz	after_go_exception
1600
	lodsd
1601
	push	esi
1602
	push	eax
1603
	call	get_new_context
1604
	and	[_eflags], not 10100h		; clear TF,RF
1605
	call	set_context
1606
.4:
1607
	call	get_dump
1608
	pop	eax
1609
; int3 command generates exception 0D, #GP
1610
	push	eax
1611
	cmp	al, 0Dh
1612
	jnz	.notdbg
1613
; check for 0xCC byte at eip
1614
	push	0
1615
	push	69
1616
	pop	eax
1617
	push	6
1618
	pop	ebx
1619
	mov	ecx, [debuggee_pid]
1620
	mov	edi, esp
1621
	mov	esi, [_eip]
1622
	push	1
1623
	pop	edx
485 heavyiron 1624
	mcall
205 heavyiron 1625
	pop	eax
1626
	cmp	al, 0xCC
1627
	jnz	.notdbg
1628
; this is either dbg breakpoint or int3 cmd in debuggee
1629
	mov	eax, [_eip]
1630
	call	find_enabled_breakpoint
1631
	jnz	.user_int3
1632
; dbg breakpoint; clear if one-shot
1633
	pop	ecx
1634
	push	eax
1635
	mov	esi, aBreakStop
1636
	test	byte [edi+4], 4
1637
	jz	.put_msg_eax
1638
	pop	ecx
1639
	call	clear_breakpoint
1640
	jmp	.done
1641
.user_int3:
1642
	mov	eax, [_eip]
1643
	inc	[_eip]
1644
	pop	ecx
1645
	push	eax
1646
	call	set_context
1647
	mov	esi, aUserBreak
1648
	jmp	.put_msg_eax
1649
.notdbg:
1650
	mov	esi, aException
1651
.put_msg_eax:
1652
	call	put_message_nodraw
1653
.done_draw:
1654
	call	draw_messages
1655
.done:
1656
	push	18
1657
	pop	eax
1658
	push	3
1659
	pop	ebx
1660
	mov	ecx, [dbgwnd]
485 heavyiron 1661
	mcall	; activate dbg window
205 heavyiron 1662
	call	redraw_title
1663
	call	redraw_registers
1664
	call	redraw_dump
1665
	call	update_disasm_eip
1666
dbgmsgend:
1667
	pop	esi
1668
	mov	ecx, [dbgbuflen]
1669
	add	ecx, dbgbuf
1670
	cmp	esi, ecx
1671
	jnz	dbgmsgstart
1672
	and	[dbgbuflen], 0
1673
	neg	[dbgbufsize]
1674
	cmp	[bReload], 2
1675
	jnz	@f
1676
	mov	[bReload], 0
1677
	call	do_reload
1678
@@:
1679
	jmp	waitevent
1680
 
1681
CtrlF7:
1682
	cmp	[debuggee_pid], 0
1683
	jz	.no
1684
	call	OnStep
1685
.no:
1686
	jmp	waitevent
1687
CtrlF8:
1688
	cmp	[debuggee_pid], 0
1689
	jz	CtrlF7.no
1690
	call	OnProceed
1691
	jmp	CtrlF7.no
1692
 
1693
OnStep:
1694
	cmp	[bSuspended], 0
1695
	jz	.running
1696
	call	get_context
1697
	or	byte [_eflags+1], 1		; set TF
1698
	call	set_context
1699
	and	byte [_eflags+1], not 1
1700
; if instruction at eip is "int xx", set one-shot breakpoint immediately after
1701
	mov	eax, [_eip]
1702
	call	find_enabled_breakpoint
1703
	jnz	@f
1704
	cmp	byte [edi+5], 0xCD
1705
	jz	.int
1706
@@:
1707
	push	0
1708
	push	69
1709
	pop	eax
1710
	push	6
1711
	pop	ebx
1712
	mov	ecx, [debuggee_pid]
1713
	push	3
1714
	pop	edx
1715
	mov	edi, esp
1716
	mov	esi, [_eip]
485 heavyiron 1717
	mcall
205 heavyiron 1718
	cmp	eax, edx
1719
	pop	eax
1720
	jnz	.doit
1721
	cmp	al, 0xCD
1722
	jz	.int
410 diamond 1723
	cmp	ax, 0x050F
1724
	jz	.syscall_enter
1725
	cmp	ax, 0x340F
1726
	jz	.syscall_enter
205 heavyiron 1727
; resume process
1728
.doit:
1729
	call	GoOn
1730
	cmp	[bAfterGo], 0
1731
	jz	@f
1732
	mov	[bAfterGo], 2
1733
@@:
1734
	ret
410 diamond 1735
.syscall_enter:
1736
	and	byte [_eflags+1], not 1	; clear TF - avoid system halt (!)
1737
	call	set_context
205 heavyiron 1738
.int:
1739
	mov	eax, [_eip]
1740
	inc	eax
1741
	inc	eax
1742
	push	eax
1743
	call	find_enabled_breakpoint
1744
	pop	eax
1745
	jz	.doit
1746
; there is no enabled breakpoint yet; set temporary breakpoint
1747
	mov	bl, 5
1748
	call	add_breakpoint
1749
	jmp	.doit
1750
.running:
1751
	mov	esi, aRunningErr
1752
	jmp	put_message
1753
 
1754
OnProceed:
1755
	cmp	[bSuspended], 0
1756
	jz	OnStep.running
1757
	mov	esi, [_eip]
1758
@@:
1759
	call	get_byte_nobreak
1760
	jc	OnStep
1761
	inc	esi
1762
; skip prefixes
1763
	call	is_prefix
1764
	jz	@b
1765
	cmp	al, 0xE8	; call
1766
	jnz	@f
1767
	add	esi, 4
1768
	jmp	.doit
1769
@@:	; A4,A5 = movs, A6,A7=cmps
1770
	cmp	al, 0xA4
1771
	jb	@f
1772
	cmp	al, 0xA8
1773
	jb	.doit
1774
@@:	; AA,AB=stos, AC,AD=lods, AE,AF=scas
1775
	cmp	al, 0xAA
1776
	jb	@f
1777
	cmp	al, 0xB0
1778
	jb	.doit
1779
@@:	; E0=loopnz,E1=loopz,E2=loop
1780
	cmp	al, 0xE0
1781
	jb	.noloop
1782
	cmp	al, 0xE2
1783
	ja	.noloop
1784
	inc	esi
1785
	jmp	.doit
1786
.noloop:	; FF /2 = call
1787
	cmp	al, 0xFF
1788
	jnz	OnStep
1789
	call	get_byte_nobreak
1790
	jc	OnStep
1791
	inc	esi
1792
	mov	cl, al
1793
	and	al, 00111000b
1794
	cmp	al, 00010000b
1795
	jnz	OnStep
1796
; skip instruction
1797
	mov	al, cl
1798
	and	eax, 7
1799
	shr	cl, 6
1800
	jz	.mod0
1801
	jp	.doit
1802
	cmp	al, 4
1803
	jnz	@f
1804
	inc	esi
1805
@@:
1806
	inc	esi
1807
	dec	cl
1808
	jz	@f
1809
	add	esi, 3
1810
@@:
1811
	jmp	.doit
1812
.mod0:
1813
	cmp	al, 4
1814
	jnz	@f
1815
	call	get_byte_nobreak
1816
	jc	OnStep
1817
	inc	esi
1818
	and	al, 7
1819
@@:
1820
	cmp	al, 5
1821
	jnz	.doit
1822
	add	esi, 4
1823
.doit:
1824
; insert one-shot breakpoint at esi and resume
1825
	call	get_byte_nobreak
1826
	jc	OnStep
1827
	mov	eax, esi
1828
	call	find_enabled_breakpoint
1829
	jz	.ret
1830
	mov	eax, esi
1831
	mov	bl, 5
1832
	call	add_breakpoint
1833
	jmp	OnStep.doit
1834
.ret:
1835
	ret
1836
 
1837
get_byte_nobreak:
1838
	mov	eax, esi
1839
	call	find_enabled_breakpoint
1840
	jnz	.nobreak
1841
	mov	al, [edi+5]
1842
	clc
1843
	ret
1844
.nobreak:
1845
	push	69
1846
	pop	eax
1847
	push	6
1848
	pop	ebx
1849
	mov	ecx, [debuggee_pid]
1850
	xor	edx, edx
1851
	push	edx
1852
	inc	edx
1853
	mov	edi, esp
485 heavyiron 1854
	mcall
205 heavyiron 1855
	dec	eax
1856
	clc
1857
	jz	@f
1858
	stc
1859
@@:	pop	eax
1860
	ret
1861
 
1862
is_prefix:
1863
	cmp	al, 0x64	; fs:
1864
	jz	.ret
1865
	cmp	al, 0x65	; gs:
1866
	jz	.ret
1867
	cmp	al, 0x66	; use16/32
1868
	jz	.ret
1869
	cmp	al, 0x67	; addr16/32
1870
	jz	.ret
1871
	cmp	al, 0xF0	; lock
1872
	jz	.ret
1873
	cmp	al, 0xF2	; repnz
1874
	jz	.ret
1875
	cmp	al, 0xF3	; rep(z)
1876
	jz	.ret
1877
	cmp	al, 0x2E	; cs:
1878
	jz	.ret
1879
	cmp	al, 0x36	; ss:
1880
	jz	.ret
1881
	cmp	al, 0x3E	; ds:
1882
	jz	.ret
1883
	cmp	al, 0x26	; es:
1884
.ret:	ret
1885
 
1886
token_end	equ	1
1887
token_reg	equ	2
1888
token_hex	equ	3
1889
token_add	equ	4
1890
token_sub	equ	5
1891
token_mul	equ	6
1892
token_div	equ	7
1893
token_lp	equ	8
1894
token_rp	equ	9
1895
token_err	equ	-1
1896
 
1897
is_hex_digit:
1898
	cmp	al, '0'
1899
	jb	.no
1900
	cmp	al, '9'
1901
	jbe	.09
1902
	cmp	al, 'A'
1903
	jb	.no
1904
	cmp	al, 'F'
1905
	jbe	.AF
1906
	cmp	al, 'a'
1907
	jb	.no
1908
	cmp	al, 'f'
1909
	jbe	.af
1910
.no:
1911
	stc
1912
	ret
1913
.09:
1914
	sub	al, '0'
1915
;	clc
1916
	ret
1917
.AF:
1918
	sub	al, 'A'-10
1919
;	clc
1920
	ret
1921
.af:
1922
	sub	al, 'a'-10
1923
;	clc
1924
	ret
1925
 
1926
find_reg:
1927
	mov	edi, reg_table
1928
.findreg:
1929
	movzx	ecx, byte [edi]
1930
	stc
1931
	jecxz	.regnotfound
1932
	inc	edi
1933
	push	esi edi ecx
1934
@@:
1935
	lodsb
1936
	or	al, 20h
1937
	scasb
1938
	loopz	@b
1939
	pop	ecx edi esi
1940
	lea	edi, [edi+ecx+1]
1941
	jnz	.findreg
1942
	movzx	edi, byte [edi-1]
1943
	add	esi, ecx
1944
.regnotfound:
1945
	ret
1946
 
1947
expr_get_token:
1948
	lodsb
1949
	cmp	al, 0
1950
	jz	.end_token
1951
	cmp	al, ' '
1952
	jbe	expr_get_token
1953
	cmp	al, '+'
1954
	jz	.add
1955
	cmp	al, '-'
1956
	jz	.sub
1957
	cmp	al, '*'
1958
	jz	.mul
1959
	cmp	al, '/'
1960
	jz	.div
1961
	cmp	al, '('
1962
	jz	.lp
1963
	cmp	al, ')'
1964
	jnz	.notsign
1965
.rp:
1966
	mov	al, token_rp
1967
	ret
1968
.div:
1969
	mov	al, token_div
1970
	ret
1971
.end_token:
1972
	mov	al, token_end
1973
	ret
1974
.add:
1975
	mov	al, token_add
1976
	ret
1977
.sub:
1978
	mov	al, token_sub
1979
	ret
1980
.mul:
1981
	mov	al, token_mul
1982
	ret
1983
.lp:
1984
	mov	al, token_lp
1985
	ret
1986
.notsign:
1987
	dec	esi
1988
	call	find_reg
1989
	jc	.regnotfound
1990
	mov	al, token_reg
1991
	ret
1992
.regnotfound:
1993
; test for hex number
1994
	xor	ecx, ecx
1995
	xor	edi, edi
1996
	xor	eax, eax
1997
@@:
1998
	lodsb
1999
	call	is_hex_digit
2000
	jc	@f
2001
	shl	edi, 4
2002
	or	edi, eax
2003
	inc	ecx
2004
	jmp	@b
2005
@@:
2006
	dec	esi
2007
	jecxz	.err
2008
	cmp	ecx, 8
2009
	ja	.err
2010
	mov	al, token_hex
2011
	ret
2012
.err:
2013
	mov	al, token_err
2014
	mov	esi, aParseError
2015
	ret
2016
 
2017
expr_read2:
2018
	cmp	al, token_hex
2019
	jz	.hex
2020
	cmp	al, token_reg
2021
	jz	.reg
2022
	cmp	al, token_lp
2023
	jz	.lp
2024
	mov	al, token_err
2025
	mov	esi, aParseError
2026
	ret
2027
.hex:
2028
	mov	ebp, edi
2029
.ret:
2030
	jmp	expr_get_token
2031
.reg:
2032
	cmp	edi, 24
2033
	jz	.eip
2034
	sub	edi, 4
2035
	jb	.8lo
2036
	sub	edi, 4
2037
	jb	.8hi
2038
	sub	edi, 8
2039
	jb	.16
2040
	mov	ebp, [_eax+edi*4]
2041
	jmp	.ret
2042
.16:
2043
	movzx	ebp, word [_eax+(edi+8)*4]
2044
	jmp	.ret
2045
.8lo:
2046
	movzx	ebp, byte [_eax+(edi+4)*4]
2047
	jmp	.ret
2048
.8hi:
2049
	movzx	ebp, byte [_eax+(edi+4)*4+1]
2050
	jmp	.ret
2051
.eip:
2052
	mov	ebp, [_eip]
2053
	jmp	.ret
2054
.lp:
2055
	call	expr_get_token
2056
	call	expr_read0
2057
	cmp	al, token_err
2058
	jz	@f
2059
	cmp	al, token_rp
2060
	jz	expr_get_token
2061
	mov	al, token_err
2062
	mov	esi, aParseError
2063
@@:	ret
2064
 
2065
expr_read1:
2066
	call	expr_read2
2067
.1:
2068
	cmp	al, token_mul
2069
	jz	.mul
2070
	cmp	al, token_div
2071
	jz	.div
2072
	ret
2073
.mul:
2074
	push	ebp
2075
	call	expr_get_token
2076
	call	expr_read2
2077
	pop	edx
2078
; ebp := edx*ebp
2079
	imul	ebp, edx
2080
	jmp	.1
2081
.div:
2082
	push	ebp
2083
	call	expr_get_token
2084
	call	expr_read2
2085
	pop	edx
2086
; ebp := edx/ebp
2087
	test	ebp, ebp
2088
	jz	.div0
2089
	push	eax
2090
	xor	eax, eax
2091
	xchg	eax, edx
2092
	div	ebp
2093
	xchg	eax, ebp
2094
	pop	eax
2095
	jmp	.1
2096
.div0:
2097
	mov	al, token_err
2098
	mov	esi, aDivByZero
2099
	ret
2100
 
2101
expr_read0:
2102
	xor	ebp, ebp
2103
	cmp	al, token_add
2104
	jz	.add
2105
	cmp	al, token_sub
2106
	jz	.sub
2107
	call	expr_read1
2108
.1:
2109
	cmp	al, token_add
2110
	jz	.add
2111
	cmp	al, token_sub
2112
	jz	.sub
2113
	ret
2114
.add:
2115
	push	ebp
2116
	call	expr_get_token
2117
	call	expr_read1
2118
	pop	edx
2119
; ebp := edx+ebp
2120
	add	ebp, edx
2121
	jmp	.1
2122
.sub:
2123
	push	ebp
2124
	call	expr_get_token
2125
	call	expr_read1
2126
	pop	edx
2127
; ebp := edx-ebp
2128
	xchg	edx, ebp
2129
	sub	ebp, edx
2130
	jmp	.1
2131
 
2132
calc_expression:
2133
; in: esi->expression
2134
; out: CF=1 if error
2135
;      CF=0 and ebp=value if ok
2136
	call	expr_get_token
2137
	call	expr_read0
2138
	cmp	al, token_end
2139
	jz	.end
2140
	cmp	al, token_err
2141
	jz	@f
2142
	mov	esi, aParseError
2143
@@:
2144
	call	put_message
2145
	stc
2146
	ret
2147
.end:
2148
	clc
2149
	ret
2150
 
2151
OnCalc:
2152
	mov	esi, [curarg]
2153
	call	calc_expression
2154
	jc	.ret
2155
	push	ebp
2156
	mov	esi, calc_string
2157
	call	put_message_nodraw
2158
	jmp	draw_messages
2159
.ret:
2160
	ret
2161
 
2162
OnDump:
2163
	mov	esi, [curarg]
2164
	cmp	byte [esi], 0
2165
	jnz	.param
2166
	add	[dumppos], dump_height*10h
2167
	jmp	.doit
2168
.param:
2169
	call	calc_expression
2170
	jc	.ret
2171
	mov	[dumppos], ebp
2172
.doit:
2173
	call	get_dump
2174
	call	redraw_dump
2175
.ret:
2176
	ret
2177
 
2178
OnUnassemble:
2179
	mov	esi, [curarg]
2180
	cmp	byte [esi], 0
2181
	jnz	.param
2182
	mov	eax, [disasm_start_pos]
2183
	mov	ecx, disasm_height
2184
	mov	[disasm_cur_pos], eax
2185
@@:
2186
	push	ecx
2187
	call	disasm_instr
2188
	pop	ecx
2189
	jc	.err
2190
	loop	@b
2191
	mov	eax, [disasm_cur_pos]
2192
	jmp	.doit
2193
.param:
2194
	call	calc_expression
2195
	jc	.ret
2196
	mov	eax, ebp
2197
.doit:
2198
	push	eax
2199
	push	[disasm_start_pos]
2200
	mov	[disasm_start_pos], eax
2201
	call	update_disasm
2202
	pop	[disasm_start_pos]
2203
	pop	eax
2204
	cmp	[disasm_cur_str], 0
2205
	jz	@f
2206
	mov	[disasm_start_pos], eax
2207
.ret:
2208
	ret
2209
@@:
2210
	call	update_disasm
2211
.err:
2212
	mov	esi, aInvAddr
2213
	jmp	put_message
2214
 
2215
OnReg:
2216
	mov	esi, [curarg]
2217
	call	skip_spaces
2218
	call	find_reg
2219
	jnc	@f
2220
.err:
2221
	mov	esi, RSyntax
2222
	jmp	put_message
2223
@@:
2224
	call	skip_spaces
2225
	test	al, al
2226
	jz	.err
2227
	cmp	al, '='
2228
	jnz	@f
2229
	inc	esi
2230
	call	skip_spaces
2231
	test	al, al
2232
	jz	.err
2233
@@:
2234
	push	edi
2235
	call	calc_expression
2236
	pop	edi
2237
	jc	.ret
2238
; now edi=register id, ebp=value
2239
	cmp	[bSuspended], 0
2240
	mov	esi, aRunningErr
2241
	jz	put_message
2242
	xchg	eax, ebp
2243
	cmp	edi, 24
2244
	jz	.eip
2245
	sub	edi, 4
2246
	jb	.8lo
2247
	sub	edi, 4
2248
	jb	.8hi
2249
	sub	edi, 8
2250
	jb	.16
2251
	mov	[_eax+edi*4], eax
2252
	jmp	.ret
2253
.16:
2254
	mov	word [_eax+(edi+8)*4], ax
2255
	jmp	.ret
2256
.8lo:
2257
	mov	byte [_eax+(edi+4)*4], al
2258
	jmp	.ret
2259
.8hi:
2260
	mov	byte [_eax+(edi+4)*4+1], al
2261
	jmp	.ret
2262
.eip:
2263
	mov	[_eip], eax
2264
	call	update_disasm_eip
2265
.ret:
2266
	call	set_context
2267
	jmp	redraw_registers
2268
 
2269
; Breakpoints manipulation
2270
OnBp:
2271
	mov	esi, [curarg]
2272
	call	calc_expression
2273
	jc	.ret
2274
	xchg	eax, ebp
2275
	push	eax
2276
	call	find_breakpoint
2277
	inc	eax
2278
	pop	eax
2279
	jz	.notfound
2280
	mov	esi, aDuplicateBreakpoint
2281
	jmp	.sayerr
2282
.notfound:
2283
	mov	bl, 1
2284
	call	add_breakpoint
2285
	jnc	.ret
2286
	mov	esi, aBreakpointLimitExceeded
2287
.sayerr:
2288
	call	put_message
2289
.ret:
2290
	jmp	redraw_disasm
2291
 
2292
OnBpmb:
2293
	mov	dh, 0011b
2294
	jmp	DoBpm
2295
OnBpmw:
2296
	mov	dh, 0111b
2297
	jmp	DoBpm
2298
OnBpmd:
2299
	mov	dh, 1111b
2300
DoBpm:
2301
	mov	esi, [curarg]
2302
	cmp	byte [esi], 'w'
2303
	jnz	@f
2304
	and	dh, not 2
2305
	inc	esi
2306
@@:
2307
	push	edx
2308
	call	calc_expression
2309
	pop	edx
2310
	jnc	@f
2311
	ret
2312
@@:
2313
; ebp=expression, dh=flags
2314
	movzx	eax, dh
2315
	shr	eax, 2
2316
	test	ebp, eax
2317
	jz	@f
2318
	mov	esi, aUnaligned
2319
	jmp	put_message
2320
@@:
2321
	mov	eax, ebp
2322
	mov	bl, 0Bh
2323
	call	add_breakpoint
2324
	jnc	@f
2325
	mov	esi, aBreakpointLimitExceeded
2326
	jmp	put_message
2327
@@:
2328
; now find index
2329
	push	eax
2330
	xor	ecx, ecx
2331
.l1:
2332
	cmp	[drx_break+ecx*4], 0
2333
	jnz	.l2
2334
	push	69
2335
	pop	eax
2336
	push	ecx
2337
	mov	dl, cl
2338
	mov	ecx, [debuggee_pid]
2339
	mov	esi, ebp
2340
	push	9
2341
	pop	ebx
485 heavyiron 2342
	mcall
205 heavyiron 2343
	test	eax, eax
2344
	jz	.ok
2345
	pop	ecx
2346
.l2:
2347
	inc	ecx
2348
	cmp	ecx, 4
2349
	jb	.l1
2350
	pop	eax
2351
	call	clear_breakpoint
2352
	mov	esi, aBreakpointLimitExceeded
2353
	jmp	put_message
2354
.ok:
2355
	pop	ecx
2356
	pop	eax
2357
	and	byte [edi], not 2	; breakpoint is enabled
2358
	shl	dl, 6
2359
	or	dl, dh
2360
	mov	byte [edi+1], dl
2361
	inc	eax
2362
	mov	[drx_break+ecx*4], eax
2363
	ret
2364
 
2365
OnBc:
2366
	mov	esi, [curarg]
2367
@@:	call	get_hex_number
2368
	jc	OnBp.ret
2369
	call	clear_breakpoint
2370
	jmp	@b
2371
 
2372
OnBd:
2373
	mov	esi, [curarg]
2374
@@:	call	get_hex_number
2375
	jc	OnBp.ret
2376
	call	disable_breakpoint
2377
	jmp	@b
2378
 
2379
OnBe:
2380
	mov	esi, [curarg]
2381
@@:	call	get_hex_number
2382
	jc	OnBp.ret
2383
	push	eax
2384
	call	find_enabled_breakpoint
2385
	pop	eax
2386
	jz	.err
2387
	call	enable_breakpoint
2388
	jmp	@b
2389
.err:
2390
	mov	esi, OnBeErrMsg
2391
	jmp	put_message
2392
 
2393
get_hex_number:
2394
	call	skip_spaces
2395
	xor	ecx, ecx
2396
	xor	edx, edx
2397
@@:
2398
	lodsb
2399
	call	is_hex_digit
2400
	jc	.ret
2401
	shl	edx, 4
2402
	or	dl, al
2403
	inc	ecx
2404
	jmp	@b
2405
.ret:
2406
	dec	esi
2407
	cmp	ecx, 1
2408
	xchg	eax, edx
2409
	ret
2410
 
2411
OnBl:
2412
	mov	esi, [curarg]
2413
	cmp	byte [esi], 0
2414
	jz	.listall
2415
	call	get_hex_number
2416
	jc	.ret
2417
	cmp	eax, breakpoints_n
2418
	jae	.err
2419
	push	eax
2420
	add	eax, eax
2421
	lea	edi, [breakpoints + eax + eax*2]
2422
	pop	eax
2423
	test	byte [edi+4], 1
2424
	jz	.err
2425
	call	show_break_info
2426
.ret:
2427
	ret
2428
.err:
2429
	mov	esi, aInvalidBreak
2430
	jmp	put_message
2431
.listall:
2432
	mov	edi, breakpoints
2433
	xor	eax, eax
2434
@@:
2435
	test	byte [edi+4], 1
2436
	jz	.cont
2437
	push	edi eax
2438
	call	show_break_info
2439
	pop	eax edi
2440
.cont:
2441
	add	edi, 6
2442
	inc	eax
2443
	cmp	eax, breakpoints_n
2444
	jb	@b
2445
	ret
2446
 
2447
show_break_info:
2448
	push	edi
2449
	test	byte [edi+4], 8
2450
	jnz	.dr
2451
	push	dword [edi]
2452
	push	eax
2453
	mov	esi, aBreakNum
2454
	call	put_message_nodraw
2455
	jmp	.cmn
2456
.dr:
2457
	push	eax
2458
	mov	esi, aMemBreak1
2459
	call	put_message_nodraw
2460
	pop	edi
2461
	push	edi
2462
	mov	esi, aMemBreak2
2463
	test	byte [edi+5], 2
2464
	jz	@f
2465
	mov	esi, aMemBreak3
2466
@@:
2467
	call	put_message_nodraw
2468
	pop	edi
2469
	push	edi
2470
	mov	esi, aMemBreak6
2471
	test	byte [edi+5], 8
2472
	jnz	@f
2473
	mov	esi, aMemBreak5
2474
	test	byte [edi+5], 4
2475
	jnz	@f
2476
	mov	esi, aMemBreak4
2477
@@:
2478
	call	put_message_nodraw
2479
	pop	edi
2480
	push	edi
2481
	push	dword [edi]
2482
	mov	esi, aMemBreak7
2483
	call	put_message_nodraw
2484
.cmn:
2485
	pop	edi
2486
	test	byte [edi+4], 2
2487
	jz	@f
2488
	push	edi
2489
	mov	esi, aDisabled
2490
	call	put_message_nodraw
2491
	pop	edi
2492
@@:
2493
	test	byte [edi+4], 4
2494
	jz	@f
2495
	mov	esi, aOneShot
2496
	call	put_message_nodraw
2497
@@:
2498
	mov	esi, newline
2499
	jmp	put_message
2500
 
2501
add_breakpoint:
2502
; in: eax=address, bl=flags
2503
; out: CF=1 => error, CF=0 => eax=breakpoint number
2504
	xor	ecx, ecx
2505
	mov	edi, breakpoints
2506
@@:
2507
	test	byte [edi+4], 1
2508
	jz	.found
2509
	add	edi, 6
2510
	inc	ecx
2511
	cmp	ecx, breakpoints_n
2512
	jb	@b
2513
	stc
2514
	ret
2515
.found:
2516
	stosd
2517
	xchg	eax, ecx
2518
	mov	[edi], bl
2519
	test	bl, 2
2520
	jnz	@f
2521
	or	byte [edi], 2
2522
	push	eax
2523
	call	enable_breakpoint
2524
	pop	eax
2525
@@:
2526
	clc
2527
	ret
2528
 
2529
clear_breakpoint:
2530
	cmp	eax, breakpoints_n
2531
	jae	.ret
2532
	mov	ecx, 4
2533
	inc	eax
2534
.1:
2535
	cmp	[drx_break-4+ecx*4], eax
2536
	jnz	@f
2537
	and	[drx_break-4+ecx*4], 0
2538
@@:	loop	.1
2539
	dec	eax
2540
	push	eax
2541
	add	eax, eax
2542
	lea	edi, [breakpoints + eax + eax*2 + 4]
2543
	test	byte [edi], 1
2544
	pop	eax
2545
	jz	.ret
2546
	push	edi
2547
	call	disable_breakpoint
2548
	pop	edi
2549
	mov	byte [edi], 0
2550
.ret:
2551
	ret
2552
 
2553
disable_breakpoint:
2554
	cmp	eax, breakpoints_n
2555
	jae	.ret
2556
	add	eax, eax
2557
	lea	edi, [breakpoints + eax + eax*2 + 5]
2558
	test	byte [edi-1], 1
2559
	jz	.ret
2560
	test	byte [edi-1], 2
2561
	jnz	.ret
2562
	or	byte [edi-1], 2
2563
	test	byte [edi-1], 8
2564
	jnz	.dr
2565
	push	esi
2566
	push	7
2567
	pop	ebx
2568
	push	69
2569
	pop	eax
2570
	mov	ecx, [debuggee_pid]
2571
	xor	edx, edx
2572
	inc	edx
2573
	mov	esi, [edi-5]
485 heavyiron 2574
	mcall
205 heavyiron 2575
	pop	esi
2576
.ret:
2577
	ret
2578
.dr:
2579
	mov	dl, [edi]
2580
	shr	dl, 6
2581
	mov	dh, 80h
2582
	push	69
2583
	pop	eax
2584
	push	9
2585
	pop	ebx
2586
	mov	ecx, [debuggee_pid]
485 heavyiron 2587
	mcall
205 heavyiron 2588
	ret
2589
 
2590
enable_breakpoint:
2591
	push	esi
2592
	cmp	eax, breakpoints_n
2593
	jae	.ret
2594
	add	eax, eax
2595
	lea	edi, [breakpoints + eax + eax*2 + 5]
2596
	test	byte [edi-1], 1
2597
	jz	.ret
2598
	test	byte [edi-1], 2
2599
	jz	.ret
2600
	and	byte [edi-1], not 2
2601
	test	byte [edi-1], 8
2602
	jnz	.dr
2603
	push	6
2604
	pop	ebx
2605
	push	69
2606
	pop	eax
2607
	mov	esi, [edi-5]
2608
	mov	ecx, [debuggee_pid]
2609
	xor	edx, edx
2610
	inc	edx
485 heavyiron 2611
	mcall
205 heavyiron 2612
	dec	eax
2613
	jnz	.err
2614
	mov	al, 69
2615
	push	0xCC
2616
	mov	edi, esp
2617
	inc	ebx
485 heavyiron 2618
	mcall
205 heavyiron 2619
	pop	eax
2620
.ret:
2621
	pop	esi
2622
	ret
2623
.err:
2624
	or	byte [edi-1], 2
2625
	mov	esi, aBreakErr
2626
	call	put_message
2627
	pop	esi
2628
	ret
2629
.dr:
2630
	push	9
2631
	pop	ebx
2632
	push	69
2633
	pop	eax
2634
	mov	esi, [edi-5]
2635
	mov	ecx, [debuggee_pid]
2636
	mov	dl, [edi]
2637
	shr	dl, 6
2638
	mov	dh, [edi]
2639
	and	dh, 0xF
485 heavyiron 2640
	mcall
205 heavyiron 2641
	test	eax, eax
2642
	jnz	.err
2643
	pop	esi
2644
	ret
2645
 
2646
find_breakpoint:
2647
	xor	ecx, ecx
2648
	xchg	eax, ecx
2649
	mov	edi, breakpoints
2650
@@:
2651
	test	byte [edi+4], 1
2652
	jz	.cont
2653
	test	byte [edi+4], 8
2654
	jnz	.cont
2655
	cmp	[edi], ecx
2656
	jz	.found
2657
.cont:
2658
	add	edi, 6
2659
	inc	eax
2660
	cmp	eax, breakpoints_n
2661
	jb	@b
2662
	or	eax, -1
2663
.found:
2664
	ret
2665
 
2666
find_enabled_breakpoint:
2667
	xor	ecx, ecx
2668
	xchg	eax, ecx
2669
	mov	edi, breakpoints
2670
@@:
2671
	test	byte [edi+4], 1
2672
	jz	.cont
2673
	test	byte [edi+4], 2 or 8
2674
	jnz	.cont
2675
	cmp	[edi], ecx
2676
	jz	.found
2677
.cont:
2678
	add	edi, 6
2679
	inc	eax
2680
	cmp	eax, breakpoints_n
2681
	jb	@b
2682
	or	eax, -1
2683
.found:
2684
	ret
2685
 
2686
OnUnpack:
2687
; program must be loaded - checked when command was parsed
2688
; program must be stopped
2689
	mov	esi, aRunningErr
2690
	cmp	[bSuspended], 0
2691
	jz	put_message
2692
; all breakpoints must be disabled
2693
	mov	edi, breakpoints
2694
@@:
2695
	test	byte [edi+4], 1
2696
	jz	.cont
2697
	test	byte [edi+4], 2
2698
	jnz	.cont
2699
	mov	esi, aEnabledBreakErr
2700
	jmp	put_message
2701
.cont:
2702
	add	edi, 6
2703
	cmp	edi, breakpoints+breakpoints_n*6
2704
	jb	@b
2705
; ok, now do it
2706
; set breakpoint on 0xC dword access
2707
	push	9
2708
	pop	ebx
2709
	mov	ecx, [debuggee_pid]
2710
	mov	dx, 1111b*256
2711
	push	0xC
2712
	pop	esi
2713
@@:
2714
	push	69
2715
	pop	eax
485 heavyiron 2716
	mcall
205 heavyiron 2717
	test	eax, eax
2718
	jz	.breakok
2719
	inc	edx
2720
	cmp	dl, 4
2721
	jb	@b
2722
.breakok:
2723
	call	GoOn
2724
; now wait for event
2725
.wait:
2726
	push	10
2727
	pop	eax
485 heavyiron 2728
	mcall
205 heavyiron 2729
	dec	eax
2730
	jz	.redraw
2731
	dec	eax
2732
	jz	.key
2733
	dec	eax
2734
	jnz	.debug
2735
; button; we have only one button, close
2736
	or	eax, -1
485 heavyiron 2737
	mcall
205 heavyiron 2738
.redraw:
2739
	call	draw_window
2740
	jmp	.wait
2741
.key:
2742
	mov	al, 2
485 heavyiron 2743
	mcall
205 heavyiron 2744
	cmp	ah, 3	; Ctrl+C
2745
	jnz	.wait
2746
.userbreak:
2747
	mov	esi, aInterrupted
2748
.x1:
2749
	push	edx esi
2750
	call	put_message
2751
	pop	esi edx
2752
	or	dh, 80h
2753
	push	69
2754
	pop	eax
2755
	push	9
2756
	pop	ebx
2757
	mov	ecx, [debuggee_pid]
485 heavyiron 2758
	mcall
205 heavyiron 2759
	cmp	esi, aUnpacked
2760
	jnz	OnSuspend
2761
	jmp	AfterSuspend
2762
.debug:
2763
	cmp	[dbgbuflen], 4*3
2764
	jnz	.notour
2765
	cmp	dword [dbgbuf], 3
2766
	jnz	.notour
2767
	test	byte [dbgbuf+8], 1
2768
	jnz	.our
2769
.notour:
2770
	mov	esi, aInterrupted
2771
	push	edx
2772
	call	put_message
2773
	pop	edx
2774
	or	dh, 80h
2775
	push	69
2776
	pop	eax
2777
	push	9
2778
	pop	ebx
2779
	mov	ecx, [debuggee_pid]
485 heavyiron 2780
	mcall
205 heavyiron 2781
	jmp	debugmsg
2782
.our:
2783
	and	[dbgbuflen], 0
2784
	push	edx
2785
	call	get_context
2786
	push	eax
2787
	mov	al, 69
2788
	mov	bl, 6
2789
	mov	ecx, [debuggee_pid]
2790
	mov	edi, esp
2791
	push	4
2792
	pop	edx
2793
	push	0xC
2794
	pop	esi
485 heavyiron 2795
	mcall
205 heavyiron 2796
	pop	eax
2797
	pop	edx
2798
	cmp	eax, [_eip]
2799
	jz	.done
2800
	call	DoResume
2801
	jmp	.wait
2802
.done:
2803
	mov	esi, aUnpacked
2804
	jmp	.x1
2805
 
2806
disasm_get_byte:
2807
; out: al=byte
2808
	push	ecx
2809
	mov	ecx, [disasm_cur_pos]
2810
	sub	ecx, [disasm_start_pos]
2811
	cmp	ecx, [disasm_buf_size]
2812
	jae	disasm_err
2813
	mov	al, [disasm_buffer+ecx]
2814
	pop	ecx
2815
	inc	[disasm_cur_pos]
2816
	ret
2817
disasm_get_word:
2818
	push	ecx
2819
	mov	ecx, [disasm_cur_pos]
2820
	sub	ecx, [disasm_start_pos]
2821
	inc	ecx
2822
	cmp	ecx, [disasm_buf_size]
2823
	jae	disasm_err
2824
	mov	ax, word [disasm_buffer-1+ecx]
2825
	pop	ecx
2826
	add	[disasm_cur_pos], 2
2827
	ret
2828
disasm_get_dword:
2829
	push	ecx
2830
	mov	ecx, [disasm_cur_pos]
2831
	sub	ecx, [disasm_start_pos]
2832
	add	ecx, 3
2833
	cmp	ecx, [disasm_buf_size]
2834
	jae	disasm_err
2835
	mov	eax, dword [disasm_buffer-3+ecx]
2836
	pop	ecx
2837
	add	[disasm_cur_pos], 4
2838
	ret
2839
 
2840
disasm_err:
2841
	mov	esp, ebp
2842
stc_ret:
2843
	stc
2844
	ret
2845
disasm_ret:
2846
	mov	esp, ebp
2847
	and	byte [edi], 0
2848
	ret
2849
 
2850
disasm_instr:
2851
	mov	ebp, esp
2852
	cmp	[debuggee_pid], 0
2853
	jz	stc_ret
2854
	mov	edi, disasm_string
2855
	xor	ecx, ecx
2856
; ecx=flags
2857
disasm_loop1:
2858
	xor	eax, eax
2859
	call	disasm_get_byte
2860
	jmp	dword [disasm_table_1 + eax*4]
2861
 
2862
cop0:
2863
clock:
2864
crepnz:
2865
crep:
2866
csegcs:
2867
csegds:
2868
cseges:
2869
csegss:
2870
csegfs:
2871
cseggs:
2872
	call	@f
2873
	db	0x2E,3,'cs:'
2874
	db	0x36,3,'ss:'
2875
	db	0x3E,3,'ds:'
2876
	db	0x26,3,'es:'
2877
	db	0x64,3,'fs:'
2878
	db	0x65,3,'gs:'
2879
	db	0x06,10,'push    es'
2880
	db	0x07,10,'pop     es'
2881
	db	0x0E,10,'push    cs'
2882
	db	0x16,10,'push    ss'
2883
	db	0x17,10,'pop     ss'
2884
	db	0x1E,10,'push    ds'
2885
	db	0x1F,10,'pop     ds'
2886
	db	0x27,3,'daa'
2887
	db	0x2F,3,'das'
2888
	db	0x37,3,'aaa'
2889
	db	0x3F,3,'aas'
2890
	db	0x60,6,0,'pusha'
2891
	db	0x61,5,0,'popa'
2892
	db	0x90,3,'nop'
2893
	db	0x9B,5,'fwait'
2894
	db	0x9C,6,0,'pushf'
2895
	db	0x9D,5,0,'popf'
2896
	db	0x9E,4,'sahf'
2897
	db	0x9F,4,'lahf'
2898
	db	0xA4,5,'movsb'
2899
	db	0xA5,5,0,'movs'
2900
	db	0xA6,5,'cmpsb'
2901
	db	0xA7,5,0,'cmps'
2902
	db	0xAA,5,'stosb'
2903
	db	0xAB,5,0,'stos'
2904
	db	0xAC,5,'lodsb'
2905
	db	0xAD,5,0,'lods'
2906
	db	0xAE,5,'scasb'
2907
	db	0xAF,5,0,'scas'
2908
	db	0xC3,3,'ret'
2909
	db	0xC9,5,'leave'
2910
	db	0xCC,4,'int3'
2911
	db	0xF0,4,'lock'
2912
	db	0xF2,5,'repnz'
2913
	db	0xF3,6,'rep(z)'
2914
	db	0xF5,3,'cmc'
2915
	db	0xF8,3,'clc'
2916
	db	0xF9,3,'stc'
2917
	db	0xFA,3,'cli'
2918
	db	0xFB,3,'sti'
2919
	db	0xFC,3,'cld'
2920
	db	0xFD,3,'std'
410 diamond 2921
csysenter:
2922
csyscall:
2923
ccpuid:
2924
crdtsc:
2925
	call	@f
2926
	db	0x05,7,'syscall'
2927
	db	0x31,5,'rdtsc'
2928
	db	0x34,8,'sysenter'
2929
	db	0xA2,5,'cpuid'
205 heavyiron 2930
@@:
2931
	pop	esi
2932
@@:
2933
	cmp	al, [esi]
2934
	jz	.found
2935
	inc	esi
2936
	movzx	edx, byte [esi]
2937
	inc	esi
2938
	add	esi, edx
2939
	jmp	@b
2940
.found:
2941
	inc	esi
2942
	lodsb
2943
	cmp	byte [esi], 0
2944
	jz	@f
2945
	movzx	ecx, al
2946
disasm_1:
2947
	rep	movsb
2948
	and	byte [edi], 0
2949
	ret
2950
@@:
2951
	mov	dl, ch
2952
	movzx	ecx, al
2953
	dec	ecx
2954
	inc	esi
2955
	rep	movsb
2956
	test	dl, 1
2957
	mov	al, 'w'
2958
	jnz	@f
2959
	mov	al, 'd'
2960
@@:	stosb
2961
	and	byte [edi], 0
2962
	ret
2963
 
2964
c67:
2965
	or	ch, 2
2966
	jmp	disasm_loop1
2967
c66:
2968
	or	ch, 1
2969
	jmp	disasm_loop1
2970
 
2971
center:
2972
caam:
2973
cxlat:
2974
ccmpxchg:
2975
cbsf:
2976
cbsr:
2977
ccmpxchg8b:
2978
cunk:
2979
cerr:
2980
	mov	eax, '???'
2981
	stosd
2982
	clc
2983
	ret
2984
 
2985
cF:
2986
	call	disasm_get_byte
2987
	jmp	dword [disasm_table_2 + eax*4]
2988
 
2989
macro disasm_set_modew
2990
{
2991
	test	al, 1
2992
	jz	@f
2993
	or	ch, 80h
2994
@@:
2995
}
2996
 
2997
cmov2:
2998
	disasm_set_modew
2999
; mov r/m,i
3000
	call	disasm_get_byte
3001
	dec	[disasm_cur_pos]
3002
	test	al, 00111000b
3003
	jnz	cunk
3004
	mov	eax, 'mov '
3005
	stosd
3006
	mov	eax, '    '
3007
	stosd
3008
	call	disasm_readrmop
3009
	mov	ax, ', '
3010
	stosw
3011
	xor	eax, eax
3012
	test	ch, 80h
3013
	jnz	.1
3014
	call	disasm_get_byte
3015
	jmp	.3
3016
.1:
3017
	test	ch, 1
3018
	jnz	.2
3019
	call	disasm_get_dword
3020
	jmp	.3
3021
.2:
3022
	call	disasm_get_word
3023
.3:
3024
	call	disasm_write_num
3025
	and	byte [edi], 0
3026
	ret
3027
 
3028
cret2:
3029
	mov	eax, 'ret '
3030
	stosd
3031
	mov	eax, '    '
3032
	stosd
3033
	xor	eax, eax
3034
	jmp	cmov2.2
3035
 
3036
disasm_write_num:
3037
	push	ecx eax
3038
	inc	edi
3039
@@:
3040
	mov	ecx, eax
3041
	shr	eax, 4
3042
	jz	@f
3043
	inc	edi
3044
	jmp	@b
3045
@@:
3046
	pop	eax
3047
	cmp	ecx, 10
3048
	jb	@f
3049
	inc	edi
3050
@@:
3051
	push	edi eax
3052
@@:
3053
	mov	ecx, eax
3054
	and	al, 0xF
3055
	cmp	al, 10
3056
	sbb	al, 69h
3057
	das
3058
	dec	edi
3059
	mov	[edi], al
3060
	mov	eax, ecx
3061
	shr	eax, 4
3062
	jnz	@b
3063
	cmp	ecx, 10
3064
	jb	@f
3065
	mov	byte [edi-1], '0'
3066
@@:
3067
	pop	eax edi ecx
3068
	cmp	eax, 10
3069
	jb	@f
3070
	mov	byte [edi], 'h'
3071
	inc	edi
3072
@@:
3073
	ret
3074
 
3075
label disasm_regs32 dword
3076
label disasm_regs dword
3077
	db	'eax',0
3078
	db	'ecx',0
3079
	db	'edx',0
3080
	db	'ebx',0
3081
	db	'esp',0
3082
	db	'ebp',0
3083
	db	'esi',0
3084
	db	'edi',0
3085
disasm_regs16	dw	'ax','cx','dx','bx','sp','bp','si','di'
3086
disasm_regs8	dw	'al','cl','dl','bl','ah','ch','dh','bh'
3087
disasm_scale	db	'1248'
3088
disasm_readrmop:
3089
	call	disasm_get_byte
3090
	test	ch, 40h
3091
	jnz	.skip_size
3092
	push	eax
3093
	and	al, 0xC0
3094
	cmp	al, 0xC0
3095
	pop	eax
3096
	jz	.skip_size
3097
	test	ch, 80h
3098
	jz	.byte
3099
	test	ch, 1
3100
	jnz	.word
3101
	mov	dword [edi], 'dwor'
3102
	mov	byte [edi+4], 'd'
3103
	inc	edi
3104
	jmp	@f
3105
.byte:
3106
	test	ch, 20h
3107
	jz	.qb
3108
	mov	byte [edi], 't'
3109
	inc	edi
3110
.qb:
3111
	mov	dword [edi], 'byte'
3112
	jmp	@f
3113
.word:
3114
	test	ch, 20h
3115
	jz	.qw
3116
	mov	byte [edi], 'q'
3117
	inc	edi
3118
.qw:
3119
	mov	dword [edi], 'word'
3120
@@:
3121
	mov	byte [edi+4], ' '
3122
	add	edi, 5
3123
.skip_size:
3124
	test	ch, 2
3125
	jnz	disasm_readrmop16
3126
	push	ecx
3127
	movzx	ecx, al
3128
	and	eax, 7
3129
	shr	ecx, 6
3130
	jz	.vmod0
3131
	jp	.vmod3
3132
	mov	byte [edi], '['
3133
	inc	edi
3134
	cmp	al, 4
3135
	jz	.sib1
3136
	mov	eax, [disasm_regs+eax*4]
3137
	stosd
3138
	dec	edi
3139
	jmp	@f
3140
.sib1:
3141
	call	.parse_sib
3142
@@:
3143
	mov	al, '+'
3144
	stosb
3145
	dec	ecx
3146
	jz	.vmod1
3147
	call	disasm_get_dword
3148
	jmp	@f
3149
.vmod1:
3150
	call	disasm_get_byte
3151
	movsx	eax, al
3152
@@:
3153
	test	eax, eax
3154
	jns	.2
3155
	neg	eax
3156
	mov	byte [edi-1], '-'
3157
.2:
3158
	call	disasm_write_num
3159
	mov	al, ']'
3160
	stosb
3161
	pop	ecx
3162
	ret
3163
.vmod3:
3164
	pop	ecx
3165
	test	ch, 80h
3166
	jz	.vmod3_byte
3167
	test	ch, 1
3168
	jnz	.vmod3_word
3169
	test	ch, 20h
3170
	jnz	.vmod3_sti
3171
	mov	eax, [disasm_regs32+eax*4]
3172
	stosd
3173
	dec	edi
3174
	ret
3175
.vmod3_byte:
3176
	mov	ax, [disasm_regs8+eax*2]
3177
@@:
3178
	stosw
3179
	ret
3180
.vmod3_word:
3181
	mov	ax, [disasm_regs16+eax*2]
3182
	jmp	@b
3183
.vmod3_sti:
3184
	mov	word [edi], 'st'
3185
	add	al, '0'
3186
	mov	byte [edi+2], al
3187
	add	edi, 3
3188
	ret
3189
.vmod0:
3190
	mov	byte [edi], '['
3191
	inc	edi
3192
	cmp	al, 4
3193
	jz	.sib2
3194
	cmp	al, 5
3195
	jz	.ofs32
3196
	mov	eax, [disasm_regs+eax*4]
3197
	stosd
3198
	mov	byte [edi-1], ']'
3199
	pop	ecx
3200
	ret
3201
.ofs32:
3202
	call	disasm_get_dword
3203
	jmp	.2
3204
.sib2:
3205
	call	.parse_sib
3206
	mov	al, ']'
3207
	stosb
3208
	pop	ecx
3209
	ret
3210
.parse_sib:
3211
	call	disasm_get_byte
3212
	push	edx
3213
	mov	dl, al
3214
	mov	dh, 0
3215
	and	eax, 7
3216
	cmp	al, 5
3217
	jnz	@f
3218
	jecxz	.sib0
3219
@@:
3220
	mov	eax, [disasm_regs+eax*4]
3221
	stosd
3222
	dec	edi
3223
	mov	dh, 1
3224
.sib0:
3225
	mov	al, dl
3226
	shr	eax, 3
3227
	and	eax, 7
3228
	cmp	al, 4
3229
	jz	.sibret
3230
	test	dh, dh
3231
	jz	@f
3232
	mov	byte [edi], '+'
3233
	inc	edi
3234
@@:
3235
	mov	eax, [disasm_regs+eax*4]
3236
	stosd
3237
	dec	edi
3238
	shr	dl, 6
3239
	jz	@f
3240
	mov	al, '*'
3241
	stosb
3242
	movzx	eax, dl
3243
	mov	al, [disasm_scale+eax]
3244
	stosb
3245
@@:
3246
.sibret:
3247
	test	dh, dh
3248
	jnz	.sibret2
3249
	call	disasm_get_dword
3250
	cmp	byte [edi-1], '['
3251
	jz	@f
3252
	mov	byte [edi], '+'
3253
	test	eax, eax
3254
	jns	.sibns
3255
	neg	eax
3256
	mov	byte [edi], '-'
3257
.sibns:
3258
	inc	edi
3259
@@:
3260
	call	disasm_write_num
3261
.sibret2:
3262
	pop	edx
3263
	ret
3264
 
3265
disasm_rm16_1	dd	'bxsi','bxdi','bpsi','bpdi'
3266
disasm_rm16_2	dw	'si','di','bp','bx'
3267
disasm_readrmop16:
3268
	push	ecx
3269
	movzx	ecx, al
3270
	and	eax, 7
3271
	shr	ecx, 6
3272
	jz	.vmod0
3273
	jp	disasm_readrmop.vmod3	; mod=3 is the same in 16- and 32-bit code
3274
; 1 or 2
3275
	mov	byte [edi], '['
3276
	inc	edi
3277
	cmp	al, 4
3278
	jae	@f
3279
	mov	eax, [disasm_rm16_1+eax*4]
3280
	stosw
3281
	mov	al, '+'
3282
	stosb
3283
	shr	eax, 16
3284
	jmp	.1
3285
@@:
3286
	mov	eax, dword [disasm_rm16_2+eax*2-4*2]
3287
.1:
3288
	stosw
3289
	mov	al, '+'
3290
	stosb
3291
	xor	eax, eax
3292
	dec	ecx
3293
	jnz	.2
3294
	call	disasm_get_byte
3295
	cbw
3296
	jmp	@f
3297
.2:
3298
	call	disasm_get_word
3299
@@:
3300
	test	ax, ax
3301
	jns	@f
3302
	mov	byte [edi-1], '-'
3303
	neg	ax
3304
@@:
3305
	call	disasm_write_num
3306
.done1:
3307
	mov	al, ']'
3308
	stosb
3309
	pop	ecx
3310
	ret
3311
.vmod0:
3312
	mov	byte [edi], '['
3313
	inc	edi
3314
	cmp	al, 6
3315
	jz	.ofs16
3316
	cmp	al, 4
3317
	jae	@f
3318
	mov	eax, [disasm_rm16_1+eax*4]
3319
	stosw
3320
	mov	al, '+'
3321
	stosb
3322
	shr	eax, 16
3323
	jmp	.3
3324
@@:
3325
	mov	eax, dword [disasm_rm16_2+eax*2-4*2]
3326
.3:
3327
	stosw
3328
	jmp	.done1
3329
.ofs16:
3330
	xor	eax, eax
3331
	call	disasm_get_word
3332
	call	disasm_write_num
3333
	jmp	.done1
3334
 
3335
cpush21:
3336
	mov	eax, 'push'
3337
	stosd
3338
	mov	eax, '    '
3339
	stosd
3340
disasm_i32:
3341
	call	disasm_get_dword
3342
	call	disasm_write_num
3343
	and	byte [edi], 0
3344
	ret
3345
 
3346
cpush22:
3347
	mov	eax, 'push'
3348
	stosd
3349
	mov	eax, '    '
3350
	stosd
3351
	call	disasm_get_byte
3352
	movsx	eax, al
3353
	call	disasm_write_num
3354
	and	byte [edi], 0
3355
	ret
3356
 
3357
cinc1:
3358
; inc reg32
3359
cdec1:
3360
; dec reg32
3361
cpush1:
3362
; push reg32
3363
cpop1:
3364
; pop reg32
3365
cbswap:
3366
; bswap reg32
3367
	mov	edx, eax
3368
	and	edx, 7
3369
	shr	eax, 3
3370
	sub	al, 8
3371
	mov	esi, 'inc '
3372
	jz	@f
3373
	mov	esi, 'dec '
3374
	dec	al
3375
	jz	@f
3376
	mov	esi, 'push'
3377
	dec	al
3378
	jz	@f
3379
	mov	esi, 'pop '
3380
	dec	al
3381
	jz	@f
3382
	mov	esi, 'bswa'
3383
@@:
3384
	xchg	eax, esi
3385
	stosd
3386
	mov	eax, '    '
3387
	jz	@f
3388
	mov	al, 'p'
3389
@@:
3390
	stosd
3391
	xchg	eax, edx
3392
	call	disasm_write_reg1632
3393
	and	byte [edi], 0
3394
	ret
3395
 
3396
cxchg1:
3397
; xchg eax,reg32
3398
	and	eax, 7
3399
	xchg	eax, edx
3400
	mov	eax, 'xchg'
3401
	stosd
3402
	mov	eax, '    '
3403
	stosd
3404
	xor	eax, eax
3405
	call	disasm_write_reg1632
3406
	mov	ax, ', '
3407
	stosw
3408
	xchg	eax, edx
3409
	call	disasm_write_reg1632
3410
	and	byte [edi], 0
3411
	ret
3412
 
3413
cint:
3414
	mov	eax, 'int '
3415
	stosd
3416
	mov	eax, '    '
3417
	stosd
3418
disasm_i8u:
3419
	xor	eax, eax
3420
	call	disasm_get_byte
3421
	call	disasm_write_num
3422
	and	byte [edi], 0
3423
	ret
3424
 
3425
cmov11:
3426
; mov r8,i8
3427
	mov	ecx, eax
3428
	mov	eax, 'mov '
3429
	stosd
3430
	mov	eax, '    '
3431
	stosd
3432
	and	ecx, 7
3433
	mov	ax, [disasm_regs8+ecx*2]
3434
	stosw
3435
	mov	ax, ', '
3436
	stosw
3437
	jmp	disasm_i8u
3438
 
3439
cmov12:
3440
; mov r32,i32
3441
	xchg	eax, edx
3442
	mov	eax, 'mov '
3443
	stosd
3444
	mov	eax, '    '
3445
	stosd
3446
	xchg	eax, edx
3447
	and	eax, 7
3448
	call	disasm_write_reg1632
3449
	mov	ax, ', '
3450
	stosw
3451
	jmp	cmov2.1
3452
 
3453
disasm_shifts	dd	'rol ','ror ','rcl ','rcr ','shl ','shr ','sal ','sar '
3454
cshift2:
3455
; shift r/m,1 = D0/D1
3456
cshift3:
3457
; shift r/m,cl = D2/D3
3458
	disasm_set_modew
3459
	mov	dl, al
3460
	call	disasm_get_byte
3461
	dec	[disasm_cur_pos]
3462
	shr	al, 3
3463
	and	eax, 7
3464
	mov	eax, [disasm_shifts+eax*4]
3465
	stosd
3466
	mov	eax, '    '
3467
	stosd
3468
	call	disasm_readrmop
3469
	cmp	dl, 0xD2
3470
	jb	.s1
3471
	mov	eax, ', cl'
3472
	stosd
3473
	and	byte [edi], 0
3474
	ret
3475
.s1:
3476
	mov	eax, ', 1'
3477
	stosd
3478
	clc
3479
	ret
3480
 
3481
cshift1:
3482
; shift r/m,i8 = C0/C1
3483
	disasm_set_modew
3484
	call	disasm_get_byte
3485
	dec	[disasm_cur_pos]
3486
	shr	al, 3
3487
	and	eax, 7
3488
	mov	eax, [disasm_shifts+eax*4]
3489
	stosd
3490
	mov	eax, '    '
3491
	stosd
3492
	call	disasm_readrmop
3493
	mov	ax, ', '
3494
	stosw
3495
	jmp	disasm_i8u
3496
 
3497
 
3498
cmov3:
3499
; A0: mov al,[ofs32]
3500
; A1: mov ax/eax,[ofs32]
3501
; A2: mov [ofs32],al
3502
; A3: mov [ofs32],ax/eax
3503
	mov	edx, 'mov '
3504
	xchg	eax, edx
3505
	stosd
3506
	mov	eax, '    '
3507
	stosd
3508
	test	dl, 2
3509
	jnz	.1
3510
	call	.write_acc
3511
	mov	ax, ', '
3512
	stosw
3513
	call	.write_ofs32
3514
	jmp	.2
3515
.1:
3516
	call	.write_ofs32
3517
	mov	ax, ', '
3518
	stosw
3519
	call	.write_acc
3520
.2:	and	byte [edi], 0
3521
	ret
3522
.write_acc:
3523
	test	dl, 1
3524
	jz	.8bit
3525
	test	ch, 1
3526
	jnz	.16bit
3527
	mov	eax, 'eax'
3528
	stosd
3529
	dec	edi
3530
	ret
3531
.16bit:
3532
	mov	ax, 'ax'
3533
	stosw
3534
	ret
3535
.8bit:
3536
	mov	ax, 'al'
3537
	stosw
3538
	ret
3539
.write_ofs32:
3540
	mov	al, '['
3541
	stosb
3542
	call	disasm_get_dword
3543
	call	disasm_write_num
3544
	mov	al, ']'
3545
	stosb
3546
	ret
3547
 
3548
disasm_write_reg:
3549
	test	ch, 80h
3550
	jnz	disasm_write_reg1632
3551
	mov	ax, [disasm_regs8+eax*2]
3552
	stosw
3553
	ret
3554
disasm_write_reg1632:
3555
	test	ch, 1
3556
	jnz	@f
3557
	mov	eax, [disasm_regs32+eax*4]
3558
	stosd
3559
	dec	edi
3560
	ret
3561
@@:
3562
	mov	ax, [disasm_regs16+eax*2]
3563
	stosw
3564
	ret
3565
 
3566
cmovzx:		; 0F B6/B7
3567
cmovsx:		; 0F BE/BF
3568
	mov	edx, eax
3569
	disasm_set_modew
3570
	mov	eax, 'movz'
3571
	cmp	dl, 0xB8
3572
	jb	@f
3573
	mov	eax, 'movs'
3574
@@:
3575
	stosd
3576
	mov	eax, 'x   '
3577
	stosd
3578
	call	disasm_get_byte
3579
	dec	[disasm_cur_pos]
3580
	shr	al, 3
3581
	and	eax, 7
3582
	call	disasm_write_reg1632
3583
	mov	ax, ', '
3584
	stosw
3585
	or	ch, 1	; 2nd operand - 8 or 16 bits
3586
	call	disasm_readrmop
3587
	and	byte [edi], 0
3588
	ret
3589
 
3590
disasm_op2cmds	dd 'add ','or  ','adc ','sbb ','and ','sub ','xor ','cmp '
3591
cop21:
3592
	disasm_set_modew
3593
	mov	esi, 'test'
3594
	cmp	al, 0A8h
3595
	jae	@f
3596
	shr	al, 3
3597
	and	eax, 7
3598
	mov	esi, [disasm_op2cmds+eax*4]
3599
@@:
3600
	xchg	eax, esi
3601
	stosd
3602
	mov	eax, '    '
3603
	stosd
3604
	test	ch, 80h
3605
	jnz	.1632
3606
	mov	eax, 'al, '
3607
	stosd
3608
	jmp	disasm_i8u
3609
.1632:
3610
	test	ch, 1
3611
	jnz	.16
3612
	mov	eax, 'eax,'
3613
	stosd
3614
	mov	al, ' '
3615
	stosb
3616
	call	disasm_get_dword
3617
	jmp	.x
3618
.16:
3619
	mov	eax, 'ax, '
3620
	stosd
3621
	xor	eax, eax
3622
	call	disasm_get_word
3623
.x:
3624
	call	disasm_write_num
3625
	and	byte [edi], 0
3626
	ret
3627
 
3628
cop22:
3629
	disasm_set_modew
3630
	or	ch, 40h
3631
	mov	edx, eax
3632
	mov	esi, 'lea '
3633
	cmp	al, 8Dh
3634
	jz	@f
3635
	mov	esi, 'imul'
3636
	cmp	al, 0xAF
3637
	jz	@f
3638
	mov	esi, 'mov '
3639
	cmp	al, 88h
3640
	jae	@f
3641
	mov	esi, 'xchg'
3642
	cmp	al, 86h
3643
	jae	@f
3644
	mov	esi, 'test'
3645
	cmp	al, 84h
3646
	jae	@f
3647
	shr	al, 3
3648
	and	eax, 7
3649
	mov	esi, [disasm_op2cmds+eax*4]
3650
@@:
3651
	xchg	eax, esi
3652
	stosd
3653
	mov	eax, '    '
3654
	stosd
3655
	call	disasm_get_byte
3656
	dec	[disasm_cur_pos]
3657
	shr	al, 3
3658
	and	eax, 7
3659
	cmp	dl, 0x8D
3660
	jz	@f
3661
	cmp	dl, 0x86
3662
	jz	@f
3663
	cmp	dl, 0x87
3664
	jz	@f
3665
	test	dl, 2
3666
	jz	.d0
3667
@@:
3668
	call	disasm_write_reg
3669
	mov	ax, ', '
3670
	stosw
3671
	call	disasm_readrmop
3672
	and	byte [edi], 0
3673
	ret
3674
.d0:
3675
	push	eax
3676
	call	disasm_readrmop
3677
	mov	ax, ', '
3678
	stosw
3679
	pop	eax
3680
	call	disasm_write_reg
3681
	and	byte [edi], 0
3682
	ret
3683
 
3684
cop23:
3685
	disasm_set_modew
3686
	xchg	eax, edx
3687
	call	disasm_get_byte
3688
	dec	[disasm_cur_pos]
3689
	shr	eax, 3
3690
	and	eax, 7
3691
	mov	eax, [disasm_op2cmds+eax*4]
3692
ctest:
3693
	stosd
3694
	mov	eax, '    '
3695
	stosd
3696
	call	disasm_readrmop
3697
	mov	ax, ', '
3698
	stosw
3699
	test	ch, 80h
3700
	jz	.i8
3701
	cmp	dl, 83h
3702
	jz	.i8
3703
	test	ch, 1
3704
	jnz	.i16
3705
	call	disasm_get_dword
3706
	jmp	.ic
3707
.i8:
3708
	xor	eax, eax
3709
	call	disasm_get_byte
3710
	cmp	dl, 83h
3711
	jnz	.ic
3712
	movsx	eax, al
3713
	jmp	.ic
3714
.i16:
3715
	xor	eax, eax
3716
	call	disasm_get_word
3717
.ic:
3718
	call	disasm_write_num
3719
	and	byte [edi], 0
3720
	ret
3721
 
3722
cbtx1:
3723
; btx r/m,i8 = 0F BA
3724
	or	ch, 80h
3725
	call	disasm_get_byte
3726
	dec	[disasm_cur_pos]
3727
	shr	al, 3
3728
	and	eax, 7
3729
	cmp	al, 4
3730
	jb	cunk
3731
	mov	eax, [btx1codes+eax*4-4*4]
3732
	stosd
3733
	mov	eax, '    '
3734
	stosd
3735
	call	disasm_readrmop
3736
	mov	ax, ', '
3737
	stosw
3738
	jmp	disasm_i8u
3739
btx1codes	dd	'bt  ','bts ','btr ','btc '
3740
cbtx2:
3741
; btx r/m,r = 0F 101xx011 (A3,AB,B3,BB)
3742
	shr	al, 3
3743
	and	eax, 3
3744
	mov	eax, [btx1codes+eax*4]
3745
	stosd
3746
	mov	eax, '    '
3747
	stosd
3748
	or	ch, 0xC0
3749
	call	disasm_get_byte
3750
	dec	[disasm_cur_pos]
3751
	shr	al, 3
3752
	and	eax, 7
3753
	push	eax
3754
	call	disasm_readrmop
3755
	mov	ax, ', '
3756
	stosw
3757
	pop	eax
3758
	call	disasm_write_reg1632
3759
	and	byte [edi], 0
3760
	ret
3761
 
3762
csetcc:
3763
	and	eax, 0xF
3764
	mov	ax, [disasm_jcc_codes + eax*2]
3765
	mov	dword [edi], 'setc'
3766
	add	edi, 3
3767
	stosw
3768
	mov	ax, '  '
3769
	stosw
3770
	stosb
3771
	call	disasm_readrmop
3772
	and	byte [edi], 0
3773
	ret
3774
 
3775
disasm_jcc_codes dw 'o ','no','b ','ae','z ','nz','be','a ','s ','ns','p ','np','l ','ge','le','g '
3776
cjcc1:
3777
cjmp2:
3778
	cmp	al, 0xEB
3779
	jz	.1
3780
	and	eax, 0xF
3781
	mov	ax, [disasm_jcc_codes + eax*2]
3782
	jmp	.2
3783
.1:
3784
	mov	ax, 'mp'
3785
.2:
3786
	mov	byte [edi], 'j'
3787
	inc	edi
3788
	stosw
3789
	mov	eax, '    '
3790
	stosb
3791
	stosd
3792
	call	disasm_get_byte
3793
	movsx	eax, al
3794
disasm_rva:
3795
	add	eax, [disasm_cur_pos]
3796
	call	disasm_write_num
3797
	and	byte [edi], 0
3798
	ret
3799
 
3800
ccall1:
3801
cjmp1:
3802
cjcc2:
3803
	mov	edx, 'call'
3804
	cmp	al, 0xE8
3805
	jz	@f
3806
	mov	edx, 'jmp '
3807
	cmp	al, 0xE9
3808
	jz	@f
3809
	mov	edx, '    '
3810
	and	eax, 0xF
3811
	mov	dx, [disasm_jcc_codes+eax*2]
3812
	shl	edx, 8
3813
	mov	dl, 'j'
3814
@@:
3815
	xchg	eax, edx
3816
	stosd
3817
	mov	eax, '    '
3818
	stosd
3819
	call	disasm_get_dword
3820
	jmp	disasm_rva
3821
 
3822
op11codes	dd	'test',0,'not ','neg ','mul ','imul','div ','idiv'
3823
op12codes	dd	'inc ','dec ','call',0,'jmp ',0,'push',0
3824
cop1:
3825
	disasm_set_modew
3826
	xchg	eax, edx
3827
	call	disasm_get_byte
3828
	dec	[disasm_cur_pos]
3829
	shr	al, 3
3830
	and	eax, 7
3831
	cmp	dl, 0xFE
3832
	jnz	@f
3833
	cmp	al, 1
3834
	ja	cunk
3835
@@:
3836
	and	edx, 8
3837
	add	eax, edx
3838
	mov	eax, [op11codes+eax*4]
3839
	test	eax, eax
3840
	jz	cunk
3841
	cmp	eax, 'test'
3842
	jz	ctest
3843
@@:
3844
	stosd
3845
	mov	eax, '    '
3846
	stosd
3847
	call	disasm_readrmop
3848
	and	byte [edi], 0
3849
	ret
3850
 
3851
cpop2:
3852
	or	ch, 80h
3853
	call	disasm_get_byte
3854
	dec	[disasm_cur_pos]
3855
	test	al, 00111000b
3856
	jnz	cunk
3857
	mov	eax, 'pop '
3858
	jmp	@b
3859
 
3860
cloopnz:
3861
	mov	eax, 'loop'
3862
	stosd
3863
	mov	eax, 'nz  '
3864
	test	ch, 2
3865
	jz	@f
3866
	mov	ah, 'w'
3867
@@:	jmp	cloop.cmn
3868
cloopz:
3869
	mov	eax, 'loop'
3870
	stosd
3871
	mov	eax, 'z   '
3872
	test	ch, 2
3873
	jz	@f
3874
	mov	eax, 'zw  '
3875
@@:	jmp	cloop.cmn
3876
 
3877
cjcxz:
3878
cloop:
3879
	cmp	al, 0xE2
3880
	jz	.loop
3881
	test	ch, 2
3882
	jnz	.jcxz
3883
	mov	eax, 'jecx'
3884
	stosd
3885
	mov	eax, 'z   '
3886
	jmp	.cmn
3887
.jcxz:
3888
	mov	eax, 'jcxz'
3889
	stosd
3890
	mov	eax, '    '
3891
	jmp	.cmn
3892
.loop:
3893
	mov	eax, 'loop'
3894
	stosd
3895
	mov	eax, '    '
3896
	test	ch, 2
3897
	jz	.cmn
3898
	mov	al, 'w'
3899
.cmn:
3900
	stosd
3901
	call	disasm_get_byte
3902
	movsx	eax, al
3903
	add	eax, [disasm_cur_pos]
3904
	test	ch, 1
3905
	jz	@f
3906
	and	eax, 0xFFFF
3907
@@:
3908
disasm_write_num_done:
3909
	call	disasm_write_num
3910
	and	byte [edi], 0
3911
	ret
3912
 
3913
cimul1:
3914
; imul r,r/m,i
3915
	or	ch, 80h		; 32bit operation
3916
	xchg	eax, edx
3917
	mov	eax, 'imul'
3918
	stosd
3919
	mov	eax, '    '
3920
	stosd
3921
	call	disasm_get_byte
3922
	dec	[disasm_cur_pos]
3923
	shr	al, 3
3924
	and	eax, 7
3925
	call	disasm_write_reg1632
3926
	mov	ax, ', '
3927
	stosw
3928
	call	disasm_readrmop
3929
	mov	ax, ', '
3930
	stosw
3931
	test	ch, 1
3932
	jnz	.16
3933
	cmp	dl, 0x69
3934
	jz	.op32
3935
	call	disasm_get_byte
3936
	movsx	eax, al
3937
	jmp	disasm_write_num_done
3938
.op32:
3939
	call	disasm_get_dword
3940
	jmp	disasm_write_num_done
3941
.16:
3942
	cmp	dl, 0x69
3943
	jz	.op16
3944
	call	disasm_get_byte
3945
	cbw
3946
	jmp	disasm_write_num_done
3947
.op16:
3948
	xor	eax, eax
3949
	call	disasm_get_word
3950
	jmp	disasm_write_num_done
3951
 
3952
cshld:
3953
cshrd:
3954
	mov	edx, 'shld'
3955
	test	al, 8
3956
	jz	@f
3957
	mov	edx, 'shrd'
3958
@@:
3959
	xchg	eax, edx
3960
	stosd
3961
	mov	eax, '    '
3962
	stosd
3963
	call	disasm_get_byte
3964
	dec	[disasm_cur_pos]
3965
	shr	al, 3
3966
	and	eax, 7
3967
	push	eax
3968
	or	ch, 80h
3969
	call	disasm_readrmop
3970
	mov	ax, ', '
3971
	stosw
3972
	pop	eax
3973
	call	disasm_write_reg1632
3974
	mov	ax, ', '
3975
	stosw
3976
	test	dl, 1
3977
	jz	disasm_i8u
3978
	mov	ax, 'cl'
3979
	stosw
3980
	and	byte [edi], 0
3981
	ret
3982
 
3983
ccbw:
3984
	mov	eax, 'cbw '
3985
	test	ch, 1
3986
	jnz	@f
3987
	mov	eax, 'cwde'
3988
@@:	stosd
3989
	and	byte [edi+1], 0
3990
	ret
3991
ccwd:
3992
	mov	eax, 'cwd '
3993
	test	ch, 1
3994
	jnz	@b
3995
	mov	eax, 'cdq '
3996
	jmp	@b
3997
 
3998
fpuD8	dd	'add ','mul ','com ','comp','sub ','subr','div ','divr'
3999
 
4000
cD8:
4001
	call	disasm_get_byte
4002
	dec	[disasm_cur_pos]
4003
	push	eax
4004
	shr	al, 3
4005
	and	eax, 7
4006
	mov	byte [edi], 'f'
4007
	inc	edi
4008
	xchg	eax, edx
4009
	mov	eax, [fpuD8+edx*4]
4010
	stosd
4011
	mov	ax, '  '
4012
	stosw
4013
	stosb
4014
	pop	eax
4015
	cmp	dl, 2
4016
	jb	.1
4017
	cmp	dl, 3
4018
	jbe	.2
4019
.1:
4020
	cmp	al, 0xC0
4021
	jb	.2
4022
	mov	eax, 'st0,'
4023
	stosd
4024
	mov	al, ' '
4025
	stosb
4026
.2:
4027
	or	ch, 80h or 20h
4028
	and	ch, not 1
4029
	call	disasm_readrmop
4030
	and	byte [edi], 0
4031
	ret
4032
 
4033
fpuD9_2:
4034
	dq	'fchs    ','fabs    ',0,0,'ftst    ','fxam    ',0,0
4035
	db	'fld1    fldl2t  fldl2e  fldpi   fldlg2  fldln2  fldz    '
4036
	dq	0
4037
	db	'f2xm1   fyl2x   fptan   fpatan  fxtract fprem1  fdecstp fincstp '
4038
	db	'fprem   fyl2xp1 fsqrt   fsincos frndint fscale  fsin    fcos    '
4039
fpuD9_fnop	db	'fnop    '
4040
cD9:
4041
	call	disasm_get_byte
4042
	sub	al, 0xC0
4043
	jae	.l1
4044
	dec	[disasm_cur_pos]
4045
	shr	al, 3
4046
	and	eax, 7
4047
	cmp	al, 7
4048
	jnz	@f
4049
	mov	eax, 'fnst'
4050
	stosd
4051
	mov	eax, 'cw  '
4052
	jmp	.x1
4053
@@:
4054
	cmp	al, 5
4055
	jnz	@f
4056
	mov	eax, 'fldc'
4057
	stosd
4058
	mov	eax, 'w   '
4059
.x1:
4060
	stosd
4061
	or	ch, 0C1h
4062
	jmp	.cmn
4063
@@:
4064
	mov	edx, 'fld '
4065
	test	al, al
4066
	jz	@f
4067
	mov	edx, 'fst '
4068
	cmp	al, 2
4069
	jz	@f
4070
	mov	edx, 'fstp'
4071
	cmp	al, 3
4072
	jnz	cunk
4073
@@:
4074
	xchg	eax, edx
4075
	stosd
4076
	mov	eax, '    '
4077
	stosd
4078
	or	ch, 80h
4079
	and	ch, not 1
4080
.cmn:
4081
	call	disasm_readrmop
4082
	and	byte [edi], 0
4083
	ret
4084
.l1:
4085
	cmp	al, 10h
4086
	jae	.l2
4087
	mov	edx, 'fld '
4088
	cmp	al, 8
4089
	jb	@f
4090
	mov	edx, 'fxch'
4091
@@:
4092
	xchg	eax, edx
4093
	stosd
4094
	mov	eax, '    '
4095
	stosd
4096
	xchg	eax, edx
4097
	and	al, 7
4098
	add	al, '0'
4099
	shl	eax, 16
4100
	mov	ax, 'st'
4101
	stosd
4102
	clc
4103
	ret
4104
.l2:
4105
	cmp	al, 0x10
4106
	jnz	@f
4107
	mov	esi, fpuD9_fnop
4108
	jmp	.l3
4109
@@:
4110
	sub	al, 0x20
4111
	jb	cerr
4112
	lea	esi, [fpuD9_2+eax*8]
4113
	cmp	byte [esi], 0
4114
	jz	cerr
4115
.l3:
4116
	movsd
4117
	movsd
4118
	and	byte [edi-1], 0
4119
	ret
4120
 
4121
cDA:
4122
	call	disasm_get_byte
4123
	cmp	al, 0xC0
4124
	jae	cunk
4125
	dec	[disasm_cur_pos]
4126
	shr	al, 3
4127
	and	eax, 7
4128
	mov	word [edi], 'fi'
4129
	inc	edi
4130
	inc	edi
4131
	mov	eax, [fpuD8+eax*4]
4132
	stosd
4133
	mov	ax, '  '
4134
	stosw
4135
	or	ch, 80h
4136
	and	ch, not 1	; 32-bit operand
4137
	call	disasm_readrmop
4138
	and	byte [edi], 0
4139
	ret
4140
 
4141
fpuDB	dd	'ild ',0,'ist ','istp',0,'ld  ',0,'stp '
4142
cDB:
4143
	call	disasm_get_byte
4144
	cmp	al, 0xC0
4145
	jae	.1
4146
	dec	[disasm_cur_pos]
4147
	shr	al, 3
4148
	and	eax, 7
4149
	xchg	eax, edx
4150
	mov	eax, [fpuDB+edx*4]
4151
	test	eax, eax
4152
	jz	cerr
4153
	mov	byte [edi], 'f'
4154
	inc	edi
4155
	stosd
4156
	mov	ax, '  '
4157
	stosw
4158
	stosb
4159
	or	ch, 80h
4160
	and	ch, not 1	; 32-bit operand
4161
	cmp	dl, 4
4162
	jb	@f
4163
	or	ch, 20h
4164
	and	ch, not 80h	; 80-bit operand
4165
@@:
4166
	call	disasm_readrmop
4167
	and	byte [edi], 0
4168
	ret
4169
.1:
4170
	cmp	al, 0xE3
4171
	jnz	cunk
4172
	mov	eax, 'fnin'
4173
	stosd
4174
	mov	eax, 'it'
4175
	stosd
4176
	dec	edi
4177
	ret		; CF cleared
4178
 
4179
fpuDC	dd	'add ','mul ',0,0,'subr','sub ','divr','div '
4180
cDC:
4181
	call	disasm_get_byte
4182
	cmp	al, 0xC0
4183
	jae	.1
4184
	dec	[disasm_cur_pos]
4185
	shr	al, 3
4186
	and	eax, 7
4187
	mov	byte [edi], 'f'
4188
	inc	edi
4189
	mov	eax, [fpuD8+eax*4]
4190
	stosd
4191
	mov	ax, '  '
4192
	stosw
4193
	stosb
4194
	or	ch, 0A1h	; qword
4195
	call	disasm_readrmop
4196
	and	byte [edi], 0
4197
	ret
4198
.1:
4199
	mov	dl, al
4200
	shr	al, 3
4201
	and	eax, 7
4202
	mov	eax, [fpuDC+eax*4]
4203
	test	eax, eax
4204
	jz	cerr
4205
	mov	byte [edi], 'f'
4206
	inc	edi
4207
	stosd
4208
	mov	eax, '   s'
4209
	stosd
4210
	mov	al, 't'
4211
	stosb
4212
	and	edx, 7
4213
	lea	eax, [edx+'0']
4214
	stosb
4215
	mov	eax, ', st'
4216
	stosd
4217
	mov	ax, '0'
4218
	stosw
4219
	ret	; CF cleared
4220
 
4221
fpuDD	dd	'fld ',0,'fst ','fstp',0,0,0,0
4222
fpuDD_2	dq	'ffree   ',0,'fst     ','fstp    ','fucom   ','fucomp  ',0,0
4223
cDD:
4224
	call	disasm_get_byte
4225
	cmp	al, 0xC0
4226
	jae	.1
4227
	dec	[disasm_cur_pos]
4228
	shr	al, 3
4229
	and	eax, 7
4230
	xchg	eax, edx
4231
	mov	eax, [fpuDD+edx*4]
4232
	test	eax, eax
4233
	jz	cunk
4234
	stosd
4235
	mov	eax, '    '
4236
	stosd
4237
	or	ch, 0A1h	; qword operand
4238
	call	disasm_readrmop
4239
	and	byte [edi], 0
4240
	ret
4241
.1:
4242
	push	eax
4243
	shr	al, 3
4244
	and	eax, 7
4245
	xchg	eax, edx
4246
	mov	eax, dword [fpuDD_2+edx*8]
4247
	test	eax, eax
4248
	jz	cerr
4249
	stosd
4250
	mov	eax, dword [fpuDD_2+4+edx*8]
4251
	stosd
4252
	mov	ax, 'st'
4253
	stosw
4254
	pop	eax
4255
	and	al, 7
4256
	add	al, '0'
4257
	stosb
4258
	and	byte [edi], 0
4259
	ret
4260
 
4261
fpuDE	dd	'add ','mul ',0,0,'subr','sub ','divr','div '
4262
cDE:
4263
	call	disasm_get_byte
4264
	cmp	al, 0xC0
4265
	jae	.1
4266
	dec	[disasm_cur_pos]
4267
	mov	word [edi], 'fi'
4268
	inc	edi
4269
	inc	edi
4270
	shr	al, 3
4271
	and	eax, 7
4272
	mov	eax, [fpuD8+eax*4]
4273
	stosd
4274
	mov	ax, '  '
4275
	stosw
4276
	or	ch, 81h		; force 16-bit
4277
	call	disasm_readrmop
4278
	and	byte [edi], 0
4279
	ret
4280
.1:
4281
	push	eax
4282
	shr	al, 3
4283
	and	eax, 7
4284
	xchg	eax, edx
4285
	mov	eax, [fpuDE+edx*4]
4286
	test	eax, eax
4287
	jz	.fcompp
4288
	mov	byte [edi], 'f'
4289
	inc	edi
4290
	stosd
4291
	mov	al, 'p'
4292
	cmp	byte [edi-1], ' '
4293
	jnz	@f
4294
	mov	byte [edi-1], al
4295
	mov	al, ' '
4296
@@:	stosb
4297
	mov	eax, '  st'
4298
	stosd
4299
	pop	eax
4300
	and	al, 7
4301
	add	al, '0'
4302
	stosb
4303
	mov	ax, ', '
4304
	stosw
4305
	mov	eax, 'st0'
4306
	stosd
4307
	ret	; CF cleared
4308
.fcompp:
4309
	pop	eax
4310
	cmp	al, 0xD9
4311
	jnz	cerr
4312
	mov	eax, 'fcom'
4313
	stosd
4314
	mov	ax, 'pp'
4315
	stosw
4316
	and	byte [edi], 0
4317
	ret
4318
 
4319
fpuDF	dd	'ild ',0,'ist ','istp','bld ','ild ','bstp','istp'
4320
 
4321
cDF:
4322
	call	disasm_get_byte
4323
	cmp	al, 0xC0
4324
	jae	.1
4325
	dec	[disasm_cur_pos]
4326
	shr	al, 3
4327
	and	eax, 7
4328
	xchg	eax, edx
4329
	mov	eax, [fpuDF+edx*4]
4330
	test	eax, eax
4331
	jz	cerr
4332
	mov	byte [edi], 'f'
4333
	inc	edi
4334
	stosd
4335
	mov	ax, '  '
4336
	stosw
4337
	stosb
4338
	or	ch, 81h		; force 16-bit operand
4339
	cmp	dl, 4
4340
	jb	@f
4341
	or	ch, 20h
4342
	test	dl, 1
4343
	jnz	@f
4344
	or	ch, 40h
4345
@@:
4346
	call	disasm_readrmop
4347
	and	byte [edi], 0
4348
	ret
4349
.1:
4350
	cmp	al, 0xE0
4351
	jnz	cunk
4352
	mov	eax, 'fnst'
4353
	stosd
4354
	mov	eax, 'sw  '
4355
	stosd
4356
	mov	ax, 'ax'
4357
	stosw
4358
	and	byte [edi], 0
4359
	ret
4360
 
4361
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4362
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4363
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4364
 
4365
caption_str db 'Kolibri Debugger',0
4366
caption_len = $ - caption_str
4367
begin_str db	'Kolibri Debugger, version 0.2',10
4368
	db	'Hint: type "help" for help, "quit" for quit'
4369
newline	db	10,0
4370
prompt	db	'> ',0
4371
 
4372
help_groups:
4373
	dd	aControl, 0, 0, help_control_msg
4374
	db	0
4375
	dd	aData, 0, 0, help_data_msg
4376
	db	0
4377
	dd	aBreakpoints, 0, 0, help_breaks_msg
4378
	db	0
4379
; flags field:
4380
; &1: command may be called without parameters
4381
; &2: command may be called with parameters
4382
; &4: command may be called without loaded program
4383
; &8: command may be called with loaded program
4384
commands:
4385
	dd	_aH, OnHelp, HelpSyntax, HelpHelp
4386
	db	0Fh
4387
	dd	aHelp, OnHelp, HelpSyntax, HelpHelp
4388
	db	0Fh
4389
	dd	aQuit, OnQuit, QuitSyntax, QuitHelp
4390
	db	0Dh
4391
	dd	aLoad, OnLoad, LoadSyntax, LoadHelp
4392
	db	6
4393
	dd	aReload, OnReload, ReloadSyntax, ReloadHelp
4394
	db	0Dh
4395
	dd	aTerminate, OnTerminate, TerminateSyntax, TerminateHelp
4396
	db	9
4397
	dd	aDetach, OnDetach, DetachSyntax, DetachHelp
4398
	db	9
4399
	dd	aSuspend, OnSuspend, SuspendSyntax, SuspendHelp
4400
	db	9
4401
	dd	aResume, OnResume, ResumeSyntax, ResumeHelp
4402
	db	0Bh
4403
	dd	aStep, OnStep, StepSyntax, StepHelp
4404
	db	9
4405
	dd	aProceed, OnProceed, ProceedSyntax, ProceedHelp
4406
	db	9
4407
	dd	aCalc, OnCalc, CalcSyntax, CalcHelp
4408
	db	0Eh
4409
	dd	aDump, OnDump, DumpSyntax, DumpHelp
4410
	db	0Bh
4411
	dd	aUnassemble, OnUnassemble, UnassembleSyntax, UnassembleHelp
4412
	db	0Bh
4413
	dd	aBp, OnBp, BpSyntax, BpHelp
4414
	db	0Ah
4415
	dd	aBpm, OnBpmb, BpmSyntax, BpmHelp
4416
	db	0Ah
4417
	dd	aBpmb, OnBpmb, BpmSyntax, BpmHelp
4418
	db	0Ah
4419
	dd	aBpmw, OnBpmw, BpmSyntax, BpmHelp
4420
	db	0Ah
4421
	dd	aBpmd, OnBpmd, BpmSyntax, BpmHelp
4422
	db	0Ah
4423
	dd	aBl, OnBl, BlSyntax, BlHelp
4424
	db	0Bh
4425
	dd	aBc, OnBc, BcSyntax, BcHelp
4426
	db	0Ah
4427
	dd	aBd, OnBd, BdSyntax, BdHelp
4428
	db	0Ah
4429
	dd	aBe, OnBe, BeSyntax, BeHelp
4430
	db	0Ah
4431
	dd	aReg, OnReg, RSyntax, RHelp
4432
	db	0Ah
4433
	dd	aUnpack, OnUnpack, UnpackSyntax, UnpackHelp
4434
	db	9
4435
	dd	0
4436
aHelp	db	5,'help',0
4437
_aH	db	2,'h',0
4438
HelpHelp db	'Help on specified function',10
4439
HelpSyntax db	'Usage: h or help [group | command]',10,0
4440
 
4441
help_msg db	'List of known command groups:',10
4442
	db	'"help control"     - display list of control commands',10
4443
	db	'"help data"        - display list of commands concerning data',10
4444
	db	'"help breakpoints" - display list of commands concerning breakpoints',10,0
4445
aControl db	8,'control',0
4446
help_control_msg db	'List of control commands:',10
4447
	db	'h = help             - help',10
4448
	db	'quit                 - exit from debugger',10
4449
	db	'load  [params] - load program for debugging',10
4450
	db	'reload               - reload debugging program',10
4451
	db	'terminate            - terminate loaded program',10
4452
	db	'detach               - detach from debugging program',10
4453
	db	'stop                 - suspend execution of debugging program',10
4454
	db	'g []     - go on (resume execution of debugging program)',10
4455
	db	's =         - program step',10
4456
	db	'p =         - program wide step',10
4457
	db	'unpack               - try to bypass unpacker code (heuristic)',10,0
4458
aData	db	5,'data',0
4459
help_data_msg db	'List of data commands:',10
4460
	db	'?        - calculate value of expression',10
4461
	db	'd []     - dump data at given address',10
4462
	db	'u []     - unassemble instructions at given address',10
4463
	db	'r   or',10
4464
	db	'r = - set register value',10,0
4465
aBreakpoints db 12,'breakpoints',0
4466
help_breaks_msg db	'List of breakpoints commands:',10
4467
	db	'bp       - set breakpoint on execution',10
4468
	db	'bpm[b|w|d]   - set breakpoint on memory access',10
4469
	db	'bl []        - breakpoint(s) info',10
4470
	db	'bc ...       - clear breakpoint',10
4471
	db	'bd ...       - disable breakpoint',10
4472
	db	'be ...       - enable breakpoint',10,0
4473
 
4474
aQuit	db	5,'quit',0
4475
QuitHelp db	'Quit from debugger',10
4476
QuitSyntax db	'Usage: quit',10,0
4477
 
4478
aLoad	db	5,'load',0
4479
LoadHelp db	'Load program for debugging',10
4480
LoadSyntax db	'Usage: load  [parameters]',10,0
4481
 
4482
aReload db	7,'reload',0
4483
ReloadHelp db	'Reload debugging program (restart debug session)',10
4484
ReloadSyntax db	'Usage: reload',10,0
4485
 
4486
aTerminate db	10,'terminate',0
4487
TerminateHelp db 'Terminate debugged program',10
4488
TerminateSyntax db 'Usage: terminate',10,0
4489
 
4490
aDetach	db	7,'detach',0
4491
DetachHelp db	'Detach from debugged program',10
4492
DetachSyntax db	'Usage: detach',10,0
4493
 
4494
aSuspend db	5,'stop',0
4495
SuspendHelp db	'Suspend execution of debugged program',10
4496
SuspendSyntax db 'Usage: stop',10,0
4497
 
4498
aResume db	2,'g',0
4499
ResumeHelp db	'Go (resume execution of debugged program)',10
4500
ResumeSyntax db	'Usage: g',10
4501
	db	'   or: g  - wait until specified address is reached',10,0
4502
 
4503
aStep	db	2,'s',0
4504
StepHelp db	'Make step in debugged program',10
4505
StepSyntax db	'Usage: s',10,0
4506
 
4507
aProceed db	2,'p',0
4508
ProceedHelp db	'Make wide step in debugged program (step over CALL, REPxx, LOOP)',10
4509
ProceedSyntax db 'Usage: p',10,0
4510
 
4511
aDump	db	2,'d',0
4512
DumpHelp db	'Dump data of debugged program',10
4513
DumpSyntax db	'Usage: d  - dump data at specified address',10
4514
	db	'   or: d              - continue current dump',10,0
4515
 
4516
aCalc	db	2,'?',0
4517
CalcHelp db	'Calculate value of expression',10
4518
CalcSyntax db	'Usage: ? ',10,0
4519
 
4520
aUnassemble db	2,'u',0
4521
UnassembleHelp db 'Unassemble',10
4522
UnassembleSyntax:
4523
	db	'Usage: u  - unassemble instructions at specified address',10
4524
	db	'   or: u              - continue current unassemble screen',10,0
4525
 
4526
aReg	db	2,'r',0
4527
RHelp	db	'Set register value',10
4528
RSyntax:
4529
	db	'Usage: r  ',10
4530
	db	'   or: r = - set value of  to ',10,0
4531
 
4532
aBp	db	3,'bp',0
4533
BpHelp	db	'set BreakPoint on execution',10
4534
BpSyntax db	'Usage: bp ',10,0
4535
 
4536
aBpm	db	4,'bpm',0
4537
aBpmb	db	5,'bpmb',0
4538
aBpmw	db	5,'bpmw',0
4539
aBpmd	db	5,'bpmd',0
4540
BpmHelp	db	'set BreakPoint on Memory access',10
4541
	db	'Maximum 4 breakpoints of this type are allowed',10
4542
	db	'Note that for this breaks debugger is activated after access',10
4543
BpmSyntax db	'Usage: bpmb [w] ',10
4544
	db	'       bpmw [w] ',10
4545
	db	'       bpmd [w] ',10
4546
	db	'       bpm is synonym for bpmd',10
4547
	db	'"w" means break only on writes (default is on read/write)',10,0
4548
 
4549
aBl	db	3,'bl',0
4550
BlHelp	db	'Breakpoint List',10
4551
BlSyntax db	'Usage: bl          - list all breakpoints',10
4552
	db	'       bl  - display info on particular breakpoint',10,0
4553
 
4554
aBc	db	3,'bc',0
4555
BcHelp	db	'Breakpoint Clear',10
4556
BcSyntax db	'Usage: bc ',10
4557
	db	'Examples: bc 2',10
4558
	db	'          bc 1 3 4 A',10,0
4559
 
4560
aBd	db	3,'bd',0
4561
BdHelp	db	'Breakpoint Disable',10
4562
BdSyntax db	'Usage: bd ',10
4563
	db	'Examples: bd 2',10
4564
	db	'          bd 1 3 4 A',10,0
4565
 
4566
aBe	db	3,'be',0
4567
BeHelp	db	'Breakpoint Enable',10
4568
BeSyntax db	'Usage: be ',10
4569
	db	'Examples: be 2',10
4570
	db	'          be 1 3 4 A',10,0
4571
 
4572
aUnpack	db	7,'unpack',0
4573
UnpackHelp db	'Try to bypass unpacker code',10
4574
UnpackSyntax db	'Usage: unpack',10,0
4575
 
4576
aUnknownCommand db 'Unknown command',10,0
4577
 
4578
load_err_msg	db	'Cannot load program. ',0
4579
unk_err_msg	db	'Unknown error code -%4X',10,0
4580
load_err_msgs:
4581
	dd	.1, 0, .3, 0, .5, .6, 0, 0, .9, .A, 0, 0, 0, 0, 0, 0
4582
	dd	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .1E, .1F, .20
4583
.1		db	'HD undefined.',10,0
4584
.3		db	'Unknown FS.',10,0
4585
.5		db	'File not found.',10,0
4586
.6		db	'Unexpected EOF.',10,0
4587
.9		db	'FAT table corrupted.',10,0
4588
.A		db	'Access denied.',10,0
4589
.1E		db	'No memory.',10,0
4590
.1F		db	'Not Menuet/Kolibri executable.',10,0
4591
.20		db	'Too many processes.',10,0
4592
load_succ_msg	db	'Program loaded successfully! PID=%4X. Use "g" to run.',10,0
4593
need_debuggee	db	'No program loaded. Use "load" command.',10,0
4594
aAlreadyLoaded	db	'Program is already loaded. Use "terminate" or "detach" commands',10,0
4595
terminated_msg	db	'Program terminated.',10,0
4596
aException	db	'Debugged program caused an exception %2X. '
4597
aSuspended	db	'Suspended',10,0
4598
aContinued	db	'Continuing',10,0
4599
aRunningErr	db	'Program is running',10,0
4600
read_mem_err	db	'ERROR: cannot read process memory!!!',10,0
4601
aBreakpointLimitExceeded db 'Breakpoint limit exceeded',10,0
4602
aBreakErr	db	'Cannot activate breakpoint, it will be disabled',10,0
4603
aDuplicateBreakpoint db	'Duplicate breakpoint',10,0
4604
aInvalidBreak	db	'Invalid breakpoint number',10,0
4605
OnBeErrMsg	db	'There is already enabled breakpoint on this address',10,0
4606
aBreakNum	db	'%2X: at %8X',0
4607
aMemBreak1	db	'%2X: on ',0
4608
aMemBreak2	db	'read from ',0
4609
aMemBreak3	db	'access of ',0
4610
aMemBreak4	db	'byte',0
4611
aMemBreak5	db	'word',0
4612
aMemBreak6	db	'dword',0
4613
aMemBreak7	db	' at %8X',0
4614
aOneShot	db	', one-shot',0
4615
aDisabled	db	', disabled',0
4616
aBreakStop	db	'Breakpoint #%2X',10,0
4617
aUserBreak	db	'int3 command at %8X',10,0
4618
;dbgmsg_str	db	'Debug message for process %4X.',10,0
4619
aInvAddr	db	'Invalid address',10,0
4620
NoPrgLoaded_str	db	'No program loaded'
4621
NoPrgLoaded_len = $ - NoPrgLoaded_str
4622
aRunning	db	'Running'
4623
aPaused		db	'Paused'
4624
aPoint		db	0x1C
4625
aMinus		db	'-'
4626
aColon		db	':'
4627
aQuests		db	'??'
4628
aDots		db	'...'
4629
aParseError	db	'Parse error',10,0
4630
aDivByZero	db	'Division by 0',10,0
4631
calc_string	db	'%8X',10,0
4632
aUnaligned	db	'Unaligned address',10,0
4633
aEnabledBreakErr db	'Enabled breakpoints are not allowed',10,0
4634
aInterrupted	db	'Interrupted',10,0
4635
aUnpacked	db	'Unpacked successful!',10,0
4636
aPacked1	db	'Program is probably packed with ',0
4637
aPacked2	db	'.',10,'Try to unpack automatically? [y/n]: ',0
4638
aY_str		db	'y',10,0
4639
aN_str		db	'n',10,0
4640
mxp_nrv_name	db	'mxp_nrv',0
4641
mxp_name	db	'mxp',0
4642
mxp_lzo_name	db	'mxp_lzo',0
4643
mtappack_name	db	'mtappack',0
4644
flags		db	'CPAZSDO'
4645
flags_bits	db	0,2,4,6,7,10,11
4646
regs_strs:
4647
	db	'EAX='
4648
	db	'EBX='
4649
	db	'ECX='
4650
	db	'EDX='
4651
	db	'ESI='
4652
	db	'EDI='
4653
	db	'EBP='
4654
	db	'ESP='
4655
	db	'EIP='
4656
	db	'EFLAGS='
4657
 
4658
debuggee_pid	dd	0
4659
bSuspended	db	0
4660
bAfterGo	db	0
4661
temp_break	dd	0
4662
 
4663
disasm_table_1:
4664
	dd	cop22, cop22, cop22, cop22, cop21, cop21, cop0,  cop0		; 0x
4665
	dd	cop22, cop22, cop22, cop22, cop21, cop21, cop0,  cF
4666
	dd	cop22, cop22, cop22, cop22, cop21, cop21, cop0,  cop0		; 1x
4667
	dd	cop22, cop22, cop22, cop22, cop21, cop21, cop0,  cop0
4668
	dd	cop22, cop22, cop22, cop22, cop21, cop21, cseges,cop0		; 2x
4669
	dd	cop22, cop22, cop22, cop22, cop21, cop21, csegcs,cop0
4670
	dd	cop22, cop22, cop22, cop22, cop21, cop21, csegss,cop0		; 3x
4671
	dd	cop22, cop22, cop22, cop22, cop21, cop21, csegds,cop0
4672
	dd	cinc1, cinc1, cinc1, cinc1, cinc1, cinc1, cinc1, cinc1		; 4x
4673
	dd	cdec1, cdec1, cdec1, cdec1, cdec1, cdec1, cdec1, cdec1
4674
	dd	cpush1,cpush1,cpush1,cpush1,cpush1,cpush1,cpush1,cpush1		; 5x
4675
	dd	cpop1, cpop1, cpop1, cpop1, cpop1, cpop1, cpop1, cpop1
4676
	dd	cop0,  cop0,  cunk,  cunk,  csegfs,cseggs,c66,   c67		; 6x
4677
	dd	cpush21,cimul1,cpush22,cimul1,cunk,cunk,  cunk,  cunk
4678
	dd	cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1		; 7x
4679
	dd	cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1, cjcc1
4680
	dd	cop23, cop23, cop23, cop23, cop22, cop22, cop22, cop22		; 8x
4681
	dd	cop22, cop22, cop22, cop22, cunk,  cop22, cunk,  cpop2
4682
	dd	cop0,  cxchg1,cxchg1,cxchg1,cxchg1,cxchg1,cxchg1,cxchg1		; 9x
4683
	dd	ccbw,  ccwd,  cunk,  cop0,  cop0,  cop0,  cop0,  cop0
4684
	dd	cmov3, cmov3, cmov3, cmov3, cop0,  cop0,  cop0,  cop0		; Ax
4685
	dd	cop21, cop21, cop0,  cop0,  cop0,  cop0,  cop0,  cop0
4686
	dd	cmov11,cmov11,cmov11,cmov11,cmov11,cmov11,cmov11,cmov11		; Bx
4687
	dd	cmov12,cmov12,cmov12,cmov12,cmov12,cmov12,cmov12,cmov12
4688
	dd	cshift1,cshift1,cret2,cop0, cunk,  cunk,  cmov2, cmov2		; Cx
4689
	dd	center,cop0,  cunk,  cunk,  cop0,  cint,  cunk,  cunk
4690
	dd	cshift2,cshift2,cshift3,cshift3,caam,cunk,cunk,  cxlat		; Dx
4691
	dd	cD8,   cD9,   cDA,   cDB,   cDC,   cDD,   cDE,   cDF
4692
	dd	cloopnz,cloopz,cloop,cjcxz, cunk,  cunk,  cunk,  cunk		; Ex
4693
	dd	ccall1,cjmp1, cunk,  cjmp2, cunk,  cunk,  cunk,  cunk
4694
	dd	clock, cunk,  crepnz,crep,  cunk,  cop0,  cop1,  cop1		; Fx
4695
	dd	cop0,  cop0,  cop0,  cop0,  cop0,  cop0,  cop1,  cop1
4696
 
4697
disasm_table_2:
410 diamond 4698
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  csyscall,cunk,cunk		; 0x
205 heavyiron 4699
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4700
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 1x
4701
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4702
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 2x
4703
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4704
	dd	cunk,  crdtsc,cunk,  cunk,  csysenter,cunk,cunk, cunk		; 3x
4705
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4706
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 4x
4707
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4708
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 5x
4709
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4710
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 6x
4711
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4712
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; 7x
4713
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4714
	dd	cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2		; 8x
4715
	dd	cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2, cjcc2
4716
	dd	csetcc,csetcc,csetcc,csetcc,csetcc,csetcc,csetcc,csetcc		; 9x
4717
	dd	csetcc,csetcc,csetcc,csetcc,csetcc,csetcc,csetcc,csetcc
4718
	dd	cunk,  cunk,  ccpuid,cbtx2, cshld, cshld, cunk,  cunk		; Ax
4719
	dd	cunk,  cunk,  cunk,  cbtx2, cshrd, cshrd, cunk,  cop22
4720
	dd	ccmpxchg,ccmpxchg,cunk,cbtx2,cunk, cunk,  cmovzx,cmovzx		; Bx
4721
	dd	cunk,  cunk,  cbtx1, cbtx2, cbsf,  cbsr,  cmovsx,cmovsx
4722
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  ccmpxchg8b	; Cx
4723
	dd	cbswap,cbswap,cbswap,cbswap,cbswap,cbswap,cbswap,cbswap
4724
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; Dx
4725
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4726
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; Ex
4727
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4728
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk		; Fx
4729
	dd	cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk,  cunk
4730
 
4731
reg_table:
4732
	db	2,'al',0
4733
	db	2,'cl',1
4734
	db	2,'dl',2
4735
	db	2,'bl',3
4736
	db	2,'ah',4
4737
	db	2,'ch',5
4738
	db	2,'dh',6
4739
	db	2,'bh',7
4740
	db	2,'ax',8
4741
	db	2,'cx',9
4742
	db	2,'dx',10
4743
	db	2,'bx',11
4744
	db	2,'sp',12
4745
	db	2,'bp',13
4746
	db	2,'si',14
4747
	db	2,'di',15
4748
	db	3,'eax',16
4749
	db	3,'ecx',17
4750
	db	3,'edx',18
4751
	db	3,'ebx',19
4752
	db	3,'esp',20
4753
	db	3,'ebp',21
4754
	db	3,'esi',22
4755
	db	3,'edi',23
4756
	db	3,'eip',24
4757
	db	0
4758
 
4759
fn70_load_block:
4760
	dd	7
4761
	dd	1
4762
load_params dd	0
4763
	dd	0
4764
	dd	0
4765
i_end:
4766
loadname:
4767
	db	0
4768
	rb	255
4769
 
4770
prgname_ptr dd ?
4771
prgname_len dd ?
4772
 
4773
dbgwnd		dd	?
4774
 
4775
messages	rb	messages_height*messages_width
4776
messages_pos	dd	?
4777
 
4778
cmdline		rb	cmdline_width+1
4779
cmdline_len	dd	?
4780
cmdline_pos	dd	?
4781
curarg		dd	?
4782
 
4783
was_temp_break	db	?
4784
 
4785
dbgbufsize	dd	?
4786
dbgbuflen	dd	?
4787
dbgbuf		rb	256
4788
 
4789
needzerostart:
4790
 
4791
context:
4792
_eip	dd	?
4793
_eflags	dd	?
4794
_eax	dd	?
4795
_ecx	dd	?
4796
_edx	dd	?
4797
_ebx	dd	?
4798
_esp	dd	?
4799
_ebp	dd	?
4800
_esi	dd	?
4801
_edi	dd	?
4802
 
4803
oldcontext rb $-context
4804
 
4805
dumpread dd	?
4806
dumppos dd	?
4807
dumpdata rb	dump_height*10h
4808
 
4809
; breakpoint structure:
4810
; dword +0: address
4811
; byte +4: flags
4812
; bit 0: 1 <=> breakpoint valid
4813
; bit 1: 1 <=> breakpoint disabled
4814
; bit 2: 1 <=> one-shot breakpoint
4815
; bit 3: 1 <=> DRx breakpoint
4816
; byte +5: overwritten byte
4817
;          for DRx breaks: flags + (index shl 6)
4818
breakpoints_n = 256
4819
breakpoints	rb	breakpoints_n*6
4820
drx_break	rd	4
4821
 
4822
disasm_buf_size		dd	?
4823
 
4824
bReload			db	?
4825
 
4826
needzeroend:
4827
 
4828
disasm_buffer		rb	256
4829
disasm_start_pos	dd	?
4830
disasm_cur_pos		dd	?
4831
disasm_cur_str		dd	?
4832
disasm_string		rb	256
4833
 
4834
i_param		rb	256
4835
 
4836
; stack
4837
	align	400h
4838
	rb	400h
4839
used_mem: