forked from binary-manu/js86
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js86.js
1293 lines (1172 loc) · 48.2 KB
/
js86.js
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
js86 = (function () {
var js86 = {};
var MOD_MEM = 0;
var MOD_MEM_DISP8 = 1;
var MOD_MEM_DISP16 = 2;
var MOD_MEM_REG = 3;
var RM_DISPONLY = 6;
var SEGOVERRIDE_ES = 0x26;
var SEGOVERRIDE_CS = 0x2E;
var SEGOVERRIDE_SS = 0x36;
var SEGOVERRIDE_DS = 0x3E;
var REP_ZERO = 0xF3;
var REP_NOTZERO = 0xF2;
var LOCK_PREFIX = 0xF0;
var EXCEPTION_ENDOFSTREAM = js86.EXCEPTION_ENDOFSTREAM = "EndOfStreamException";
var EXCEPTION_BADOPCODE = js86.EXCEPTION_BADOPCODE = "BadOpcodeException";
var EXCEPTION_BADOPSTRUCT = js86.EXCEPTION_BADOPSTRUCT = "BadOpcodeStructureException";
var throwIt = function(excType, message, extra) {
throw { "name" : excType, "message" : message, "extra" : extra };
};
var toSigned = function(value, max) {
if (value >= (max/2))
value = value - max;
return value;
};
var makeToSigned = function(max) {
return function(value) {
return toSigned(value, max);
};
};
var toSigned8 = makeToSigned(256);
var toSigned16 = makeToSigned(65536);
js86.toSigned = toSigned;
js86.toSigned8 = toSigned8;
js86.toSigned16 = toSigned16;
/**
* <p>Tries to decode a MOD/REG/RM byte and its associated (optional)
* displacement from the next bytes of the stream.</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>reg: the value of the REG bit field</li>
* <li>mod: the value of the MOD bit field</li>
* <li>rm: the value of the RM bit field</li>
* <li>disp: the value of the displacement, as a signed value</li>
* </ul>
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var decodeModRegRm = function(info, allowedReg, allowedMod) {
allowedReg = allowedReg || [true, true, true, true, true, true, true, true];
allowedMod = allowedMod || [true, true, true, true];
var theByte = info.opcodes.getByte();
var reg = (theByte >> 3) & 0x07;
var disp;
var rm = theByte & 0x07;
var mod = (theByte >> 6) & 0x03;
if (!allowedReg[reg] || !allowedMod[mod]) {
throwIt(
EXCEPTION_BADOPCODE,
"MOD/REG/RM byte contains a value not allowed for this instruction",
{stream : info.opcodes}
);
}
disp = 0;
if (mod === MOD_MEM_DISP8) {
disp = toSigned8(info.opcodes.getByte());
} else if (mod === MOD_MEM_DISP16 || (mod === MOD_MEM && rm === RM_DISPONLY)) {
disp = info.opcodes.getByte();
disp += info.opcodes.getByte() * 256;
disp = toSigned16(disp);
}
info.decoded.reg = reg;
info.decoded.mod = mod;
info.decoded.rm = rm;
info.decoded.disp = disp;
};
/**
* <p>Tries to decode the rest of an instruction in the form
* OPCODE MOD/REG/RM (disp-lo (disp-hi)?)?
* </p>
* <p>
* Since this boils down to decoding the MOD/REG/RM sequence, it works
* just like decodeModRegRm.
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
* @see decodeModRegRm
*/
var dcModRegRm = function(info) {
decodeModRegRm(info);
};
var dcModRegRmMemory = function(info) {
decodeModRegRm(info, null, [true, true, true, false]);
};
/**
* <p>Tries to decode an instruction in the form OPCODE IMM8</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>imm1: the value of the immediate, as an unsigned value</li>
* </ul>
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var dcImm8 = function(info) {
// Decode an instruction in the form OPCODE IMM8
info.decoded.imm1 = info.opcodes.getByte();
};
/**
* <p>Tries to decode an instruction in the form OPCODE IMM16</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>imm1: the value of the immediate, as an unsigned value</li>
* </ul>
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var dcImm16 = function(info) {
var imm = info.opcodes.getByte();
imm += info.opcodes.getByte() * 256;
info.decoded.imm1 = imm;
};
/**
* <p>Tries to decode an instruction in the form OPCODE</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* Since this method parses what come after the opcode, this is actually
* just a no-op method.
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var dcOpcode = function(info) {
// Decode an instruction in the form OPCODE
// Oh, it's already done :)
};
/**
* <p>Tries to decode an instruction in the form OPCODE MOD/SEG/RM and
* its displacement.</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>reg: the value of the REG bit field, which is actually a segment register</li>
* <li>seg: the same as reg, but it's only present when the reg field points to a segment reister</li>
* <li>mod: the value of the MOD bit field</li>
* <li>rm: the value of the RM bit field</li>
* <li>disp: the value of the displacement, as a signed value</li>
* </ul>
* </p>
* <p>
* This will throw an exception if REG fields holds an invalid encoding.
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var dcModSegRm = function(mask) {
return function(info) {
decodeModRegRm(info, mask.concat([false, false, false, false]));
info.decoded.seg = info.decoded.reg;
};
};
/**
* <p>Tries to decode an instruction in the form OPCODE IMM16 IMM16.</p>
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>imm1: the value of the first (closest to the opcode) immediate, as an unsigned value</li>
* <li>imm2: the value of the second (farther from the opcode) immediate, as an unsigned value</li>
* </ul>
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var dcTwoImm = function(info) {
var imm1, imm2;
imm1 = info.opcodes.getByte();
imm1 += info.opcodes.getByte() * 256;
imm2 = info.opcodes.getByte();
imm2 += info.opcodes.getByte() * 256;
info.decoded.imm1 = imm1;
info.decoded.imm2 = imm2;
};
/**
* <p>Create a function for parsing instructions with opcodes extensions in the REG field.</p>
* <p>
* The only argument should be an array of 8 integer, one for each possible combination
* of the 3-bit opcode extension, where each item can take one of this choices:
* <ul>
* <li>-1: this opcode extension is not assigned</li>
* <li>0: this opcode extension have no immediate in the instruction</li>
* <li>1: this opcode extension have a 1-byte immediate in the instruction</li>
* <li>2: this opcode extension have a 2-byte immediate in the instruction</li>
* </ul>
* </p>
*
* <p> The returned function will try to parse instructions as per its
* extension specification.
* <p>If the stream ends before the all the needed bytes can be read, decoded
* is not modified. Otherwise the following properties are added to it:
* <ul>
* <li>ext: the value of the REG bit field, which is actually an opcode extension</li>
* <li>mod: the value of the MOD bit field</li>
* <li>rm: the value of the RM bit field</li>
* <li>disp: the value of the displacement, as a signed value</li>
* </ul>
* </p>
*
* <p>This function will throw an exception if it comes across an extension
* that is deemed unsupported by the input array.
* </p>
*
* @param {Object} decoded Broken down representation of an instruction
* @param {Stream} opcodes Byte stream from which opcodes are retrieved
*/
var makeModExtRmDc = function(extMap) {
var allowedReg = [];
for (i = 0; i < extMap.length; i++) {
allowedReg.push(extMap[i] >= 0);
}
return function(info) {
var ext, extFlag, imm, scale;
decodeModRegRm(info, allowedReg);
ext = info.decoded.reg;
info.decoded.ext = ext;
delete info.decoded.reg; // There is no reg in this instruction
extFlag = extMap[ext];
imm = 0;
scale = 1;
for (; extFlag > 0; extFlag--) {
imm += info.opcodes.getByte() * scale;
scale *= 256;
}
if (scale > 1) {
info.decoded.imm1 = imm;
}
};
};
var dcPrefix = function(info) {
info.decoded.prefixes = info.decoded.prefixes || [];
info.decoded.prefixes.push(info.decoded.opcode);
delete info.opcode;
info.disassemble(info);
};
var opcodeToDecodingClass = [
// 00
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcOpcode,
dcOpcode,
// 08
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcOpcode,
undefined,
// 10
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcOpcode,
dcOpcode,
// 18
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcOpcode,
dcOpcode,
// 20
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcPrefix, // Segment Override
dcOpcode,
// 28
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcPrefix, // Segment override
dcOpcode,
// 30
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcPrefix, // Segment override
dcOpcode,
// 38
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcImm8,
dcImm16,
dcPrefix, // Segment override,
dcOpcode,
// 40
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// 48
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// 50
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// 58
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// 60
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
// 68
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
// 70
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
// 78
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
// 80
makeModExtRmDc([1,1,1,1,1,1,1,1]),
makeModExtRmDc([2,2,2,2,2,2,2,2]),
makeModExtRmDc([1,-1,1,1,-1,1,-1,1]),
makeModExtRmDc([1,-1,1,1,-1,1,-1,1]),
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
// 88
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModRegRm,
dcModSegRm([true, true, true, true]),
dcModRegRmMemory,
dcModSegRm([true, false, true, true]), // this is for MOV SEG, MEM/REG, which should not accept CS as a target
makeModExtRmDc([0,-1,-1,-1,-1,-1,-1,-1]),
// 90
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// 98
dcOpcode,
dcOpcode,
dcTwoImm,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// A0
dcImm16,
dcImm16,
dcImm16,
dcImm16,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// A8
dcImm8,
dcImm16,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// B0
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
// B8
dcImm16,
dcImm16,
dcImm16,
dcImm16,
dcImm16,
dcImm16,
dcImm16,
dcImm16,
// C0
undefined,
undefined,
dcImm16,
dcOpcode,
dcModRegRmMemory,
dcModRegRmMemory,
makeModExtRmDc([1,-1,-1,-1,-1,-1,-1,-1]),
makeModExtRmDc([2,-1,-1,-1,-1,-1,-1,-1]),
// C8
undefined,
undefined,
dcImm16,
dcOpcode,
dcOpcode,
dcImm8,
dcOpcode,
dcOpcode,
// D0
makeModExtRmDc([0,0,0,0,0,0,-1,0]),
makeModExtRmDc([0,0,0,0,0,0,-1,0]),
makeModExtRmDc([0,0,0,0,0,0,-1,0]),
makeModExtRmDc([0,0,0,0,0,0,-1,0]),
dcImm8,
dcImm8,
undefined,
dcOpcode,
// D8 (escape instructions, treated as undefined)
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
// E0
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
dcImm8,
// E8
dcImm16,
dcImm16,
dcTwoImm,
dcImm8,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
// F0
dcPrefix, // LOCK prefix
undefined,
dcPrefix, // REPNZ
dcPrefix, // REPZ
dcOpcode,
dcOpcode,
makeModExtRmDc([1,-1,0,0,0,0,0,0]),
makeModExtRmDc([2,-1,0,0,0,0,0,0]),
// F8
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
dcOpcode,
makeModExtRmDc([0,0,-1,-1,-1,-1,-1,-1]),
makeModExtRmDc([0,0,0,0,0,0,0,-1])
];
var makePutbackByteStream = function(delegate) {
var theBytes = []
var nBytes = 0;
return {
"isEof" : function() {
return nBytes <= 0 && delegate.isEof();
},
"getPosition" : function() {
return delegate.getPosition() - nBytes;
},
"getByte" : function() {
var newByte;
if (nBytes > 0) {
return theBytes[--nBytes];
} else {
newByte = delegate.getByte();
theBytes.unshift(newByte);
return newByte;
}
},
"putback" : function() {
nBytes = theBytes.length;
},
"discard" : function() {
if (nBytes >= theBytes.length) {
theBytes = [];
} else {
theBytes.splice(nBytes, theBytes.length - nBytes);
}
}
};
};
var makeByteStreamFromString = function(string) {
var position = 0;
var getByte = function() {
if (position >= string.length) {
throwIt(EXCEPTION_ENDOFSTREAM, "Unexpected end of opcode stream");
}
return string.charCodeAt(position++);
};
var isEof = function() {
return position >= string.length;
};
var getPosition = function() {
return position;
};
return makePutbackByteStream({
getByte : getByte,
isEof : isEof,
getPosition : getPosition
});
};
js86.makeByteStreamFromString = makeByteStreamFromString;
var makeByteStreamFromHexString = function(string) {
var matcher = new RegExp("\\s*([0-9A-F])([0-9A-F])\\s*", "gi");
var result = null;
var byteString = "";
while (true) {
result = matcher.exec(string);
if (!result) {
break;
}
byteString += String.fromCharCode(parseInt("0x" + result));
}
return makeByteStreamFromString(byteString);
};
js86.makeByteStreamFromHexString = makeByteStreamFromHexString;
var makeIntelSyntaxFormatter = function(params) {
params = params || {};
var state = {
org : (typeof(params.org) === "number" && isFinite(params.org) && params.org >= 0) ? params.org : 0 // Initial disassembly address
};
var toGPRegName = function() {
var regMap = [
// One row for each size, the first is unused so that the
// index is equal to the size of the register in bytes
[],
["al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"], // 8 bits
["ax", "bx", "cx", "dx", "sp", "bp", "si", "di"] // 16 bits
];
return function(reg, size) {
var row = regMap[size];
return row && (row[reg] || "");
};
}();
var toSegRegName = function() {
var regMap = ["es", "cs", "ss", "ds"];
return function(reg) {
return regMap[reg] || "";
};
}();
var toRepPrefix = function(prefix) {
var map = [];
map[REP_NOTZERO] = "repnz";
map[REP_ZERO] = "repz";
return function(prefix) {
return map[prefix] || "";
};
}();
var toAddressingMode = function() {
var addrModes = [
"[bx][si]", "[bx][di]", "[bp][si]", "[bp][di]",
"[si]", "[di]", "[bp]", "[bx]"
];
return function(mode) {
return addrModes[mode] || "";
};
}();
var pdModRmHelper = function() {
var ptrs = ["", "byte ptr ", "word ptr ", "", "dword ptr "];
return function(decoded, state, size) {
var baseIndex = toAddressingMode(decoded.rm);;
if (!ptrs[size] || !baseIndex) {
return "";
}
var prefix = toSegRegName(findSegOverride(decoded));
prefix = (prefix) ? (prefix + ":") : prefix;
// Displacement only
if (decoded.mod === MOD_MEM && decoded.rm === RM_DISPONLY) {
baseIndex = "[0x" + decoded.disp.toString(16) + "]";
} else if (decoded.mod === MOD_MEM_REG) {
return toGPRegName(decoded.rm, size);
} else if (decoded.mod === MOD_MEM || decoded.mod === MOD_MEM_DISP8 || decoded.mod === MOD_MEM_DISP16) {
if (decoded.disp !== 0) {
baseIndex = decoded.disp + baseIndex;
}
} else {
return "";
}
return ptrs[size] + prefix + baseIndex;
};
}();
var findRep = function(decoded) {
var result;
(decoded.prefixes || []).forEach(function(item) {
if (item === REP_ZERO ||
item === REP_NOTZERO) {
result = item;
}
});
return result;
};
var findSegOverride = function(decoded) {
var result;
(decoded.prefixes || []).forEach(function(item) {
if (item === SEGOVERRIDE_CS ||
item === SEGOVERRIDE_SS ||
item === SEGOVERRIDE_DS ||
item === SEGOVERRIDE_ES) {
result = (item >> 3) & 0x3;
}
});
return result;
};
var findLock = function(decoded) {
var result = false;
(decoded.prefixes || []).forEach(function(item) {
if (item === LOCK_PREFIX) {
result = true;
}
});
return result;
};
var pdModRm8 = function(decoded, state) {
return pdModRmHelper(decoded, state, 1);
};
var pdModRm16 = function(decoded, state) {
return pdModRmHelper(decoded, state, 2);
};
var pdReg8 = function(decoded, state) {
return toGPRegName(decoded.reg, 1);
};
var pdReg16 = function(decoded, state) {
return toGPRegName(decoded.reg, 2);
};
var pdSigned8 = function(decoded, state) {
return toSigned8(decoded.imm1).toString();
};
var pdSigned16 = function(decoded, state) {
return toSigned16(decoded.imm1).toString();
};
var pdUnsigned8 = function(decoded, state) {
// There is no difference with the 16 bit version
return pdSigned16(decoded, state);
};
var pdUnsigned16 = function(decoded, state) {
return decoded.imm1.toString();
};
var pdReg8_2_0 = function(decoded, state) {
return toGPRegName(decoded.opcode & 0x7, 1);
};
var pdReg16_2_0 = function(decoded, state) {
return toGPRegName(decoded.opcode & 0x7, 2);
};
var pdEA8 = function(decoded, state) {
return "byte ptr [0x" + decoded.imm1.toString(16) + "]";
};
var pdEA16 = function(decoded, state) {
return "word ptr [0x" + decoded.imm1.toString(16) + "]";
};
var pdRegSeg = function(decoded, state) {
return toSegRegName(decoded.seg);
};
var pdXlat = function(decoded, state) {
var over = findSegOverride(decoded);
return (toSegRegName(over) || "ds") + ":[bx]";
};
var pdSegInOpcode = function(decoded, state) {
return toSegRegName((decoded.opcode >>> 3) & 0x3);
};
var pdConstant = function() {
// Table of constants
var constants = {};
return function(constant) {
constant = (typeof(constant) === "string") ? constant : constant.toString();
if (constants.hasOwnProperty(constant)) {
return constants[constant];
} else {
constants[constant] = function() {
return constant;
};
return constants[constant];
}
};
}();
var pdOffset8 = function(decoded, state) {
return "0x" + (state.org + decoded.usedBytes + toSigned8(decoded.imm1)).toString(16);
};
var pdOffset16 = function(decoded, state) {
return "0x" + (state.org + decoded.usedBytes + toSigned16(decoded.imm1)).toString(16);
};
var pdFarAddress = function(decoded, state) {
return "0x" + decoded.imm2.toString(16) + ":" + "0x" +
decoded.imm1.toString(16);
};
var pdFarIndirect = function(decoded, state) {
return pdModRmHelper(decoded, state, 4);
};
var intelPrinter = {
toString : function() {
return (this.prefixes.join(" ") + " " + this.mnemonic + " " + this.args.join(", ")).trim();
}
};
// Each mnemonics is at offset 0xOOE where:
// OO is the first opcode byte
// E is the optional 3-bit extension, so E should be in range 0-7
// If there is no extension, E is not present
var mnemonics = [];
mnemonics.unroll = function(item, base, iterations, increment) {
increment = (increment) ? increment : 1;
for (var i = 0; i < iterations; i++) {
this[base] = item;
base += increment;
}
};
// MOV
mnemonics[0x88] = { mnemonic : "mov", args : [pdModRm8, pdReg8] };
mnemonics[0x89] = { mnemonic : "mov", args : [pdModRm16, pdReg16] };
mnemonics[0x8A] = { mnemonic : "mov", args : [pdReg8, pdModRm8] };
mnemonics[0x8B] = { mnemonic : "mov", args : [pdReg16, pdModRm16] };
mnemonics[0xC60] = { mnemonic : "mov", args : [pdModRm8, pdSigned8] };
mnemonics[0xC70] = { mnemonic : "mov", args : [pdModRm16, pdSigned16] };
mnemonics.unroll({ mnemonic : "mov", args : [pdReg8_2_0, pdSigned8] }, 0xB0, 8);
mnemonics.unroll({ mnemonic : "mov", args : [pdReg16_2_0, pdSigned16] }, 0xB8, 8);
mnemonics[0xA0] = { mnemonic : "mov", args : [pdConstant("al"), pdEA8] };
mnemonics[0xA1] = { mnemonic : "mov", args : [pdConstant("ax"), pdEA16] };
mnemonics[0xA2] = { mnemonic : "mov", args : [pdEA8, pdConstant("al")] };
mnemonics[0xA3] = { mnemonic : "mov", args : [pdEA16, pdConstant("ax")] };
mnemonics[0x8E] = { mnemonic : "mov", args : [pdRegSeg, pdModRm16] };
mnemonics[0x8C] = { mnemonic : "mov", args : [pdModRm16, pdRegSeg] };
// PUSH
mnemonics[0xFF6] = { mnemonic : "push", args : [pdModRm16] };
mnemonics.unroll({ mnemonic : "push", args : [pdReg16_2_0] }, 0x50, 8);
mnemonics.unroll({ mnemonic : "push", args : [pdSegInOpcode] }, 0x06, 4, 8);
// POP
mnemonics[0x8F0] = { mnemonic : "pop", args : [pdModRm16] };
mnemonics.unroll({ mnemonic : "pop", args : [pdReg16_2_0] }, 0x58, 8);
mnemonics.unroll({ mnemonic : "pop", args : [pdSegInOpcode] }, 0x07, 4, 8);
// XCHG
mnemonics[0x86] = { mnemonic : "xchg", args : [pdReg8, pdModRm8] };
mnemonics[0x87] = { mnemonic : "xchg", args : [pdReg16, pdModRm16] };
mnemonics[0x90] = { mnemonic : "nop"};
mnemonics.unroll({ mnemonic : "xchg", args : [pdConstant("ax"), pdReg16_2_0] }, 0x91, 7);
// IN
mnemonics[0xE4] = { mnemonic : "in", args : [pdConstant("al"), pdUnsigned8] };
mnemonics[0xE5] = { mnemonic : "in", args : [pdConstant("ax"), pdUnsigned16] };
mnemonics[0xEC] = { mnemonic : "in", args : [pdConstant("al"), pdConstant("dx")] };
mnemonics[0xED] = { mnemonic : "in", args : [pdConstant("ax"), pdConstant("dx")] };
// OUT
mnemonics[0xE4] = { mnemonic : "out", args : [pdUnsigned8, pdConstant("al")] };
mnemonics[0xE5] = { mnemonic : "out", args : [pdUnsigned16, pdConstant("ax")] };
mnemonics[0xEE] = { mnemonic : "out", args : [pdConstant("dx"), pdConstant("al")] };
mnemonics[0xEF] = { mnemonic : "out", args : [pdConstant("dx"), pdConstant("ax")] };
// XLAT
mnemonics[0xD7] = { mnemonic : "xlat", args : [pdXlat] };
// LEA/LES/LDS
mnemonics[0x8D] = { mnemonic : "lea", args : [pdReg16, pdModRm16] };
mnemonics[0xC4] = { mnemonic : "les", args : [pdReg16, pdModRm16] };
mnemonics[0xC5] = { mnemonic : "lds", args : [pdReg16, pdModRm16] };
// LAHF/SAHF
mnemonics[0x9F] = { mnemonic : "lahf" };
mnemonics[0x9E] = { mnemonic : "sahf" };
// PUSHF/POPF
mnemonics[0x9D] = { mnemonic : "popf" };
// ADD
mnemonics[0x00] = { mnemonic : "add", args : [pdModRm8, pdReg8] };
mnemonics[0x01] = { mnemonic : "add", args : [pdModRm16, pdReg16] };
mnemonics[0x02] = { mnemonic : "add", args : [pdReg8, pdModRm8] };
mnemonics[0x03] = { mnemonic : "add", args : [pdReg16, pdModRm16] };
mnemonics[0x800] = { mnemonic : "add", args : [pdModRm8, pdSigned8] };
mnemonics[0x810] = { mnemonic : "add", args : [pdModRm16, pdSigned16] };
mnemonics[0x820] = { mnemonic : "add", args : [pdModRm8, pdSigned8] };
mnemonics[0x830] = { mnemonic : "add", args : [pdModRm16, pdSigned8] };
mnemonics[0x04] = { mnemonic : "add", args : [pdConstant("al"), pdSigned8] };
mnemonics[0x05] = { mnemonic : "add", args : [pdConstant("ax"), pdSigned16] };
// ADC
mnemonics[0x10] = { mnemonic : "adc", args : [pdModRm8, pdReg8] };
mnemonics[0x11] = { mnemonic : "adc", args : [pdModRm16, pdReg16] };
mnemonics[0x12] = { mnemonic : "adc", args : [pdReg8, pdModRm8] };
mnemonics[0x13] = { mnemonic : "adc", args : [pdReg16, pdModRm16] };
mnemonics[0x802] = { mnemonic : "adc", args : [pdModRm8, pdSigned8] };
mnemonics[0x812] = { mnemonic : "adc", args : [pdModRm16, pdSigned16] };
mnemonics[0x822] = { mnemonic : "adc", args : [pdModRm8, pdSigned8] };
mnemonics[0x832] = { mnemonic : "adc", args : [pdModRm16, pdSigned8] };
mnemonics[0x14] = { mnemonic : "adc", args : [pdConstant("al"), pdSigned8] };
mnemonics[0x15] = { mnemonic : "adc", args : [pdConstant("ax"), pdSigned16] };
// INC
mnemonics[0xFE0] = { mnemonic : "inc", args : [pdModRm8] };
mnemonics[0xFF0] = { mnemonic : "inc", args : [pdModRm16] };
mnemonics.unroll({ mnemonic : "inc", args : [pdReg16_2_0] }, 0x40, 8);
// AAA/DAA
mnemonics[0x37] = { mnemonic : "aaa" };
mnemonics[0x27] = { mnemonic : "daa" };
// SUB
mnemonics[0x28] = { mnemonic : "sub", args : [pdModRm8, pdReg8] };
mnemonics[0x29] = { mnemonic : "sub", args : [pdModRm16, pdReg16] };
mnemonics[0x2A] = { mnemonic : "sub", args : [pdReg8, pdModRm8] };
mnemonics[0x2B] = { mnemonic : "sub", args : [pdReg16, pdModRm16] };
mnemonics[0x805] = { mnemonic : "sub", args : [pdModRm8, pdSigned8] };
mnemonics[0x815] = { mnemonic : "sub", args : [pdModRm16, pdSigned16] };
mnemonics[0x825] = { mnemonic : "sub", args : [pdModRm8, pdSigned8] };
mnemonics[0x835] = { mnemonic : "sub", args : [pdModRm16, pdSigned8] };
mnemonics[0x2C] = { mnemonic : "sub", args : [pdConstant("al"), pdSigned8] };
mnemonics[0x2D] = { mnemonic : "sub", args : [pdConstant("ax"), pdSigned16] };
// SBB
mnemonics[0x18] = { mnemonic : "sbb", args : [pdModRm8, pdReg8] };
mnemonics[0x19] = { mnemonic : "sbb", args : [pdModRm16, pdReg16] };
mnemonics[0x1A] = { mnemonic : "sbb", args : [pdReg8, pdModRm8] };
mnemonics[0x1B] = { mnemonic : "sbb", args : [pdReg16, pdModRm16] };
mnemonics[0x803] = { mnemonic : "sbb", args : [pdModRm8, pdSigned8] };
mnemonics[0x813] = { mnemonic : "sbb", args : [pdModRm16, pdSigned16] };
mnemonics[0x823] = { mnemonic : "sbb", args : [pdModRm8, pdSigned8] };
mnemonics[0x833] = { mnemonic : "sbb", args : [pdModRm16, pdSigned8] };
mnemonics[0x1C] = { mnemonic : "sbb", args : [pdConstant("al"), pdSigned8] };
mnemonics[0x1D] = { mnemonic : "sbb", args : [pdConstant("ax"), pdSigned16] };
// DEC
mnemonics[0xFE1] = { mnemonic : "dec", args : [pdModRm8] };
mnemonics[0xFF1] = { mnemonic : "dec", args : [pdModRm16] };
mnemonics.unroll({ mnemonic : "dec", args : [pdReg16_2_0] }, 0x48, 8);
// AAS/DAS/NEG
mnemonics[0x3F] = { mnemonic : "aas" };
mnemonics[0x2F] = { mnemonic : "das" };