-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8086Disassembler.cs
1605 lines (1437 loc) · 45.2 KB
/
8086Disassembler.cs
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
namespace DotXT;
// TODO this needs a rewrite/clean-up
class P8086Disassembler
{
private State8086 _state;
private const uint MemMask = 0x00ffffff;
private Bus _b;
public byte ReadMemByte(ushort segment, ushort offset)
{
uint a = (uint)(((segment << 4) + offset) & MemMask);
var rc = _b.ReadByte(a);
_state.clock += rc.Item2;
return rc.Item1;
}
public ushort ReadMemWord(ushort segment, ushort offset)
{
return (ushort)(ReadMemByte(segment, offset) + (ReadMemByte(segment, (ushort)(offset + 1)) << 8));
}
public ushort GetAX()
{
return (ushort)((_state.ah << 8) | _state.al);
}
public ushort GetBX()
{
return (ushort)((_state.bh << 8) | _state.bl);
}
public ushort GetCX()
{
return (ushort)((_state.ch << 8) | _state.cl);
}
public ushort GetDX()
{
return (ushort)((_state.dh << 8) | _state.dl);
}
private byte GetByte(ref int instr_len, ref List<byte> bytes)
{
byte b = ReadMemByte(_state.cs, _state.ip);
bytes.Add(b);
_state.ip++;
instr_len++;
return b;
}
private ushort GetWord(ref int instr_len, ref List<byte> bytes)
{
byte low = GetByte(ref instr_len, ref bytes);
byte high = GetByte(ref instr_len, ref bytes);
return (ushort)(low + (high << 8));
}
// value, name, meta
private (ushort, string, string) GetDoubleRegisterMod00(int reg, ref int instr_len, ref List<byte> bytes)
{
ushort a = 0;
string name = "error";
string meta = "";
if (reg == 0)
{
a = (ushort)(GetBX() + _state.si);
name = "[BX+SI]";
}
else if (reg == 1)
{
a = (ushort)(GetBX() + _state.di);
name = "[BX+DI]";
}
else if (reg == 2)
{
a = (ushort)(_state.bp + _state.si);
name = "[BP+SI]";
}
else if (reg == 3)
{
a = (ushort)(_state.bp + _state.di);
name = "[BP+DI]";
}
else if (reg == 4)
{
a = _state.si;
name = "[SI]";
}
else if (reg == 5)
{
a = _state.di;
name = "[DI]";
}
else if (reg == 6)
{
a = GetWord(ref instr_len, ref bytes);
name = $"[${a:X4}]";
}
else if (reg == 7)
{
a = GetBX();
name = "[BX]";
}
else
{
meta = $"{nameof(GetDoubleRegisterMod00)} {reg} not implemented";
}
return (a, name, meta);
}
// value, name, meta, cycles
private (ushort, string, string, bool, ushort) GetDoubleRegisterMod01_02(int reg, bool word, ref int instr_len, ref List<byte> bytes)
{
ushort a = 0;
string name = "error";
bool override_segment = false;
ushort new_segment = 0;
string meta = "";
if (reg == 6)
{
a = _state.bp;
name = "[BP]";
override_segment = true;
new_segment = _state.ss;
}
else
{
(a, name, meta) = GetDoubleRegisterMod00(reg, ref instr_len, ref bytes);
}
short disp = word ? (short)GetWord(ref instr_len, ref bytes) : (sbyte)GetByte(ref instr_len, ref bytes);
return ((ushort)(a + disp), name, $"disp {disp:X4} " + meta, override_segment, new_segment);
}
// value, name_of_source, segment_a_valid, segment/, address of value, meta
private (ushort, string, bool, ushort, ushort, string) GetRegisterMem(int reg, int mod, bool w, ref int instr_len, ref List<byte> bytes)
{
string meta = "";
if (mod == 0)
{
(ushort a, string name, meta) = GetDoubleRegisterMod00(reg, ref instr_len, ref bytes);
ushort segment = _state.segment_override_set ? _state.segment_override : _state.ds;
if (_state.segment_override_set == false && (reg == 2 || reg == 3)) // BP uses SS
{
segment = _state.ss;
meta = $"BP SS-override ${_state.ss:X4}";
}
ushort v = w ? ReadMemWord(segment, a) : ReadMemByte(segment, a);
return (v, name, true, segment, a, meta);
}
if (mod == 1 || mod == 2)
{
bool word = mod == 2;
(ushort a, string name, meta, bool override_segment, ushort new_segment) = GetDoubleRegisterMod01_02(reg, word, ref instr_len, ref bytes);
ushort segment = _state.segment_override_set ? _state.segment_override : _state.ds;
if (_state.segment_override_set == false && override_segment)
{
segment = new_segment;
meta += $"BP SS-override ${_state.ss:X4} [2]";
}
if (_state.segment_override_set == false && (reg == 2 || reg == 3)) // BP uses SS
{
segment = _state.ss;
meta += $"BP SS-override ${_state.ss:X4} [3]";
}
ushort v = w ? ReadMemWord(segment, a) : ReadMemByte(segment, a);
return (v, name, true, segment, a, meta);
}
if (mod == 3)
{
(ushort v, string name) = GetRegister(reg, w);
return (v, name, false, 0, 0, "");
}
return (0, "error", false, 0, 0, $"reg {reg} mod {mod} w {w} not supported for {nameof(GetRegisterMem)}");
}
private string PutRegister(int reg, bool w, ushort val)
{
if (reg == 0)
{
if (w)
return "AX";
return "AL";
}
if (reg == 1)
{
if (w)
return "CX";
return "CL";
}
if (reg == 2)
{
if (w)
return "DX";
return "DL";
}
if (reg == 3)
{
if (w)
return "BX";
return "BL";
}
if (reg == 4)
{
if (w)
return "SP";
return "AH";
}
if (reg == 5)
{
if (w)
return "BP";
return "CH";
}
if (reg == 6)
{
if (w)
return "SI";
return "DH";
}
if (reg == 7)
{
if (w)
return "DI";
return "BH";
}
return "error";
}
private string PutSRegister(int reg, ushort v)
{
reg &= 0b00000011;
if (reg == 0b000)
return "ES";
if (reg == 0b001)
return "CS";
if (reg == 0b010)
return "SS";
if (reg == 0b011)
return "DS";
return "error";
}
private (ushort, string) GetRegister(int reg, bool w)
{
if (w)
{
if (reg == 0)
return (GetAX(), "AX");
if (reg == 1)
return (GetCX(), "CX");
if (reg == 2)
return (GetDX(), "DX");
if (reg == 3)
return (GetBX(), "BX");
if (reg == 4)
return (_state.sp, "SP");
if (reg == 5)
return (_state.bp, "BP");
if (reg == 6)
return (_state.si, "SI");
if (reg == 7)
return (_state.di, "DI");
}
else
{
if (reg == 0)
return (_state.al, "AL");
if (reg == 1)
return (_state.cl, "CL");
if (reg == 2)
return (_state.dl, "DL");
if (reg == 3)
return (_state.bl, "BL");
if (reg == 4)
return (_state.ah, "AH");
if (reg == 5)
return (_state.ch, "CH");
if (reg == 6)
return (_state.dh, "DH");
if (reg == 7)
return (_state.bh, "BH");
}
return (0, "error");
}
private (string, int) PutRegisterMem(int reg, int mod, bool w, ushort val, ref int instr_len, ref List<byte> bytes)
{
if (mod == 0)
{
(ushort a, string name, string meta) = GetDoubleRegisterMod00(reg, ref instr_len, ref bytes);
ushort segment = _state.segment_override_set ? _state.segment_override : _state.ds;
if (_state.segment_override_set == false && (reg == 2 || reg == 3)) { // BP uses SS
segment = _state.ss;
meta = $"BP SS-override ${_state.ss:X4}";
}
return (name, 0);
}
if (mod == 1 || mod == 2)
{
(ushort a, string name, string meta, bool override_segment, ushort new_segment) = GetDoubleRegisterMod01_02(reg, mod == 2, ref instr_len, ref bytes);
ushort segment = _state.segment_override_set ? _state.segment_override : _state.ds;
if (_state.segment_override_set == false && override_segment)
{
segment = new_segment;
meta = $"BP SS-override ${_state.ss:X4} [5]";
}
if (_state.segment_override_set == false && (reg == 2 || reg == 3)) // BP uses SS
{
segment = _state.ss;
meta = $"BP SS-override ${_state.ss:X4} [6]";
}
return (name, 0);
}
if (mod == 3)
return (PutRegister(reg, w, val), 0);
return ("error", 0);
}
(string, int) DisassemblyUpdateRegisterMem(int reg, int mod, bool a_valid, ushort seg, ushort addr, bool word, ushort v, ref int instr_len, ref List<byte> bytes)
{
if (a_valid)
return ($"[{addr:X4}]", 4);
return PutRegisterMem(reg, mod, word, v, ref instr_len, ref bytes);
}
private (ushort, string) GetSRegister(int reg)
{
reg &= 0b00000011;
if (reg == 0b000)
return (_state.es, "ES");
if (reg == 0b001)
return (_state.cs, "CS"); // TODO use _state.cs from Disassemble invocation?
if (reg == 0b010)
return (_state.ss, "SS");
if (reg == 0b011)
return (_state.ds, "DS");
Log.DoLog($"reg {reg} not supported for {nameof(GetSRegister)}", LogLevel.WARNING);
return (0, "error");
}
public ushort GetFlags()
{
return _state.flags;
}
private bool GetFlag(int bit)
{
return (_state.flags & (1 << bit)) != 0;
}
private bool GetFlagC()
{
return GetFlag(0);
}
private bool GetFlagP()
{
return GetFlag(2);
}
private bool GetFlagA()
{
return GetFlag(4);
}
private bool GetFlagZ()
{
return GetFlag(6);
}
private bool GetFlagS()
{
return GetFlag(7);
}
private bool GetFlagT()
{
return GetFlag(8);
}
private bool GetFlagI()
{
return GetFlag(9);
}
private bool GetFlagD()
{
return GetFlag(10);
}
private bool GetFlagO()
{
return GetFlag(11);
}
public ushort GetSS()
{
return _state.ss;
}
public ushort GetCS()
{
return _state.cs;
}
public ushort GetDS()
{
return _state.ds;
}
public ushort GetES()
{
return _state.es;
}
public ushort GetSP()
{
return _state.sp;
}
public ushort GetBP()
{
return _state.bp;
}
public ushort GetSI()
{
return _state.si;
}
public ushort GetDI()
{
return _state.di;
}
private string SegmentAddr(ushort seg, ushort a)
{
return $"{seg:X04}:{a:X04}";
}
private string GetFlagsAsString()
{
string @out = String.Empty;
@out += GetFlagO() ? "o" : "-";
@out += GetFlagI() ? "I" : "-";
@out += GetFlagT() ? "T" : "-";
@out += GetFlagS() ? "s" : "-";
@out += GetFlagZ() ? "z" : "-";
@out += GetFlagA() ? "a" : "-";
@out += GetFlagP() ? "p" : "-";
@out += GetFlagC() ? "c" : "-";
return @out;
}
public P8086Disassembler(Bus b)
{
_b = b;
}
public void SetCPUState(in State8086 state)
{
_state = state;
}
public string GetRegisters()
{
return $"{GetFlagsAsString()} AX:{GetAX():X4} BX:{GetBX():X4} CX:{GetCX():X4} DX:{GetDX():X4} SP:{GetSP():X4} BP:{GetBP():X4} SI:{GetSI():X4} DI:{GetDI():X4} flags:{GetFlags():X4} ES:{GetES():X4} CS:{_state.cs:X4} SS:{GetSS():X4} DS:{GetDS():X4} IP:{_state.ip:X4}";
}
// instruction length, instruction string, additional info, hex-string
public Tuple<int, string, string, string> Disassemble()
{
int instr_len = 0;
List<byte> bytes = new();
byte opcode = GetByte(ref instr_len, ref bytes);
string meta = "";
string prefix = "";
string instr = "";
// handle prefixes
while (opcode is (0x26 or 0x2e or 0x36 or 0x3e or 0xf2 or 0xf3))
{
if (opcode == 0x26)
prefix = "ES ";
else if (opcode == 0x2e)
prefix = "CS ";
else if (opcode == 0x36)
prefix = "SS ";
else if (opcode == 0x3e)
prefix = "DS ";
else if (opcode is (0xf2 or 0xf3))
{
}
else
{
return new Tuple<int, string, string, string>(1, "?", $"prefix {opcode:X2} not implemented", "" );
}
byte next_opcode = GetByte(ref instr_len, ref bytes);
if (opcode == 0xf2)
{
if (next_opcode is (0xa6 or 0xa7 or 0xae or 0xaf))
prefix += "REPNZ ";
else
prefix += "REP ";
}
else if (opcode == 0xf3)
{
if (next_opcode is (0xa6 or 0xa7 or 0xae or 0xaf))
prefix += "REPE/Z ";
else
prefix += "REP ";
}
opcode = next_opcode;
}
// main instruction handling
if (opcode == 0x04 || opcode == 0x14)
{
// ADD AL,xx
byte v = GetByte(ref instr_len, ref bytes);
instr = $"ADD AL,#{v:X2}";
}
else if (opcode == 0x05 || opcode == 0x15)
{
// ADD AX,xxxx
ushort v = GetWord(ref instr_len, ref bytes);
if (opcode == 0x05)
instr = $"ADD AX,${v:X4}";
else
instr = $"ADC AX,${v:X4}";
}
else if (opcode == 0x06)
{
instr = "PUSH ES";
}
else if (opcode == 0x07)
{
instr = "POP ES";
}
else if (opcode == 0x0e)
{
instr = "PUSH CS";
}
else if (opcode == 0x0f)
{
instr = "POP CS";
}
else if (opcode == 0x16)
{
instr = "PUSH SS";
}
else if (opcode == 0x17)
{
instr = "POP SS";
}
else if (opcode == 0x1c)
{
byte v = GetByte(ref instr_len, ref bytes);
instr = $"SBB AL,${v:X2}";
}
else if (opcode == 0x1d)
{
ushort v = GetWord(ref instr_len, ref bytes);
instr = $"SBB AX,${v:X4}";
}
else if (opcode == 0x1e)
{
instr = "PUSH DS";
}
else if (opcode == 0x1f)
{
instr = "POP DS";
}
else if (opcode == 0x27)
{
instr = "DAA";
}
else if (opcode == 0x2c)
{
byte v = GetByte(ref instr_len, ref bytes);
instr = $"SUB AL,${v:X2}";
}
else if (opcode == 0x2f)
{
instr = "DAS";
}
else if (opcode == 0x37)
{
instr = "AAA";
}
else if (opcode == 0x3f)
{
instr = "AAS";
}
else if (opcode == 0x2d)
{
ushort v = GetWord(ref instr_len, ref bytes);
instr = $"SUB AX,${v:X4}";
}
else if (opcode == 0x58)
{
instr = "POP AX";
}
else if (opcode == 0x59)
{
instr = "POP CX";
}
else if (opcode == 0x5a)
{
instr = "POP DX";
}
else if (opcode == 0x5b)
{
instr = "POP BX";
}
else if (opcode == 0x5c)
{
instr = "POP SP";
}
else if (opcode == 0x5d)
{
instr = "POP BP";
}
else if (opcode == 0x5e)
{
instr = "POP SI";
}
else if (opcode == 0x5f)
{
instr = "POP DI";
}
else if (opcode == 0xa4)
{
instr = "MOVSB";
}
else if (opcode == 0xa5)
{
instr = "MOVSW";
}
else if (opcode == 0xa6)
{
instr = "CMPSB";
}
else if (opcode == 0xa7)
{
instr = "CMPSW";
}
else if (opcode == 0xe3)
{
// JCXZ np
byte offset = GetByte(ref instr_len, ref bytes);
instr = "JCXZ ${offset:X02}";
}
else if (opcode == 0xe9)
{
short offset = (short)GetWord(ref instr_len, ref bytes);
ushort word = (ushort)(_state.ip + offset);
instr = $"JMP {_state.ip:X}";
meta = $"{offset:X4}";
}
else if (opcode == 0x50)
{
instr = "PUSH AX";
}
else if (opcode == 0x51)
{
instr = "PUSH CX";
}
else if (opcode == 0x52)
{
instr = "PUSH DX";
}
else if (opcode == 0x53)
{
instr = "PUSH BX";
}
else if (opcode == 0x54)
{
instr = "PUSH SP";
}
else if (opcode == 0x55)
{
instr = "PUSH BP";
}
else if (opcode == 0x56)
{
instr = "PUSH SI";
}
else if (opcode == 0x57)
{
instr = "PUSH DI";
}
else if (opcode is (0x80 or 0x81 or 0x82 or 0x83))
{
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg = o1 & 7;
int function = (o1 >> 3) & 7;
ushort r1 = 0;
string name1 = "error";
bool a_valid = false;
ushort seg = 0;
ushort addr = 0;
ushort r2 = 0;
if (opcode == 0x80)
{
(r1, name1, a_valid, seg, addr, meta) = GetRegisterMem(reg, mod, false, ref instr_len, ref bytes);
r2 = GetByte(ref instr_len, ref bytes);
}
else if (opcode == 0x81)
{
(r1, name1, a_valid, seg, addr, meta) = GetRegisterMem(reg, mod, true, ref instr_len, ref bytes);
r2 = GetWord(ref instr_len, ref bytes);
}
else if (opcode == 0x82)
{
(r1, name1, a_valid, seg, addr, meta) = GetRegisterMem(reg, mod, false, ref instr_len, ref bytes);
r2 = GetByte(ref instr_len, ref bytes);
}
else if (opcode == 0x83)
{
(r1, name1, a_valid, seg, addr, meta) = GetRegisterMem(reg, mod, true, ref instr_len, ref bytes);
r2 = GetByte(ref instr_len, ref bytes);
if ((r2 & 128) == 128)
r2 |= 0xff00;
}
else
{
meta = $"opcode {opcode:X2} not implemented";
}
string iname = "error";
if (function == 0)
iname = "ADD";
else if (function == 1)
iname = "OR";
else if (function == 2)
iname = "ADC";
else if (function == 3)
iname = "SBC";
else if (function == 4)
iname = "AND";
else if (function == 5)
iname = "SUB";
else if (function == 6)
iname = "XOR";
else if (function == 7)
iname = "CMP";
else
{
iname = "?";
meta = $"opcode {opcode:X2} function {function} not implemented";
}
instr = $"{iname} {name1},${r2:X2}";
}
else if (opcode == 0x84 || opcode == 0x85)
{
// TEST ...,...
bool word = (opcode & 1) == 1;
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg1 = (o1 >> 3) & 7;
int reg2 = o1 & 7;
(ushort r1, string name1, bool a_valid, ushort seg, ushort addr, meta) = GetRegisterMem(reg2, mod, word, ref instr_len, ref bytes);
(ushort r2, string name2) = GetRegister(reg1, word);
instr = $"TEST {name1},{name2}";
}
else if (opcode == 0x86 || opcode == 0x87)
{
// XCHG
bool word = (opcode & 1) == 1;
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg1 = (o1 >> 3) & 7;
int reg2 = o1 & 7;
(ushort r1, string name1, bool a_valid, ushort seg, ushort addr, meta) = GetRegisterMem(reg2, mod, word, ref instr_len, ref bytes);
(ushort r2, string name2) = GetRegister(reg1, word);
instr = $"XCHG {name1},{name2}";
}
else if (opcode == 0x8f)
{
// POP rmw
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg2 = o1 & 7;
(string toName, int put_cycles) = PutRegisterMem(reg2, mod, true, ReadMemWord(_state.ss, _state.sp), ref instr_len, ref bytes);
instr = $"POP {toName}";
}
else if (opcode == 0x90)
{
instr = "NOP";
}
else if (opcode >= 0x91 && opcode <= 0x97)
{
// XCHG AX,...
int reg_nr = opcode - 0x90;
(ushort v, string name_other) = GetRegister(reg_nr, true);
instr = $"XCHG AX,{name_other}";
}
else if (opcode == 0x98)
{
instr = $"CBW";
}
else if (opcode == 0x99)
{
instr = $"CDW";
}
else if (opcode == 0x9a)
{
// CALL far ptr
ushort temp_ip = GetWord(ref instr_len, ref bytes);
ushort temp_cs = GetWord(ref instr_len, ref bytes);
instr = $"CALL ${temp_cs:X} ${temp_ip:X}";
meta = $"${temp_cs * 16 + temp_ip:X}";
}
else if (opcode == 0x9c)
{
instr = "PUSHF";
}
else if (opcode == 0x9d)
{
instr = "POPF";
}
else if (opcode == 0xac)
{
instr = "LODSB";
}
else if (opcode == 0xad)
{
instr = "LODSW";
}
else if (opcode == 0xc2 || opcode == 0xc0)
{
ushort nToRelease = GetWord(ref instr_len, ref bytes);
instr = $"RET ${nToRelease:X4}";
}
else if (opcode == 0xc3 || opcode == 0xc1)
{
instr = "RET";
}
else if (opcode == 0xc4 || opcode == 0xc5)
{
// LES (c4) / LDS (c5)
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg = (o1 >> 3) & 7;
int rm = o1 & 7;
(ushort val, string name_from, bool a_valid, ushort seg, ushort addr, meta) = GetRegisterMem(rm, mod, true, ref instr_len, ref bytes);
string name;
if (opcode == 0xc4)
name = "LES";
else
name = "LDS";
string affected = PutRegister(reg, true, val);
instr = $"{name} {affected},{name_from}";
}
else if (opcode == 0xcc || opcode == 0xcd || opcode == 0xce)
{
// INT 0x..
if (opcode != 0xce || GetFlagO())
{
byte @int = 0;
if (opcode == 0xcc)
@int = 3;
else if (opcode == 0xce)
@int = 4;
else
@int = GetByte(ref instr_len, ref bytes);
ushort addr = (ushort)(@int * 4);
if (opcode == 0xce)
{
instr = $"INTO {@int:X2}";
meta = $"{SegmentAddr(_state.cs, _state.ip)} (from {addr:X4})";
}
else
{
instr = $"INT {@int:X2}";
meta = $"{SegmentAddr(_state.cs, _state.ip)} (from {addr:X4})";
}
}
}
else if (opcode == 0xcf)
{
instr = "IRET";
}
else if ((opcode >= 0x00 && opcode <= 0x03) || (opcode >= 0x10 && opcode <= 0x13) || (opcode >= 0x28 && opcode <= 0x2b) || (opcode >= 0x18 && opcode <= 0x1b) || (opcode >= 0x38 && opcode <= 0x3b))
{
bool word = (opcode & 1) == 1;
bool direction = (opcode & 2) == 2;
byte o1 = GetByte(ref instr_len, ref bytes);
int mod = o1 >> 6;
int reg1 = (o1 >> 3) & 7;
int reg2 = o1 & 7;
(ushort r1, string name1, bool a_valid, ushort seg, ushort addr, meta) = GetRegisterMem(reg2, mod, word, ref instr_len, ref bytes);
(ushort r2, string name2) = GetRegister(reg1, word);
string name = "error";
int result = 0;
bool apply = true;
if (opcode <= 0x03)
name = "ADD";
else if (opcode >= 0x10 && opcode <= 0x13)
name = "ADC";
else if (opcode >= 0x38 && opcode <= 0x3b)
{
apply = false;
name = "CMP";
}
else if (opcode >= 0x28 && opcode <= 0x2b)
name = "SUB";
else // 0x18...0x1b
{
name = "SBB";
}
// 0x38...0x3b are CMP
if (apply)
{
(string dummy, int put_cycles) = DisassemblyUpdateRegisterMem(reg2, mod, a_valid, seg, addr, word, (ushort)result, ref instr_len, ref bytes);
instr = $"{name} {name1},{name2}";
}
else
{