forked from rochus-keller/OberonSystem3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoder.Mod
2745 lines (2576 loc) · 73.8 KB
/
Decoder.Mod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE Decoder; (** non-portable *) (* NM 2.3.93 / 26.10.93 *)
(* Niklaus Mannhart, Patrik Reali, Pieter Muller *)
IMPORT Modules, Files, Fonts, Texts, Objects, Display, Oberon, Strings, Out, SYSTEM;
CONST
(* prefix *)
pCS = 2EH; pDS = 3EH; pES = 26H; pFS = 64H; pGS = 65H; pSS = 36H;
AdrSize = 67H; OpSize = 66H; none = -1;
(* output *)
PCpos = 0; HexPos = 7; OpPosC = 43; RMPosC = 52; Pos1 = 4;
OffPos = 35;
(* addressing modes *)
Reg = 0; (* Register *)
Mem1 = 1; (* addressing mode 1 *)
Mem2 = 2; (* addressing mode 2 *)
RegImm = 3; (* immediate to register *)
MemImm = 4; (* memory to register *)
MemFull = 5; (* full 32 bit adr *)
(* i386 Register *)
EAX = 0; ECX = 1; EDX = 2; EBX = 3; ESP = 4; EBP = 5; ESI = 6; EDI = 7; (* 32 bit register *)
AX = 0; CX = 1; DX = 2; BX = 3; SP = 4; BP = 5; SI = 6; DI = 7; (* 16 bit register *)
AL = 0; CL = 1; DL = 2; BL = 3; AH = 4; CH = 5; DH = 6; BH = 7; (* 8 bit register *)
ES = 20; CS = 21; SS = 22; DS = 23; FS = 24; GS = 25; (* 6, 7 reserved *) (* Segment register *)
CR = 0; DR = 8; TR = 16;
(* i387 *)
SReal = 0; (* single real 32 bit*)
LReal = 1; (* long real 64 bit *)
EReal = 2; (* extended real 80 bit *)
WInt = 3; (* word integer 16 bit *)
SInt = 4; (* single integer 32 bit *)
LInt = 5; (* long integer 64 bit *)
Byte14 = 6; Byte28 = 7; Byte94 = 8; Byte108 = 9;
Decimal = 10; (* BCD *)
nothing = 11;
(* size *)
Byte = 0; SignExt = 1; (* sign extended *)
BUG = 40;
(* String constants, define only once to avoid err 230 *)
VarEntriesStr = "VarEntries:";
EntriesStr = "Entries:";
CommandsStr = "Commands:";
PointersStr = "Pointers:";
DataStr = "Data:";
ConstStr = "Const: ";
CodeStr = "Code: ";
nofEntriesStr = " nofEntries: ";
nofCmdsStr = " nofCmds: ";
nofPtrsStr = " nofPtrs: ";
nofTypesStr = " nofTds: ";
nofImpsStr = " nofImps: ";
dataSizeStr = " dataSize: ";
constSizeStr = " conSize: ";
codeSizeStr = " codeSize: ";
refSizeStr = " refSize: ";
(*Targets*)
Priv = 16;
i486 = 1; Pentium = 2; PPro = 3;
TYPE
ProcArray = POINTER TO ARRAY OF ProcEntry;
ProcEntry = RECORD
name: ARRAY 64 OF CHAR;
point: LONGINT
END;
EntryArray = POINTER TO ARRAY OF LONGINT;
VAR
R, Ref: Files.Rider; (* Rider for Object file *)
W: Texts.Writer; (* Writer for output on screen *)
pc, breakpos: LONGINT; column: INTEGER; (* curser position in the current line *)
prefix, w: INTEGER; adrPrefix, opPrefix: BOOLEAN;
defaultFont, titleFont: Fonts.Font;
PE: ProcArray;
nofPE: LONGINT;
memory, compilable, suppressOut, suppressH: BOOLEAN;
ReadAdr: LONGINT;
target: SET;
objSuffix: ARRAY 8 OF CHAR;
OpPos, RMPos: SHORTINT;
(* general Procedures *)
PROCEDURE Bug (no: INTEGER);
BEGIN
Texts.WriteLn(W); Texts.WriteString(W, "*** decode error ***");
Texts.WriteLn(W);
(*Texts.Append(T, W.buf);
HALT(BUG)*)
END Bug;
PROCEDURE Next (VAR ch: CHAR);
BEGIN
INC(pc);
IF memory THEN SYSTEM.GET (ReadAdr, ch); INC (ReadAdr)
ELSE Files.Read(R, ch)
END;
(* IF pc = breakpos THEN Texts.SetFont(W, Fonts.This("Syntax12b.Scn.Fnt"))
ELSE Texts.SetFont(W, defaultFont)
END *)
END Next;
PROCEDURE GetByte (VAR b: INTEGER);
VAR ch: CHAR;
BEGIN
Next(ch);
b:= ORD(ch)
END GetByte;
PROCEDURE GetWord (VAR w: LONGINT);
VAR ch: CHAR;
BEGIN
Next(ch); w:= ORD(ch);
Next(ch); w:= LONG(ORD(ch)) * 100H + w
END GetWord;
PROCEDURE GetDWord (VAR dw: LONGINT);
VAR ch: CHAR; byte: INTEGER;
BEGIN
Next(ch); dw:= LONG(ORD(ch));
Next(ch); dw:= LONG(ORD(ch)) * 100H + dw;
Next(ch); dw:= LONG(ORD(ch)) * 10000H + dw;
GetByte(byte);
IF byte >= 128 THEN byte:= byte - 256 END;
dw:= LONG(byte) * 1000000H + dw
END GetDWord;
PROCEDURE GetNum (VAR x: LONGINT);
VAR ch: CHAR; n: INTEGER; y: LONGINT;
BEGIN n := 0; y := 0; Next (ch);
WHILE ch >= 80X DO INC(y, SYSTEM.LSH(ORD(ch) - 128, n)); INC(n, 7); Next (ch) END;
x := ASH(SYSTEM.LSH(ORD(ch), 25), n-25) + y
END GetNum;
PROCEDURE WriteLn;
BEGIN
Texts.WriteLn(W); column:= 0
END WriteLn;
PROCEDURE WriteString (str: ARRAY OF CHAR);
BEGIN
IF ~suppressOut THEN Texts.WriteString(W, str); column:= column + SHORT(LEN(str)) END
END WriteString;
PROCEDURE Write (ch: CHAR);
BEGIN
IF ~suppressOut THEN Texts.Write(W, ch); INC(column) END
END Write;
PROCEDURE WriteByte (byte: INTEGER);
(* write byte in hexadecimal form *)
PROCEDURE WriteHex(d: INTEGER);
BEGIN
IF d > 9 THEN Write(CHR(41H + d - 10))
ELSE Write(CHR(30H + d))
END
END WriteHex;
BEGIN (* WriteByte *)
WriteHex(byte DIV 16); WriteHex(byte MOD 16);
IF (~suppressH) & (column >= OpPos) THEN Write("H") END
END WriteByte;
PROCEDURE WriteWord (word: LONGINT);
(* write word in byte form, little endian notation *)
BEGIN
WriteByte(SHORT(word MOD 100H)); Write(" ");
WriteByte(SHORT(word DIV 100H) MOD 100H)
END WriteWord;
PROCEDURE WriteDWord (dword: LONGINT);
(* write dword in byte form, little endian notation *)
BEGIN
WriteWord(dword MOD 10000H); Write(" ");
WriteWord((dword DIV 10000H) MOD 10000H)
END WriteDWord;
PROCEDURE WriteWHex (word: LONGINT);
(* write word hexadecimal *)
VAR b: BOOLEAN;
BEGIN
b := suppressH; suppressH := TRUE;
WriteByte(SHORT(word DIV 100H)); WriteByte(SHORT(word MOD 100H));
IF ~b & (column >= OpPos) THEN Write("H") END;
suppressH := b;
END WriteWHex;
PROCEDURE WriteDWHex (dword: LONGINT);
VAR b: BOOLEAN;
BEGIN
b := suppressH; suppressH := TRUE;
WriteWHex(dword DIV 10000H MOD 10000H); WriteWHex(dword MOD 10000H);
IF ~b & (column >= OpPos) THEN Write("H") END;
suppressH := b;
END WriteDWHex;
PROCEDURE WriteDisp (disp: LONGINT);
BEGIN
Texts.WriteInt(W, disp, 1);
IF disp = MIN(LONGINT) THEN INC(column, 12) (* " -2147483648" *)
ELSE
IF disp <= 0 THEN INC(column) END;
disp:= ABS(disp);
WHILE disp # 0 DO
disp:= disp DIV 10;
INC(column)
END
END
END WriteDisp;
PROCEDURE Tab (pos: INTEGER);
BEGIN
IF ~suppressOut THEN
WHILE column < pos DO Write(" ") END
END
END Tab;
PROCEDURE WriteOp (opStr: ARRAY OF CHAR);
BEGIN
suppressOut := FALSE;
Tab(OpPos); WriteString(opStr); Tab(RMPos)
END WriteOp;
PROCEDURE WriteReg (reg: INTEGER);
(* w = 0: 8 bit; w = 1: 16/32 bit *)
BEGIN
IF reg >= ES (*DS*) THEN (* <<<< MH 15.3.1994 *)
IF reg = CS THEN WriteString("cs")
ELSIF reg = DS THEN WriteString("ds")
ELSIF reg = ES THEN WriteString("es")
ELSIF reg = SS THEN WriteString("ss")
ELSIF reg = FS THEN WriteString("fs")
ELSIF reg = GS THEN WriteString("gs")
ELSE Bug(BUG)
END
ELSIF w = 0 THEN
IF reg = 0 THEN WriteString("al")
ELSIF reg = 1 THEN WriteString("cl")
ELSIF reg = 2 THEN WriteString("dl")
ELSIF reg = 3 THEN WriteString("bl")
ELSIF reg = 4 THEN WriteString("ah")
ELSIF reg = 5 THEN WriteString("ch")
ELSIF reg = 6 THEN WriteString("dh")
ELSIF reg = 7 THEN WriteString("bh")
ELSE Bug(BUG)
END
ELSIF opPrefix THEN
IF reg = 0 THEN WriteString("ax")
ELSIF reg = 1 THEN WriteString("cx")
ELSIF reg = 2 THEN WriteString("dx")
ELSIF reg = 3 THEN WriteString("bx")
ELSIF reg = 4 THEN WriteString("sp")
ELSIF reg = 5 THEN WriteString("bp")
ELSIF reg = 6 THEN WriteString("si")
ELSIF reg = 7 THEN WriteString("di")
ELSE Bug(BUG)
END
ELSE
IF reg = 0 THEN WriteString("eax")
ELSIF reg = 1 THEN WriteString("ecx")
ELSIF reg = 2 THEN WriteString("edx")
ELSIF reg = 3 THEN WriteString("ebx")
ELSIF reg = 4 THEN WriteString("esp")
ELSIF reg = 5 THEN WriteString("ebp")
ELSIF reg = 6 THEN WriteString("esi")
ELSIF reg = 7 THEN WriteString("edi")
ELSE Bug(BUG)
END
END
END WriteReg;
PROCEDURE WriteAdrReg(reg: INTEGER);
BEGIN
IF adrPrefix THEN
IF reg = 0 THEN WriteString("ax")
ELSIF reg = 1 THEN WriteString("cx")
ELSIF reg = 2 THEN WriteString("dx")
ELSIF reg = 3 THEN WriteString("bx")
ELSIF reg = 4 THEN WriteString("sp")
ELSIF reg = 5 THEN WriteString("bp")
ELSIF reg = 6 THEN WriteString("si")
ELSIF reg = 7 THEN WriteString("di")
ELSE Bug(BUG)
END
ELSE
IF reg = 0 THEN WriteString("eax")
ELSIF reg = 1 THEN WriteString("ecx")
ELSIF reg = 2 THEN WriteString("edx")
ELSIF reg = 3 THEN WriteString("ebx")
ELSIF reg = 4 THEN WriteString("esp")
ELSIF reg = 5 THEN WriteString("ebp")
ELSIF reg = 6 THEN WriteString("esi")
ELSIF reg = 7 THEN WriteString("edi")
ELSE Bug(BUG)
END
END
END WriteAdrReg;
PROCEDURE WriteSpecialReg(reg: INTEGER);
BEGIN
IF reg >= TR THEN
WriteString("tr"); Write(CHR( reg-TR + ORD("0")))
ELSIF reg >= DR THEN
WriteString("dr"); Write(CHR( reg-DR + ORD("0")))
ELSE
WriteString("cr"); Write(CHR( reg-CR + ORD("0")))
END;
INCL(target, Priv)
END WriteSpecialReg;
PROCEDURE WritePrefix (prefix: INTEGER);
BEGIN
IF prefix = pCS THEN WriteString("CS:")
ELSIF prefix = pDS THEN WriteString("DS: ")
ELSIF prefix = pES THEN WriteString("ES: ")
ELSIF prefix = pFS THEN WriteString("FS: ")
ELSIF prefix = pGS THEN WriteString("GS: ")
ELSIF prefix = pSS THEN WriteString("SS: ")
ELSE
END
END WritePrefix;
PROCEDURE WriteRegReg (d, reg1, reg2: INTEGER);
BEGIN
IF d = 1 THEN
WriteReg(reg1); Write(","); WriteReg(reg2)
ELSE
WriteReg(reg2); Write(","); WriteReg(reg1)
END
END WriteRegReg;
PROCEDURE WriteMem (base, inx, scale: INTEGER; disp: LONGINT);
BEGIN
WritePrefix(prefix);
IF base # none THEN(* register relative *)
WriteDisp(disp);
Write("["); WriteAdrReg(base)
ELSE (* absolute *)
Write("["); WriteDisp(disp)
END;
IF (inx # none) & ~((inx = ESP) & (base = ESP))(* !! 15.4.93 Bug? & (base # ESP) *) THEN (* indexed *)
IF scale = 0 THEN WriteString(" + 1 * ")
ELSIF scale = 1 THEN WriteString(" + 2 * ")
ELSIF scale = 2 THEN WriteString(" + 4 * ")
ELSE WriteString(" + 8* ")
END;
WriteAdrReg(inx)
END;
Write("]")
END WriteMem;
PROCEDURE WriteMem1 (d, reg, base: INTEGER; disp: LONGINT);
(* d = TRUE: reg, mem ; d = FALSE: mem, reg *)
BEGIN
IF d = 1 THEN
WriteReg(reg); Write(",")
END;
WriteMem(base, none, 0, disp);
IF d = 0 THEN
Write(","); WriteReg(reg)
END
END WriteMem1;
PROCEDURE WriteMem2 (d, reg, base, inx: INTEGER; scale: SHORTINT; disp: LONGINT);
(* d = TRUE: reg, mem; d= FALSE: mem, reg *)
BEGIN
IF d = 1 THEN
WriteReg(reg); Write(",")
END;
WriteMem(base, inx, scale, disp);
IF d = 0 THEN
Write(","); WriteReg(reg)
END
END WriteMem2;
PROCEDURE WriteRegImm (reg: INTEGER; imm: LONGINT; hex: BOOLEAN);
BEGIN
WriteReg(reg); Write(",");
IF hex THEN WriteDWHex(imm) ELSE WriteDisp(imm) END
END WriteRegImm;
PROCEDURE WriteMemImm (reg, base, inx: INTEGER; scale: SHORTINT; disp, imm: LONGINT; hex: BOOLEAN);
BEGIN
WritePrefix(prefix);
WriteMem(base, inx, scale, disp);
Write(",");
IF hex THEN WriteDWHex(imm) ELSE WriteDisp(imm) END
END WriteMemImm;
PROCEDURE WriteRM (mode: SHORTINT; d, reg, base, inx, scale: INTEGER; disp, imm: LONGINT; hex: BOOLEAN);
BEGIN
CASE mode OF
RegImm:
WriteRegImm(reg, imm, hex)
| MemImm:
WriteMemImm(reg, base, inx, SHORT(scale), disp, imm, hex)
| Reg:
WriteRegReg(d, reg, base)
| Mem1:
WriteMem1(d, reg, base, disp)
| Mem2:
WriteMem2(d, reg, base, inx, SHORT(scale), disp)
| MemFull:
IF d = 1 THEN
WriteReg(reg); Write(",")
END;
Write("["); WriteDisp(disp); Write("]");
IF d = 0 THEN
Write(","); WriteReg(reg)
END
ELSE Bug(BUG)
END
END WriteRM;
(* Decode part *)
PROCEDURE GetImm (w: INTEGER; VAR imm: LONGINT);
VAR byte: INTEGER;
BEGIN
IF w = 0 THEN (* 8 bit *)
GetByte(byte); WriteByte(byte); Write(" ");
IF byte >= 128 THEN byte:= byte - 256 END;
imm:= byte
ELSIF opPrefix THEN (* 16 bit *)
GetWord(imm); WriteWord(imm); Write(" ");
IF imm >= 32768 THEN imm:= imm - 65536 END
ELSE (* 32 bit *)
GetDWord(imm); WriteDWord(imm); Write(" ")
END
END GetImm;
PROCEDURE ModRm (VAR mode: SHORTINT; VAR reg, base, inx: INTEGER; VAR scale: SHORTINT; VAR disp: LONGINT);
VAR mod, byte, temp: INTEGER;
BEGIN
GetByte(byte); WriteByte(byte); Write(" ");
mod:= byte DIV 40H;
reg:=(byte DIV 8) MOD 8;
base:= byte MOD 8;
IF mod = 3 THEN (* reg *)
mode:= Reg; inx:= none
ELSE
IF base = 4 THEN (* escape to two bytes *)
mode:= Mem2;
GetByte(byte);
WriteByte(byte); Write(" ");
scale:= SHORT(byte DIV 40H);
inx:= (byte DIV 8) MOD 8;
base:= byte MOD 8
ELSE (* one byte addressing mode *)
mode:= Mem1; inx:= none
END;
IF mod = 0 THEN (* no displ, or 32 bit address *)
IF base = 5 THEN (* disp32 *)
base:= none;
GetDWord(disp);
WriteDWord(disp); Write(" ")
ELSE disp:= 0
END
ELSIF mod = 1 THEN (* 8 bit displ *)
GetImm(0, disp)
ELSE (* 32 bit displacement *)
GetDWord(disp);
WriteDWord(disp); Write(" ")
END
END
END ModRm;
PROCEDURE Type1 (op: INTEGER; VAR mode: SHORTINT; VAR d, reg, base, inx: INTEGER;
VAR scale: SHORTINT; VAR disp, imm: LONGINT);
(* type 1: add, or, adc, sbb, and, sub, xor, cmp *)
BEGIN
IF op = 4 THEN
mode:= RegImm; w:= 0; reg:= AL; GetImm(0, imm)
ELSIF op = 5 THEN
mode:= RegImm; w:= 1; reg:= AX; GetImm(1, imm)
ELSE
CASE op OF
0: w:= 0; d:= 0
| 1: w:= 1; d:= 0
| 2: w:= 0; d:= 1
| 3: w:= 1; d:= 1
ELSE Bug(BUG)
END;
ModRm(mode, reg, base, inx, scale, disp)
END
END Type1;
PROCEDURE Add (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("add");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Add;
PROCEDURE Push (op: INTEGER);
VAR imm: LONGINT;
BEGIN
w:= 1;
IF op = 60H THEN
IF opPrefix THEN WriteOp("pusha") ELSE WriteOp("pushad") END;
ELSIF op = 68H THEN
IF opPrefix THEN
GetWord(imm); WriteWord(imm)
ELSE
GetDWord(imm); WriteDWord(imm)
END;
WriteOp("push"); WriteDisp(imm)
ELSIF op = 6AH THEN
GetImm(0, imm);
WriteOp("push"); WriteDisp(imm)
ELSIF op = 9CH THEN
IF opPrefix THEN WriteOp("pushf") ELSE WriteOp("pushfd") END;
ELSE
WriteOp("push");
CASE op OF
6: WriteReg(ES)
| 0EH: WriteReg(CS)
| 16H: WriteReg(SS)
| 1EH: WriteReg(DS)
| 50H..57H: WriteReg(op - 50H)
ELSE Bug(BUG)
END
END
END Push;
PROCEDURE Push2 (op: INTEGER);
BEGIN
END Push2;
PROCEDURE Pop(op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
IF op = 61H THEN
IF opPrefix THEN WriteOp("popa") ELSE WriteOp("popad") END;
ELSIF op = 8FH THEN
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("pop");
IF opPrefix THEN WriteString("word ptr ")
ELSE WriteString("dword ptr ")
END;
WriteMem(base, inx, scale, disp)
ELSIF op = 9DH THEN
IF opPrefix THEN WriteOp("popf") ELSE WriteOp("popfd") END;
ELSE
WriteOp("pop");
w := 1; (* pop takes only 16 or 32 bit ops *)
CASE op OF
7: WriteReg(ES)
| 17H: WriteReg(SS)
| 1FH: WriteReg(DS)
| 58H..5FH: WriteReg(op - 58H)
ELSE Bug(BUG)
END
END
END Pop;
PROCEDURE Pop2 (op: INTEGER);
BEGIN
END Pop2;
PROCEDURE Or (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 08H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("or");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, TRUE)
END Or;
PROCEDURE Adc (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op -10H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("adc");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Adc;
PROCEDURE Sbb (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 18H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("sbb");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Sbb;
PROCEDURE And (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 20H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("and");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, TRUE)
END And;
PROCEDURE Sub (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 28H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("sub");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Sub;
PROCEDURE Xor (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 30H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("xor");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, TRUE)
END Xor;
PROCEDURE Cmp (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
Type1(op - 38H, mode, d, reg, base, inx, scale, disp, imm);
WriteOp("cmp");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Cmp ;
PROCEDURE Inc (op: INTEGER);
BEGIN
WriteOp("inc"); w := 1; (* set width to 16/32 bits, bug2 *)
WriteReg(op - 40H)
END Inc;
PROCEDURE Dec (op: INTEGER);
BEGIN
WriteOp("dec"); w := 1;
WriteReg(op - 48H)
END Dec;
PROCEDURE Bound (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
w:= 1;
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("bound");
WriteRM(mode, 1, reg, base, inx, scale, disp, 0, FALSE)
END Bound;
PROCEDURE Imul (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
ModRm(mode, reg, base, inx, scale, disp);
w:= 1;
IF op = 69H THEN GetImm(1, imm)
ELSIF op = 6BH THEN GetImm(0, imm) (* sign extended *)
END;
WriteOp("imul");
WriteRM(mode, 1, reg, base, inx, scale, disp, 0, FALSE);
Write(","); WriteDisp(imm)
END Imul;
PROCEDURE Imul2 (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
w:= 1;
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("imul");
WriteRM(mode, 1, reg, base, inx, scale, disp, 0, FALSE)
END Imul2;
PROCEDURE Ins (op: INTEGER);
BEGIN
IF op = 6CH THEN WriteOp("insb")
ELSIF opPrefix THEN WriteOp("insw")
ELSE WriteOp("insd")
END
END Ins;
PROCEDURE Outs (op: INTEGER);
BEGIN
IF op = 6EH THEN WriteOp("outsb")
ELSIF opPrefix THEN WriteOp("outsw")
ELSE WriteOp("outsd")
END
END Outs;
PROCEDURE Jcc (op: INTEGER);
VAR disp: INTEGER;
BEGIN
GetByte(disp); WriteByte(disp);
IF disp >= 128 THEN disp:= disp - 256 END;
CASE op OF
70H: WriteOp("jo")
| 71H: WriteOp("jno")
| 72H: WriteOp("jb")
| 73H: WriteOp("jnb")
| 74H: WriteOp("jz")
| 75H: WriteOp("jnz")
| 76H: WriteOp("jbe")
| 77H: WriteOp("jnbe")
| 78H: WriteOp("js")
| 79H: WriteOp("jns")
| 7AH: WriteOp("jp")
| 7BH: WriteOp("jnp")
| 7CH: WriteOp("jl")
| 7DH: WriteOp("jnl")
| 7EH: WriteOp("jle")
| 7FH: WriteOp("jnle")
ELSE Bug(BUG)
END;
WriteDisp(disp); WriteString(" ("); (*Texts.*)WriteDWHex(pc + disp); Write(")")
END Jcc;
PROCEDURE Jcc2 (op: INTEGER);
VAR disp: LONGINT;
BEGIN
IF adrPrefix THEN
GetWord(disp); WriteWord(disp)
ELSE
GetDWord(disp); WriteDWord(disp)
END;
CASE op OF
80H: WriteOp("jo")
| 81H: WriteOp("jno")
| 82H: WriteOp("jb")
| 83H: WriteOp("jnb")
| 84H: WriteOp("jz")
| 85H: WriteOp("jnz")
| 86H: WriteOp("jbe")
| 87H: WriteOp("jnbe")
| 88H: WriteOp("js")
| 89H: WriteOp("jns")
| 8AH: WriteOp("jp")
| 8BH: WriteOp("jnp")
| 8CH: WriteOp("jl")
| 8DH: WriteOp("jnl")
| 8EH: WriteOp("jle")
| 8FH: WriteOp("jnle")
ELSE Bug(BUG)
END;
WriteDisp(disp); WriteString(" ("); (*Texts.*)WriteDWHex(pc + disp); Write(")")
END Jcc2;
PROCEDURE Test (op: INTEGER);
VAR reg, base, inx, byte: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
IF (op = 0A8H) OR (op = 0A9H) THEN
IF op = 0A8H THEN
w:= 0; reg:= AL
ELSE
w:= 1; reg:= AX
END;
GetImm(w, imm);
mode:= RegImm
ELSE
ModRm(mode, reg, base, inx, scale, disp);
IF op = 84H THEN w:= 0
ELSE w:= 1
END
END;
WriteOp("test");
WriteRM(mode, 0, reg, base, inx, scale, disp, imm, FALSE) (* bug1 *)
END Test;
PROCEDURE Xchg (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
IF (op >= 91H) & (op <= 97H) THEN (* xchg .ax, reg *)
w:= 1; reg:= AX; base:= op MOD 8;
mode:= Reg
ELSE
ModRm(mode, reg, base, inx, scale, disp);
IF op = 86H THEN w:= 0
ELSE w:= 1
END
END;
WriteOp("xchg");
WriteRM(mode, 1, reg, base, inx, scale, disp, 0, FALSE)
END Xchg;
PROCEDURE Mov (op: INTEGER);
VAR reg, base, inx, d: INTEGER; scale, mode: SHORTINT; disp, imm: LONGINT;
BEGIN
IF (op >= 88H) & (op <= 8BH) THEN
Type1(op - 88H, mode, d, reg, base, inx, scale, disp, imm)
ELSIF (op >= 0B0H) & (op <= 0B7H) THEN
mode:= RegImm; w:= 0; reg:= op - 0B0H; GetImm(w, imm)
ELSIF (op >= 0B8H) & (op <= 0BFH) THEN
mode:= RegImm; w:= 1; reg:= op - 0B8H; GetImm(w, imm)
ELSIF (op >= 0A0H) & (op <= 0A3H) THEN
CASE op OF
0A0H: w:= 0; d:= 1; reg:= AL
| 0A1H: w:= 1; d:= 1; reg:= AX
| 0A2H: w:= 0; d:= 0; reg:= AL
| 0A3H: w:= 1; d:= 0; reg:= AX
END;
mode:= MemFull;
IF adrPrefix THEN
GetWord(disp); WriteWord(disp)
ELSE
GetDWord(disp); WriteDWord(disp)
END
ELSIF op = 8CH THEN (* mov mem, seg *)
w:= 1; d:= 0; opPrefix:= TRUE;
ModRm(mode, reg, base, inx, scale, disp);
reg:= reg + ES (* reg is a segment register *)
ELSIF op = 8EH THEN (* mov seg, mem *)
w:= 1; d:= 1; opPrefix:= TRUE;
ModRm(mode, reg, base, inx, scale, disp);
reg:= reg + ES (* reg is segment register *)
ELSIF (op = 0C6H) OR (op = 0C7H) THEN
d:= 1;
IF op = 0C6H THEN w:= 0
ELSE w:= 1
END;
ModRm(mode, reg, base, inx, scale, disp);
IF mode = Reg THEN
reg:= base; mode:= RegImm
ELSE mode:= MemImm
END;
GetImm(w, imm)
END;
WriteOp("mov");
WriteRM(mode, d, reg, base, inx, scale, disp, imm, FALSE)
END Mov;
PROCEDURE Mov2 (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN (* reg, base only used, because Mov2 op codes contains special registers (debug/test/controll) *)
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("mov");
CASE op OF
20H: WriteReg(base); Write(","); WriteSpecialReg(CR+reg)
| 21H: WriteReg(base); Write(","); WriteSpecialReg(DR+reg)
| 22H: WriteSpecialReg(CR+reg); Write(","); WriteReg(base)
| 23H: WriteSpecialReg(DR+reg); Write(","); WriteReg(base)
| 24H: WriteReg(base); Write(","); WriteSpecialReg(TR+reg)
| 26H: WriteSpecialReg(TR+reg); Write(","); WriteReg(base)
ELSE Bug(BUG)
END
END Mov2;
PROCEDURE Movzx (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("movzx");
w:= 1;
WriteReg(reg); Write(",");
IF mode = Reg THEN WriteReg(base)
ELSE
IF op = 0B6H THEN WriteString("byte ptr ")
ELSE WriteString("word ptr ")
END;
WriteMem(base, inx, scale, disp)
END
END Movzx;
PROCEDURE Movsx (op: INTEGER);
VAR VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("movsx");
w:= 1;
WriteReg(reg); Write(",");
IF mode = Reg THEN
IF op = 0BEH THEN
w:= 0; WriteReg(base)
ELSE
w:= 1; opPrefix:= TRUE; WriteReg(base)
END;
ELSE
IF op = 0BEH THEN WriteString("byte ptr ")
ELSE WriteString("word ptr ")
END;
WriteMem(base, inx, scale, disp)
END
END Movsx;
PROCEDURE Lea (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
ModRm(mode, reg, base, inx, scale, disp);
w:= 1;
WriteOp("lea");
WriteRM(mode, 1, reg, base, inx, scale, disp, 0, TRUE)
END Lea;
PROCEDURE Call (op: INTEGER);
VAR imm, sel: LONGINT;
BEGIN
IF op = 0E8H THEN
IF adrPrefix THEN
GetWord(imm); WriteWord(imm)
ELSE
GetDWord(imm); WriteDWord(imm)
END
ELSE (* intrasegment *)
IF adrPrefix THEN
GetWord(imm); WriteWord(imm)
ELSE
GetDWord(imm); WriteDWord(imm)
END;
GetWord(sel); Write(" "); WriteWord(sel);
END;
WriteOp("call");
IF op = 09AH THEN
WriteDisp(sel); Write(":")
END;
WriteDisp(imm); WriteString(" ("); (*Texts.*)WriteDWHex(pc + imm); Write(")")
END Call;
PROCEDURE Movs (op: INTEGER);
BEGIN
IF op = 0A4H THEN WriteOp("movsb")
ELSIF (op = 0A5H) & opPrefix THEN WriteOp("movsw")
ELSIF op = 0A5H THEN WriteOp("movsd")
ELSE Bug(BUG)
END
END Movs;
PROCEDURE Cmps (op: INTEGER);
BEGIN
IF op = 0A6H THEN WriteOp("cmpsb")
ELSIF (op = 0A7H) & opPrefix THEN WriteOp("cmpsb")
ELSIF op = 0A7H THEN WriteOp("cmpsw")
ELSE Bug(BUG)
END
END Cmps;
PROCEDURE Stos (op: INTEGER);
BEGIN
IF op = 0AAH THEN WriteOp("stosb")
ELSIF (op = 0ABH) & opPrefix THEN WriteOp("stosw")
ELSIF op = 0ABH THEN WriteOp("stosd")
ELSE Bug(BUG)
END
END Stos;
PROCEDURE Lods (op: INTEGER);
BEGIN
IF op = 0ACH THEN WriteOp("lodsb")
ELSIF op = 0ADH THEN
IF opPrefix THEN WriteOp("lodsw") ELSE WriteOp("lodsd") END
ELSE Bug(BUG)
END
END Lods;
PROCEDURE Scas (op: INTEGER);
BEGIN
IF op = 0AEH THEN WriteOp("scasb")
ELSIF (op = 0AFH) & opPrefix THEN WriteOp("scasw")
ELSIF op = 0AFH THEN WriteOp("scasd")
ELSE Bug(BUG)
END
END Scas;
PROCEDURE Ret (op: INTEGER);
VAR imm: LONGINT;
BEGIN
IF (op = 0C2H) OR (op = 0CAH) THEN
GetWord(imm); WriteWord(imm)
END;
IF (op = 0CAH) OR (op = 0CBH) THEN WriteOp("ret far")
ELSE WriteOp("ret")
END;
IF (op = 0C2H) OR (op = 0CAH) THEN WriteDisp(imm) END
END Ret;
PROCEDURE Enter (op: INTEGER);
VAR l: LONGINT; b: INTEGER;
BEGIN
GetWord(l); WriteWord(l); Write(" ");
GetByte(b); WriteByte(b); Write(" ");
WriteOp("enter");
WriteDisp(l); Write(","); WriteDisp(b)
END Enter;
PROCEDURE Les (op: INTEGER);
VAR reg, base, inx: INTEGER; scale, mode: SHORTINT; disp: LONGINT;
BEGIN
ModRm(mode, reg, base, inx, scale, disp);
WriteOp("les");