forked from crypto2011/KBBUILDER
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DAsmUtil.pas
1370 lines (1266 loc) · 30.1 KB
/
DAsmUtil.pas
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
unit DasmUtil;
(*
The main i80x86 disassembler module of the DCU32INT utility by Alexei Hmelnov.
----------------------------------------------------------------------------
E-Mail: [email protected]
http://hmelnov.icc.ru/DCU/
----------------------------------------------------------------------------
See the file "readme.txt" for more details.
------------------------------------------------------------------------
IMPORTANT NOTE:
This software is provided 'as-is', without any expressed or implied warranty.
In no event will the author be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented, you must not
claim that you wrote the original software.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
interface
uses
DasmDefs,FixUp,{$IFNDEF XMLx86}DasmOpT{$ELSE}x86Reg,x86Dasm{$ENDIF};
function Identic(I: integer): integer;
function ReadByte(var B: integer): boolean;
function SeeNextCodeByte: Integer{-1 => Error};
procedure SkipByte; //Call after SeeNextCodeByte only - no checks inside
function UnReadByte: boolean;
{$IFNDEF XMLx86}
procedure SetPrefix(V: integer);
procedure SetSuffix(V: integer);
procedure SetOpName(V: integer);
procedure SetOpPrefix(V: integer);
procedure setEAMod3Tbl(const Tbl: array of TRegIndex);
{$ELSE}
procedure setEAMod3Tbl(Tbl: TRegTblIndex);
{$ENDIF}
procedure SetCmdArg(V: integer);
procedure SetSeg(V: integer);
function GetSeg: integer;
function imPtr: boolean;
//function im(DS: integer): boolean;
function ImmedBW(DS: integer): boolean;
//function imSExt(DS: integer): boolean;
function imInt(DS: integer): boolean;
function jmpOfs(DS: integer): boolean;
function getEA(W: integer;var M,A: integer): boolean;
function getImmOfsEA(W: integer;var A: integer): boolean;
procedure setEASize(DS: integer);
procedure setOS;
procedure setAS;
function GetAS: integer;
function GetOS: integer;
function GetMode: integer;
//64-bit support
{$IFDEF I64}
//function mode64: integer; replaced by absolute var
procedure setREX(W,V: integer);
function GetOS64: integer;
function wasREX: integer;
function wasRexW: integer;
function addREXBit(V,hPlace: integer): integer;
{$ENDIF}
function ReportImmed(IsInt,SignRq: boolean;DSF,hDSize,SegN,Ofs:Byte;
Fix: PFixupRec): LongInt;
type
PBMTblProc = ^TBMTblProc;
TBMTblProc = array[byte]of THBMName;
const
{Command arguments}
caReg = 1;
caEffAdr = 2;
caImmed = 3;
caVal = 4;
caJmpOfs = 5;
caInt = 6;
caMask = $f;
const
dsByte = 1;
dsWord = 2;
dsDbl = 3;
dsQWord = 4;
dsTWord = 5;
dsPtr = 6;
dsPtr6b = 7;
dsMax = 7;
dsMask = $7;
dsIPOfs = 8; //for TEffAddr.dOfs only
dsRIPOfs = 9; //for TEffAddr.dOfs only
dsToSize: array[0..9] of Cardinal = (0,1,2,4,8,10,4,6,4,4);
const
hAX=0;
hCX=1;
hDX=2;
hBX=3;
hSP=4;
hBP=5;
hSI=6;
hDI=7;
hPresent=$80;
regcSzB=$00;
regcSzW=$20;
regcSzD=$40;
regcSzQ=$60;
hPresent2b=hPresent+regcSzW;
hWReg=$10;
hRegHasRex=$10;
hRegSizeShift=5;
hRegSizeMask=$3;
dOfsSizeShift=4{5 (4 is enough because all the commands must fit in 15 bytes)};
dOfsOfsMask = (1 shl dOfsSizeShift)-1;
const
hBXF=hBX+hPresent2b;
hBPF=hBP+hPresent2b;
hSIF=(hSI+hPresent2b){shl 4};
hDIF=(hDI+hPresent2b){shl 4};
const
RMSL : array[0..7] of Byte = (
hBXF,
hBXF,
hBPF,
hBPF,
0,
0,
hBPF,
hBXF
) ;
RMSR : array[0..7] of Byte = (
hSIF,
hDIF,
hSIF,
hDIF,
hSIF,
hDIF,
0,
0
) ;
const
hES=0;
hCS=1;
hSS=2;
hDS=3;
hFS=4;
hGS=5;
hNoSeg=7;
hDefSeg=8;
DefEASeg: array[0..7] of Byte = (
hDS, hDS, hSS, hSS, hDS, hDS, hDS, hDS);
DefRegSeg: array[0..7] of Byte = (
hDS, hDS, hDS, hDS, hSS, hSS, hDS, hDS);
type
// TRegNum=0..7;
TRegCode=Byte;
PEffAddr=^TEffAddr;
TEffAddr=object
//protected required for other modules, which extend DasmUtils
hSeg:Byte;
DataSize: Byte; //dsXXX
//hBase:Byte;{Index4,Base4}
hBaseOnly:TRegCode;{Base4,HasRex1,dSize2,hPresent1}
hIndex:TRegCode;{Index4,HasRex1,dSize2,hPresent1}
dOfs:Byte;{OfsSize3,Ofs5}
SS: Byte;
Fix: PFixupRec;
//public
end ;
TCmArg=record
CmdKind: Byte;
DSize: Byte;
{$IFDEF XMLx86}
nArg: Byte; //In the list of TOpcodeArg`s
{$ENDIF}
Inf:integer{Byte};
Fix: PFixupRec;
end ;
PCmdInfo=^TCmdInfo;
TCmdInfo=record
PrefSize:Byte;
hCmd: TCmdIndex;
EA:TEffAddr;
Cnt:Byte;
Arg:array[1..3] of TCmArg;
end ;
var
OpSeg: Byte;
Cmd: TCmdInfo;
{$IFNDEF XMLx86}
CmdPrefix,CmdSuffix: integer;
PrefixCnt: integer;
PrefixTbl: array[0..10] of integer;
{$ENDIF}
var
AdrIs32Deft: boolean = true;
OpIs32Deft: boolean = true;
//{$IFDEF XMLx86}
ShowX86DasmExtraInfo: Boolean = false;
//{$ENDIF}
{$IFDEF I64}
const
WordSize: array[0..2]of Byte = (2,4,8);
rexfW=$8;
rexfPresent=$10;
var
modeI64: boolean = false;
mode64: byte absolute modeI64;
var
CurREX: Byte;
{$ELSE}
const
WordSize: array[0..1]of Byte = (2,4);
{$ENDIF}
//procedure ClearCommand;
function ReadCommand: boolean;
procedure ShowCommand;
{$IFNDEF XMLx86}
var
{$IFDEF I64}
RegTbl:array[Boolean{with REX}]of array[0..3] of PBMTblProc;
{$ELSE}
RegTbl:array[0..2] of PBMTblProc;
{$ENDIF}
SegRegTbl: PBMTblProc;
{$ELSE}
const
RegTbl:array[Boolean{with REX}]of array[0..3] of TRegTblIndex = ((rtRB,rtRW,rtRD,rtRQ),(rtRB64,rtRW,rtRD,rtRQ));
{$ENDIF}
function GetIntData(hDSize,Ofs:Byte;var I: LongInt): boolean;
implementation
uses
{$IFDEF UNICODE}AnsiStrings{$ELSE}SysUtils{$ENDIF},
{$IFNDEF XMLx86}op{$ELSE}x86Defs,x86Op,TypInfo{$ENDIF}, {DCU_In,} DCU_Out;
var
AdrIs32: boolean;
OpIs32: boolean;
{$IFNDEF XMLx86}
EAMod3TblProc: PBMTblProc;
EAMod3TblProcCnt: Integer;
{$ELSE}
EAMod3TblProc: TRegTblIndex;
{$ENDIF}
var {For unread}
fxState0: TFixupState;
procedure ClearCommand;
begin
PrevCodePtr := CodePtr;
fillChar(Cmd,SizeOf(Cmd),0);
OpSeg:=hDefSeg;
{$IFNDEF XMLx86}
CmdPrefix := 0;
CmdSuffix := 0;
PrefixCnt := 0;
{$ENDIF}
AdrIs32 := AdrIs32Deft;
OpIs32 := OpIs32Deft;
{$IFNDEF XMLx86}
EAMod3TblProc := Nil;
EAMod3TblProcCnt := 0;
{$ELSE}
EAMod3TblProc := rtNone;
{$ENDIF}
{$IFDEF I64}
CurREX := 0;
{$ENDIF}
SaveFixupState(fxState0);
end ;
(*
function ReadCodeByte(var B: Byte): boolean;
{ This procedure can use fixup information to prevent parsing commands }
{ which contradict fixups }
var
Fx: PFixupRec;
F: Byte;
begin
Result := false;
if CodePtr>=CodeEnd then
Exit {Memory block finished};
SkipFixups(CodePtr-CodeStart);
if CodePtr<FixUpEnd then
Exit {Code can't be inside FixUp};
repeat
Fx := CurFixup(CodePtr-CodeStart);
if Fx=Nil then
Break;
F := TByte4(Fx^.OfsF)[3];
if F<fxStart then begin
SetFixEnd;
Exit {Code can't be inside FixUp};
end ;
if F=fxStart then begin
if CodePtr>PrevCodePtr then
Exit {Can't be inside a command};
end
else {if F=fxEnd then}
Exit {Can't be inside a code};
until not NextFixup(CodePtr-CodeStart);
B := Byte(CodePtr^);
Inc(CodePtr);
Result := true;
end ;
*)
function ReadCodeByte(var B: Byte): boolean;
{ This procedure can use fixup information to prevent parsing commands }
{ which contradict fixups }
begin
Result := ChkNoFixupIn(CodePtr,1);
if not Result then
Exit;
B := Byte(CodePtr^);
Inc(CodePtr);
Result := true;
end ;
function SeeNextCodeByte: Integer{-1 => Error};
{ This procedure can use fixup information to prevent parsing commands }
{ which contradict fixups }
begin
if not ChkNoFixupIn(CodePtr,1) then begin
Result := -1;
Exit;
end ;
Result := Byte(CodePtr^);
end ;
procedure SkipByte;
begin
Inc(CodePtr);
end ;
function ReadImmedData(Size:Cardinal; var Res: Byte; var Fix: PFixupRec): boolean;
begin
Result := GetFixupFor(CodePtr,Size,false,Fix);
if not Result then
Exit;
Res := CodePtr-PrevCodePtr;
Inc(CodePtr,Size);
end ;
function Identic(I: integer): integer;
begin
Result := i;
end ;
function ReadByte(var B: integer): boolean;
var
B0: Byte;
begin
Result := ReadCodeByte(B0);
B := B0;
end ;
function UnReadByte: boolean;
begin
Result := false;
if CodePtr<=PrevCodePtr then
Exit;
Dec(CodePtr);
{May be it's not necessary here, but it will be safer to playback fixups:}
RestoreFixupState(fxState0);
SkipFixups(CodePtr-CodeStart);
Result := true;
end ;
{$IFNDEF XMLx86}
procedure SetPrefix(V: integer);
begin
CmdPrefix := V;
end ;
procedure SetSuffix(V: integer);
begin
CmdSuffix := V;
end ;
procedure SetOpName(V: integer);
begin
Cmd.hCmd := V;
end ;
procedure SetOpPrefix(V: integer);
begin
PrefixTbl[PrefixCnt] := V;
Inc(PrefixCnt);
end ;
procedure setEAMod3Tbl(const Tbl: array of TRegIndex);
begin
EAMod3TblProc := @Tbl;
EAMod3TblProcCnt := High(Tbl)+1;
end ;
{$ELSE XMLx86}
procedure setEAMod3Tbl(Tbl: TRegTblIndex);
begin
EAMod3TblProc := Tbl;
end ;
{$ENDIF}
procedure SetCmdArg(V: integer);
begin
// Result := false;
if Cmd.Cnt>=3 then
Exit;
Inc(Cmd.Cnt);
with Cmd.Arg[Cmd.Cnt] do begin
DSize := 0;
if V=hEA then
CmdKind := caEffAdr
else if (V and {$IFNDEF XMLx86}nf{$ELSE}nbMask{$ENDIF})<>0 then begin
CmdKind := caReg;
Inf := V and nm;
end
else {if (V and $FFFFFF00)=0 then} begin
CmdKind := caVal;
Inf := V;
end
{else
Exit};
end ;
end ;
procedure SetSeg(V: integer);
begin
OpSeg := V;
end ;
function GetSeg: integer;
begin
Result := OpSeg;
end ;
function im(DSize: integer): boolean;
{const
SizeTbl: array[1..6] of Cardinal = (
SizeOf(Byte),
SizeOf(Word),
SizeOf(LongInt),
8,
10,
SizeOf(Pointer)
);
PtrSize: array[boolean] of integer = (4,6);}
var
DSz,Size: Cardinal;
imOfs: Byte;
begin
Result := false;
if Cmd.Cnt>=3 then
Exit;
DSz := DSize;
if (DSz=dsPtr)and OpIs32 then
DSz := dsPtr6b;
Size := dsToSize[DSz];
Inc(Cmd.Cnt);
{if DSize=dsPtr then
Size := PtrSize[OpIs32]
else
Size := SizeTbl[DSize];}
with Cmd.Arg[Cmd.Cnt] do begin
CmdKind := caImmed;
DSize := DSz;
if not ReadImmedData(Size,ImOfs,Fix) then
Exit;
Inf := ImOfs;
end ;
Result := true;
end ;
function imPtr: boolean;
begin
Result := im(dsPtr);
end ;
function ImmedBW(DS: integer): boolean;
const
BWTbl: array[0..3] of Byte = (dsByte,dsWord,dsDbl,dsQWord);
begin
Result := im(BWTbl[DS and 3]);
end ;
(*
function imSExt(S,W: integer): boolean;
const
BWTbl: array[0..1] of Byte = (dsByte,dsWord);
var
SExt: Byte;
begin
Result := false;
SExt := S and $1;
if not im(BWTbl[(W and 1)and not (SExt){Ïðè SExt - èñïîëüçóåòñÿ íåïîñð. áàéò}])
then
Exit;
if SExt<>0 then
with Cmd.Arg[Cmd.Cnt] do
Kind := Kind and not caMask or caInt;
Result := true;
end ;
*)
function imInt(DS: integer): boolean;
begin
Result := ImmedBW(DS);
if Result then
with Cmd.Arg[Cmd.Cnt] do begin
CmdKind := caInt; //DSize is not changed //+(Kind and not caMask);
end ;
end ;
function jmpOfs(DS: integer): boolean;
begin
Result := ImmedBW(DS);
if Result then
with Cmd.Arg[Cmd.Cnt] do
CmdKind := caJmpOfs; //DSize is not changed //+(Kind and not caMask);
end ;
function getEA(W: integer;var M,A: integer): boolean;
var
CurB,Up2,Lo3,SIB: Byte;
OpSize:byte;
imOfs: Byte;
{$IFNDEF XMLx86}
TblProc: PBMTblProc;
{$ELSE}
RegCnt: Integer;
TblProc: TRegTblIndex;
{$ENDIF}
{$IFDEF I64}
var
SzF,OfsSzF:byte;
{$ELSE}
const
SzF=regcSzD;
OfsSzF=dsDbl;
{$ENDIF}
begin
Result := false;
{if W>dsMax then
W := dsMax;
OpSize := W;}
OpSize := (W and 3);
//Prevents from describing MMX commands
// if (OpSize>=3){$IFDEF I64}and not modeI64{$ENDIF} then
// Exit;
if not ReadCodeByte(CurB) then
Exit;
Up2 := CurB shr 6 ;
Lo3 := CurB and $7 ;
M := (CurB shr 3)and $7;
if Up2=3 then begin
{$IFDEF I64}
if CurREX>0 then
Lo3 := Lo3 or (CurREX and $1)shl 3;
{$ENDIF}
{$IFNDEF XMLx86}
if EAMod3TblProc<>Nil then begin
TblProc := EAMod3TblProc;
if Lo3>=EAMod3TblProcCnt then
Lo3 := Lo3 mod EAMod3TblProcCnt; //Just in case
end
else
TblProc := RegTbl{$IFDEF I64}[CurREX>0]{$ENDIF}[OpSize];
A := TblProc^[Lo3]
{$ELSE}
if EAMod3TblProc<>rtNone then
TblProc := EAMod3TblProc
else
TblProc := RegTbl[CurREX>0][OpSize];
RegCnt := RegTblInfo[TblProc].Cnt;
if Lo3>=RegCnt then
Lo3 := Lo3 mod RegCnt; //Just in case
A := EncodeRegIndex(TblProc,Lo3);
{$ENDIF}
end
else begin
A := hEA;
if {$IFDEF I64}modeI64 or{$ENDIF} AdrIs32 then begin
{$IFDEF I64}
SzF := regcSzD;
if modeI64 and AdrIs32{In fact means 64 bit} then
SzF := regcSzQ;
{$ENDIF}
if Lo3=hSP then begin {SIB}
if not ReadCodeByte(SIB) then
Exit;
Cmd.EA.SS := SIB shr 6;
Lo3 := SIB and 7 ;
if (Lo3=hBP)and(Up2=0) then begin
Up2 := 2;
Lo3 := 0;
{Base=EBP & mod=0 => Base=None & disp32}
end
else
Inc(Lo3,hPresent);
SIB := (SIB shr 3)and 7{Index};
if SIB<>hSP then
SIB := SIB+hPresent
else
SIB := 0;
{$IFDEF I64}
if CurREX>0 then begin
if Lo3 and hPresent<>0 then
Lo3 := Lo3 or hRegHasRex or (CurREX and $1)shl 3;
if SIB and hPresent<>0 then
SIB := SIB or hRegHasRex or (CurREX and $2)shl 2;
end ;
{$ENDIF}
{Cmd.EA.hBaseOnly := Lo3+SIB shl 4;}
Cmd.EA.hBaseOnly := Lo3 or SzF;
Cmd.EA.hIndex := SIB or SzF;
end
else {No SIB} begin
if (Up2=0)and(Lo3 = hBP) then begin
Cmd.EA.hBaseOnly := 0;
Cmd.EA.hIndex := 0;
if not ReadImmedData(SizeOf(LongInt),ImOfs,Cmd.EA.Fix) then
Exit;
{$IFDEF I64}
if modeI64 then {RIP based}
OfsSzF := dsIPOfs+Ord(AdrIs32)
else
OfsSzF := dsDbl;
{$ENDIF}
Cmd.EA.dOfs := OfsSzF shl dOfsSizeShift or ImOfs;
Lo3 := 0;{For OpSeg}
end
else begin
{$IFDEF I64}
if CurREX>0 then
Lo3 := Lo3 or hRegHasRex or (CurREX and $1)shl 3;
{$ENDIF}
Cmd.EA.hBaseOnly := Lo3 or hPresent or SzF;
Cmd.EA.hIndex := 0;
end ;
end ;
if OpSeg=hDefSeg then
OpSeg := hDefSeg or DefRegSeg[Lo3 and 7];
end
else {not AdrIs32} begin
if (Up2=0)and(Lo3=6) then begin
Cmd.EA.hBaseOnly := 0;
Cmd.EA.hIndex := 0;
if not ReadImmedData(SizeOf(Word),ImOfs,Cmd.EA.Fix) then
Exit;
Cmd.EA.dOfs := dsWord shl dOfsSizeShift or ImOfs;
Lo3 := 0;{For OpSeg}
end
else begin
Cmd.EA.hBaseOnly := RMSL[Lo3];
Cmd.EA.hIndex := RMSR[Lo3];
end ;
if OpSeg=hDefSeg then
OpSeg := hDefSeg or DefEASeg[Lo3 and 7];
end ;
Case Up2 of
1: begin
if not ReadImmedData(SizeOf(Byte),ImOfs,Cmd.EA.Fix) then
Exit;
Cmd.EA.dOfs := dsByte shl dOfsSizeShift or ImOfs;
end ;
2: begin
if not ReadImmedData(WordSize[Ord({$IFDEF I64}modeI64 or{$ENDIF}AdrIs32)],ImOfs,Cmd.EA.Fix) then
Exit;
Cmd.EA.dOfs := (dsWord+Ord({$IFDEF I64}modeI64 or{$ENDIF}AdrIs32)){dsWord} shl dOfsSizeShift or ImOfs;
end ;
End ;
{GetMemStr := GetSegStr+'['+MS+']' ;}
Cmd.EA.hSeg := OpSeg;
Cmd.EA.DataSize := W+1;
end ;
Result := CodePtr<=CodeEnd;
end ;
function getImmOfsEA(W: integer;var A: integer): boolean;
var
OpSize:byte;
imOfs: Byte;
begin
Result := false;
OpSize := W and $1;
if OpSize>0 then
OpSize := 1+Ord(OpIs32);
A := hEA;
if not ReadImmedData(WordSize[Ord(AdrIs32)],ImOfs,Cmd.EA.Fix) then
Exit;
Cmd.EA.dOfs := (dsWord+Ord(AdrIs32)){dsWord} shl dOfsSizeShift or ImOfs;
if OpSeg=hDefSeg then
OpSeg := hDefSeg or hDS;
Cmd.EA.hSeg := OpSeg;
Cmd.EA.DataSize := OpSize+1;
Result := CodePtr<=CodeEnd;
end ;
procedure setEASize(DS: integer);
var
S: Byte;
begin
S := DS;
if S>=dsMax then
Exit;
Cmd.EA.DataSize := S;
end ;
procedure setOS;
begin
OpIs32 := not OpIs32Deft;
end ;
procedure setAS;
begin
AdrIs32 := not AdrIs32Deft;
end ;
function GetAS: integer;
begin
Result := Ord(AdrIs32);
end ;
function GetOS: integer;
begin
Result := Ord(OpIs32);
end ;
function GetMode: integer;
begin
Result := Ord(AdrIs32Deft){$IFDEF I64}*(1+Ord(ModeI64)){$ENDIF};
end ;
//64-bit support
{$IFDEF I64}
procedure setREX(W,V: integer);
begin
CurREX := V and $7+(W and $1)*rexfW+rexfPresent;
end ;
function GetOS64: integer;
begin
if CurREX and rexfW<>0 then
Result := 2 //64-bit
else
Result := Ord(OpIs32);
end ;
function wasREX: integer;
begin
Result := Ord(CurREX<>0);
end ;
function wasRexW: integer;
begin
Result := Ord(CurREX and rexfW<>0);
end ;
function addREXBit(V,hPlace: integer): integer;
begin
V := V and $7; //Paranoic
hPlace := hPlace and $3; //Paranoic
if (CurREX<>0{was REX})and(hPlace>0{0 means Independent of REX})and(CurREX and(1 shl(hPlace-1))<>0{The bit is 1}) then
V := V or $8;
Result := V;
end ;
{$ENDIF}
function ReadCommand: boolean;
begin
ClearCommand;
Result := ReadOp;
end ;
{$IFNDEF XMLx86}
procedure WriteBMOpName(hN: THBMName);
begin
PutKW(GetOpName(hN));
end ;
{$ENDIF}
procedure WriteInt(i:integer);
begin
PutSFmt('%d',[i]);
end ;
procedure WriteRegVarInfo(hReg: TRegIndex; Ofs,Size: integer; IsFirst: boolean);
var
S: AnsiString;
hDecl: integer;
ProcOfs: integer;
begin
if not Assigned(GetRegVarInfo) then
Exit;
if IsFirst then {i.e. It may be an assignment target}
ProcOfs := CodePtr-CodeBase
else
ProcOfs := PrevCodePtr-CodeBase;
S := GetRegVarInfo(ProcOfs,hReg,Ofs,Size,hDecl);
if S<>'' then begin
PutCh('{');
PutAddrDefStr(S,hDecl);
PutCh('}');
end ;
end ;
procedure WriteRegName(hN: TRegIndex);
begin
PutKW(GetRegName(hN));
end ;
procedure WriteRegNameInf(hN: TRegIndex; IsFirst: boolean);
begin
WriteRegName(hN);
WriteRegVarInfo(hN,0{Ofs},0{Size: auto},IsFirst);
end ;
function WriteImmed(hDSize,Ofs:Byte; MayBeAddr: boolean; Fix: PFixupRec): LongInt;
var
DP,DP1: Pointer;
DS: Byte;
{IsAddr: boolean;
A: Pointer;}
Fixed: boolean;
begin
Result := 0;
DP := PrevCodePtr+Ofs;
DS := hDSize and dsMask;
Case DS of
dsByte: Result := Byte(DP^);
dsWord: Result := Word(DP^);
dsDbl: Result := LongInt(DP^);
End ;
Fixed := ReportFixUp(Fix,Result,ShowHeuristicRefs);
{
// if (ReportFixUp(Cardinal(DP)-Cardinal(CodeStart),hDSize and dsMask,DP)=0)
// then begin
IsAddr := MayBeAddr and((Ord(AdrIs32Deft)+dsWord)=hDSize);
if IsAddr then begin
if hDSize=dsWord then
LongInt(A) := Word(DP^)
else
LongInt(A) := LongInt(DP^);
// StartStrInfo(siDataAddr,A);
end ;
}
if Fixed then begin
if (DS=dsDbl){and(Result=0)} then
Exit;
PutS('{+');
end ;
Case DS of
dsByte: PutSFmt('$%2.2x',[Result]);
dsWord: PutSFmt('$%4.4x',[Result]);
dsDbl: PutSFmt('$%8.8x',[Result]);
dsPtr: begin
Result := LongInt(DP^);
PutSFmt('$%8.8x',[Result])
end ;
dsPtr6b: begin
DP1 := DP;
Inc(TIncPtr(DP1),4);
PutSFmt('$%4.4x:$%8.8x',[Word(DP1^),LongInt(DP^)]);
end ;
{dsPtr:
if not OpIs32 then begin
Result := LongInt(DP^);
PutSFmt('$%8.8x',[Result])
end
else begin
DP1 := DP;
Inc(integer(DP1),4);
PutSFmt('$%4.4x:$%8.8x',[Word(DP1^),LongInt(DP^)]);
end ;}
dsQWord: PutS(CharDumpStr(DP^,8));
dsTWord: PutS(CharDumpStr(DP^,10));
else
PutS('?Immed');
End ;
if Fixed then
PutS('}');
// if IsAddr then
// EndStrInfo;
//end ;
end ;
function WriteIntData(SignRq,FixSignRq,IsJmpOfs: boolean;hDSize,Ofs:Byte; Fix: PFixupRec): LongInt;
var
DP: Pointer;
DS: Byte;
Fixed: boolean;
begin
DP := PrevCodePtr+Ofs;
DS := hDSize and dsMask;
Case DS of
dsByte: Result := ShortInt(DP^);
dsWord: Result := SmallInt(DP^);
dsDbl: begin
Result := LongInt(DP^);
if IsJmpOfs then //The referenced address is computed from the command end (CodePtr)
Inc(Result,(CodePtr-TIncPtr(DP))-SizeOf(LongInt)); //required for IP-relative addressing in 64-bit mode
end ;
else
PutS('?Int');
Result := 0;
Exit;
End ;
if SignRq and ((Result>0)or FixSignRq and FixupOk(Fix)) then
PutS('+');
// if (ReportFixUp(Cardinal(DP)-Cardinal(CodeStart),hDSize and dsMask,DP)=0{<>0})
// then
Fixed := ReportFixUp(Fix,Result,ShowHeuristicRefs);
if Fixed then begin
if (DS=dsDbl){and(Result=0)} then
Exit;
end ;
if SignRq and(Result=0) then
Exit;
if Fixed then
PutS('{+');
WriteInt(Result);
if Fixed then
PutS('}');
end ;
procedure WriteJmpOfs(hDSize,Ofs:Byte; Fix: PFixupRec);
var
DOfs: LongInt;
begin
DOfs := WriteIntData(true,false,true{IsJmpOfs},hDSize,Ofs,Fix);
if Fix=Nil then begin
PutS(' (');
PutMemRefStr(Format('0x%x',[(CodePtr-CodeBase)+DOfs]),CodePtr-CodeMemBase+DOfs);
PutS(')');
end ;
end ;
function ReportImmed(IsInt,SignRq: boolean;DSF,hDSize,SegN,Ofs:Byte;
Fix: PFixupRec): LongInt;
{var
RepRes: Byte;}
begin
{if ReportDataRefsOn then
RepRes := ReportDataRefs(SignRq,DSF,hDSize,SegN,Ofs)
else}
{RepRes := 0;
if RepRes>1 then
PutS('{');}
if (not IsInt){or(RepRes>0)} then begin
if SignRq then
PutS('+');
Result := WriteImmed(hDSize,Ofs,{RepRes>0}false{MayBeAddr},Fix);
end
else
Result := WriteIntData(SignRq,true,false{IsJmpOfs},hDSize,Ofs,Fix);
//if RepRes>1 then
// PutS('}');
//ReportImmed := RepRes>1;
end ;
procedure WriteEA;
var
SegN,DSF: Byte;
Cnt,Sz:integer;
hLastReg: TRegIndex;
procedure Plus;