-
Notifications
You must be signed in to change notification settings - Fork 0
/
opcodes_prefix.h
1619 lines (1448 loc) · 74.9 KB
/
opcodes_prefix.h
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
/*
* Licencia Apache, Version 2.0 con Modificacion
*
* Copyright 2023 Desmon (David)
*
* Se concede permiso, de forma gratuita, a cualquier persona que obtenga una copia de
* este software y archivos de documentacion asociados (el "Software"), para tratar el
* Software sin restricciones, incluidos, entre otros, los derechos de uso, copia,
* modificacion, fusion, publicacion, distribucion, sublicencia y/o venta de copias del
* Software, y para permitir a las personas a quienes se les proporcione el Software
* hacer lo mismo, sujeto a las siguientes condiciones:
*
* El anterior aviso de copyright y este aviso de permiso se incluiran en todas las
* copias o partes sustanciales del Software.
*
* EL SOFTWARE SE PROPORCIONA "TAL CUAL", SIN GARANTiA DE NINGÚN TIPO, EXPRESA O
* IMPLiCITA, INCLUYENDO PERO NO LIMITADO A LAS GARANTiAS DE COMERCIABILIDAD, IDONEIDAD
* PARA UN PROPoSITO PARTICULAR Y NO INFRACCIoN. EN NINGÚN CASO LOS TITULARES DEL
* COPYRIGHT O LOS TITULARES DE LOS DERECHOS DE AUTOR SERaN RESPONSABLES DE NINGÚN
* RECLAMO, DAnO U OTRA RESPONSABILIDAD, YA SEA EN UNA ACCIoN DE CONTRATO, AGRAVIO O DE
* OTRA MANERA, QUE SURJA DE, FUERA DE O EN CONEXIoN CON EL SOFTWARE O EL USO U OTRO TIPO
* DE ACCIONES EN EL SOFTWARE.
*
* Ademas, cualquier modificacion realizada por terceros se considerara propiedad del
* titular original de los derechos de autor. Los titulares de derechos de autor
* originales no se responsabilizan de las modificaciones realizadas por terceros.
*
* Queda explicitamente establecido que no es obligatorio especificar ni notificar los
* cambios realizados entre versiones, ni revelar porciones especificas de codigo
* modificado.
*/
#ifndef __OPCODES_PREFIX_H_
#define __OPCODES_PREFIX_H_
/*
* Formato general de instrucciones x86 (http://www.c-jump.com/CIS77/images/x86_instruction_format.png):
*
* |-----------------|---------|--------|----------------------------------------------------------|-------|----------------|-------------|
* | numero de bytes | 0 a 4 | 1 a 3 | 0 o 1 | 0 o 1 | 0, 1, 2 o 4 | 0, 1, 2 o 4 |
* | valores | prefijo | opcode | Mod-RM | SIB | desplazamiento | inmediato |
* |-----------------|---------|------v-|-------------------------------v--------------------------|---v---|----------------|-------------|
* ______________________________| | |
* | | |
* |-v---|------------------|-----------|-----------------------| |--v--|-----|-------|-------| |--v--|-------|-------|-------|
* | Bit | 7 6 5 4 3 2 | 1 | 0 | | Bit | 7 6 | 5 4 3 | 2 1 0 | | Bit | 7 6 | 5 4 3 | 2 1 0 |
* | uso | main bits opcode | direccion | Longitud del operando | | Uso | MOD | REG | R/M | | Uso | SCALE | INDEX | BASE |
* |-----|------------------|-----------|-----------------------| |-----|-----|-------|-------| |-----|-------|-------|-------|
*
*/
#include <stdint.h>
#include "format_opcode_macros.h"
#include "debug_c.h"
typedef enum string_instrution_id {
// AAA – ASCII Adjust after Addition 0011 0111
STRING_AAA,
// AAD – ASCII Adjust AX before Division 1101 0101 : 0000 1010
STRING_AAD,
// AAM – ASCII Adjust AX after Multiply 1101 0100 : 0000 1010
STRING_AAM,
// AAS – ASCII Adjust AL after Subtraction 0011 1111
STRING_AAS,
/*
* ADC – ADD with Carry.
* register1 to register2 0001 000w : 11 reg1 reg2.
* register2 to register1 0001 001w : 11 reg1 reg2.
* memory to register 0001 001w : mod reg r/m.
* register to memory 0001 000w : mod reg r/m.
* immediate to register 1000 00sw : 11 010 reg : immediate data.
* immediate to AL, AX, or EAX 0001 010w : immediate data.
* immediate to memory 1000 00sw : mod 010 r/m : immediate data.
*/
STRING_ADC,
/*
* ADD – Add.
* register1 to register2 0000 000w : 11 reg1 reg2.
* register2 to register1 0000 001w : 11 reg1 reg2.
* memory to register 0000 001w : mod reg r/m.
* register to memory 0000 000w : mod reg r/m.
* immediate to register 1000 00sw : 11 000 reg : immediate data.
* immediate to AL, AX, or EAX 0000 010w : immediate data.
* immediate to memory 1000 00sw : mod 000 r/m : immediate data.
*/
STRING_ADD,
/*
* AND – Logical AND.
* register1 to register2 0010 000w : 11 reg1 reg2.
* register2 to register1 0010 001w : 11 reg1 reg2.
* memory to register 0010 001w : mod reg r/m.
* register to memory 0010 000w : mod reg r/m.
* immediate to register 1000 00sw : 11 100 reg : immediate data.
* immediate to AL, AX, or EAX 0010 010w : immediate data.
* immediate to memory 1000 00sw : mod 100 r/m : immediate data.
*/
STRING_AND,
/*
* ARPL – Adjust RPL Field of Selector.
* from register 0110 0011 : 11 reg1 reg2.
* from memory 0110 0011 : mod reg r/m.
*/
STRING_ARPL,
// BOUND – Check Array Against Bounds 0110 0010 : modA reg r/m
STRING_BOUND,
/*
* BSF – Bit Scan Forward.
* register1, register2 0000 1111 : 1011 1100 : 11 reg1 reg2.
* memory, register 0000 1111 : 1011 1100 : mod reg r/m.
*/
STRING_BSF,
/*
* BSR – Bit Scan Reverse.
* register1, register2 0000 1111 : 1011 1101 : 11 reg1 reg2.
* memory, register 0000 1111 : 1011 1101 : mod reg r/m.
*/
STRING_BSR,
// BSWAP – Byte Swap 0000 1111 : 1100 1 reg
STRING_BSWAP,
/*
* BT – Bit Test.
* register, immediate 0000 1111 : 1011 1010 : 11 100 reg: imm8 data.
* memory, immediate 0000 1111 : 1011 1010 : mod 100 r/m : imm8 data.
* register1, register2 0000 1111 : 1010 0011 : 11 reg2 reg1.
* memory, reg 0000 1111 : 1010 0011 : mod reg r/m.
*/
STRING_BT,
/*
* BTC – Bit Test and Complement.
* register, immediate 0000 1111 : 1011 1010 : 11 111 reg: imm8 data.
* memory, immediate 0000 1111 : 1011 1010 : mod 111 r/m : imm8 data.
* register1, register2 0000 1111 : 1011 1011 : 11 reg2 reg1.
* memory, reg 0000 1111 : 1011 1011 : mod reg r/m.
*/
STRING_BTC,
/*
* BTR – Bit Test and Reset,
* register, immediate 0000 1111 : 1011 1010 : 11 110 reg: imm8 data,
* memory, immediate 0000 1111 : 1011 1010 : mod 110 r/m : imm8 data,
* register1, register2 0000 1111 : 1011 0011 : 11 reg2 reg1,
* memory, reg 0000 1111 : 1011 0011 : mod reg r/m,
*/
STRING_BTR,
/*
* BTS – Bit Test and Set.
* register, immediate 0000 1111 : 1011 1010 : 11 101 reg: imm8 data.
* memory, immediate 0000 1111 : 1011 1010 : mod 101 r/m : imm8 data.
* register1, register2 0000 1111 : 1010 1011 : 11 reg2 reg1.
* memory, reg 0000 1111 : 1010 1011 : mod reg r/m.
*/
STRING_BTS,
/*
* CALL – Call Procedure (in same segment).
* direct 1110 1000 : full displacement.
* register indirect 1111 1111 : 11 010 reg.
* memory indirect 1111 1111 : mod 010 r/m.
*/
STRING_CALL,
/*
* CALL – Call Procedure (in other segment).
* direct 1001 1010 : unsigned full offset, selector.
* indirect 1111 1111 : mod 011 r/m.
*/
STRING_CALL_FAR,
// CBW – Convert Byte to Word 1001 1000
STRING_CBW,
// CDQ – Convert Doubleword to Qword 1001 1001
STRING_CDQ,
// CLC – Clear Carry Flag 1111 1000
STRING_CLC,
// CLD – Clear Direction Flag 1111 1100
STRING_CLD,
// CLI – Clear Interrupt Flag 1111 1010
STRING_CLI,
// CLTS – Clear Task-Switched Flag in CR0 0000 1111 : 0000 0110
STRING_CLTS,
// CMC – Complement Carry Flag 1111 0101
STRING_CMC,
/*
* CMP – Compare Two Operands.
* register1 with register2 0011 100w : 11 reg1 reg2.
* register2 with register1 0011 101w : 11 reg1 reg2.
* memory with register 0011 100w : mod reg r/m.
* register with memory 0011 101w : mod reg r/m.
* immediate with register 1000 00sw : 11 111 reg : immediate data.
* immediate with AL, AX, or EAX 0011 110w : immediate data.
* immediate with memory 1000 00sw : mod 111 r/m : immediate data.
*/
STRING_CMP,
// CMPS/CMPSB/CMPSW/CMPSD – Compare String Operands 1010 011w
STRING_CMPS,
STRING_CMPSB = STRING_CMPS,
STRING_CMPSW = STRING_CMPS,
STRING_CMPSD = STRING_CMPS,
/*
* CMPXCHG – Compare and Exchange.
* register1, register2 0000 1111 : 1011 000w : 11 reg2 reg1.
* memory, register 0000 1111 : 1011 000w : mod reg r/m.
*/
STRING_CMPXCHG,
// CPUID – CPU Identification 0000 1111 : 1010 0010
STRING_CPUID,
// CWD – Convert Word to Doubleword 1001 1001
STRING_CWD,
// CWDE – Convert Word to Doubleword 1001 1000
STRING_CWDE,
// DAA – Decimal Adjust AL after Addition 0010 0111
STRING_DAA,
// DAS – Decimal Adjust AL after Subtraction 0010 1111
STRING_DAS,
/*
* DEC – Decrement by 1.
* register 1111 111w : 11 001 reg.
* register (alternate encoding) 0100 1 reg.
* memory 1111 111w : mod 001 r/m.
*/
STRING_DEC,
/*
* DIV – Unsigned Divide.
* AL, AX, or EAX by register 0100 000B 1111 011w : 11 110 reg.
* Divide RDX:RAX by qwordregister 0100 100B 1111 0111 : 11 110 qwordreg.
* AL, AX, or EAX by memory 0100 00XB 1111 011w : mod 110 r/m.
* Divide RDX:RAX by memory64 0100 10XB 1111 0111 : mod 110 r/m.
*/
STRING_DIV,
// HLT – Halt 1111 0100
STRING_HLT,
/*
* IDIV – Signed Divide.
* AL, AX, or EAX by register 1111 011w : 11 111 reg.
* AL, AX, or EAX by memory 1111 011w : mod 111 r/m.
*/
STRING_IDIV,
/*
* IMUL – Signed Multiply.
* AL, AX, or EAX with register 1111 011w : 11 101 reg.
* AL, AX, or EAX with memory 1111 011w : mod 101 reg.
* register1 with register2 0000 1111 : 1010 1111 : 11 : reg1 reg2.
* register with memory 0000 1111 : 1010 1111 : mod reg r/m.
* register1 with immediate to register2 0110 10s1 : 11 reg1 reg2 : immediate data.
* memory with immediate to register 0110 10s1 : mod reg r/m : immediate data.
*/
STRING_IMUL,
/*
* IN – Input From Port.
* fixed port 1110 010w : port number.
* variable port 1110 110w.
*/
STRING_IN,
/*
* INC – Increment by 1.
* reg 1111 111w : 11 000 reg.
* reg (alternate encoding) 0100 0 reg.
* memory 1111 111w : mod 000 r/m.
*/
STRING_INC,
// INS – Input from DX Port 0110 110w
STRING_INS,
// INT n – Interrupt Type n 1100 1101 : type
STRING_INT_n,
// INT – Single-Step Interrupt 3 1100 1100
STRING_INT_3,
// INTO – Interrupt 4 on Overflow 1100 1110
STRING_INTO,
// INVD – Invalidate Cache 0000 1111 : 0000 1000
STRING_INVD,
// INVLPG – Invalidate TLB Entry 0000 1111 : 0000 0001 : mod 111 r/m
STRING_INVLPG,
// INVPCID – Invalidate Process-Context Identifier 0110 0110:0000 1111:0011 1000:1000 0010: mod reg r/m
STRING_INVPCID,
// IRET/IRETD – Interrupt Return 1100 1111
STRING_IRET,
STRING_IRETD = STRING_IRET,
/*
* Jcc – Jump if Condition is Met.
* 8-bit displacement 0111 tttn : 8-bit displacement.
* full displacement 0000 1111 : 1000 tttn : full displacement.
*/
STRING_JCC,
/*
* JCXZ/JECXZ – Jump on CX/ECX Zero
* Address-size prefix differentiates JCXZ
* and JECXZ.
* 1110 0011 : 8-bit displacement.
*/
STRING_JCXZ,
STRING_JECXZ = STRING_JCXZ,
/*
* JMP – Unconditional Jump (to same segment).
* short 1110 1011 : 8-bit displacement.
* direct 1110 1001 : full displacement.
* register indirect 1111 1111 : 11 100 reg.
* memory indirect 1111 1111 : mod 100 r/m.
*/
STRING_JMP,
/*
* JMP – Unconditional Jump (to other segment).
* direct intersegment 1110 1010 : unsigned full offset, selector.
* indirect intersegment 1111 1111 : mod 101 r/m.
*/
STRING_JMP_FAR,
// LAHF – Load Flags into AHRegister 1001 1111
STRING_LAHF,
/*
* LAR – Load Access Rights Byte.
* from register 0000 1111 : 0000 0010 : 11 reg1 reg2.
* from memory 0000 1111 : 0000 0010 : mod reg r/m.
*/
STRING_LAR,
// LDS – Load Pointer to DS 1100 0101 : modA,B reg r/m
STRING_LDS,
// LEA – Load Effective Address 1000 1101 : modA reg r/m
STRING_LEA,
// LEAVE – High Level Procedure Exit 1100 1001
STRING_LEAVE,
// LES – Load Pointer to ES 1100 0100 : modA,B reg r/m
STRING_LES,
// LFS – Load Pointer to FS 0000 1111 : 1011 0100 : modA reg r/m
STRING_LFS,
// LGDT – Load Global Descriptor Table Register 0000 1111 : 0000 0001 : modA 010 r/
STRING_LGDT,
// LGS – Load Pointer to GS 0000 1111 : 1011 0101 : modA reg r/m
STRING_LGS,
// LIDT – Load Interrupt Descriptor Table Register 0000 1111 : 0000 0001 : modA 011 r/m
STRING_LIDT,
/*
* LLDT – Load Local Descriptor Table Register.
* LDTR from register 0000 1111 : 0000 0000 : 11 010 reg.
* LDTR from memory 0000 1111 : 0000 0000 : mod 010 r/m.
*/
STRING_LLDT,
/*
* LMSW – Load Machine Status Word.
* from register 0000 1111 : 0000 0001 : 11 110 reg.
* from memory 0000 1111 : 0000 0001 : mod 110 r/m.
*/
STRING_LMSW,
// LOCK – Assert LOCK# Signal Prefix 1111 0000
STRING_LOCK,
// LODS/LODSB/LODSW/LODSD – Load String Operand 1010 110w
STRING_LODS,
STRING_LODSB = STRING_LODS,
STRING_LODSW = STRING_LODS,
STRING_LODD = STRING_LODS,
// LOOP – Loop Count 1110 0010 : 8-bit displacement
STRING_LOOP,
// LOOPZ/LOOPE – Loop Count while Zero/Equal 1110 0001 : 8-bit displacement
STRING_LOOPZ,
STRING_LOOPE = STRING_LOOPZ,
// LOOPNZ/LOOPNE – Loop Count while not Zero/Equal 1110 0000 : 8-bit displacement
STRING_LOOPNZ,
STRING_LOOPNE = STRING_LOOPNZ,
/*
* LSL – Load Segment Limit.
* from register 0000 1111 : 0000 0011 : 11 reg1 reg2.
* from memory 0000 1111 : 0000 0011 : mod reg r/m.
*/
STRING_LSL,
// LSS – Load Pointer to SS 0000 1111 : 1011 0010 : modA reg r/m
STRING_LSS,
/*
* LTR – Load Task Register.
* from register 0000 1111 : 0000 0000 : 11 011 reg.
* from memory 0000 1111 : 0000 0000 : mod 011 r/m.
*/
STRING_LTR,
/*
* MOV – Move Data.
* register1 to register2 1000 100w : 11 reg1 reg2.
* register2 to register1 1000 101w : 11 reg1 reg2.
* memory to reg 1000 101w : mod reg r/m.
* reg to memory 1000 100w : mod reg r/m.
* immediate to register 1100 011w : 11 000 reg : immediate data.
* immediate to register (alternate encoding) 1011 w reg : immediate data.
* immediate to memory 1100 011w : mod 000 r/m : immediate data.
* memory to AL, AX, or EAX 1010 000w : full displacement.
* AL, AX, or EAX to memory 1010 001w : full displacement.
*
* MOV – Move to/from Control Registers.
* CR0 from register 0000 1111 : 0010 0010 : -- 000 reg.
* CR2 from register 0000 1111 : 0010 0010 : -- 010reg.
* CR3 from register 0000 1111 : 0010 0010 : -- 011 reg.
* CR4 from register 0000 1111 : 0010 0010 : -- 100 reg.
* register from CR0-CR4 0000 1111 : 0010 0000 : -- eee reg.
*
* MOV – Move to/from Debug Registers.
* DR0-DR3 from register 0000 1111 : 0010 0011 : -- eee reg.
* DR4-DR5 from register 0000 1111 : 0010 0011 : -- eee reg.
* DR6-DR7 from register 0000 1111 : 0010 0011 : -- eee reg.
* register from DR6-DR7 0000 1111 : 0010 0001 : -- eee reg.
* register from DR4-DR5 0000 1111 : 0010 0001 : -- eee reg.
* register from DR0-DR3 0000 1111 : 0010 0001 : -- eee reg.
*
* MOV – Move to/from Segment Registers.
* register to segment register 1000 1110 : 11 sreg3 reg.
* register to SS 1000 1110 : 11 sreg3 reg.
* memory to segment reg 1000 1110 : mod sreg3 r/m.
* memory to SS 1000 1110 : mod sreg3 r/m.
* segment register to register 1000 1100 : 11 sreg3 reg.
* segment register to memory 1000 1100 : mod sreg3 r/m.
*/
STRING_MOV,
/*
* MOVBE – Move data after swapping bytes.
* memory to register 0000 1111 : 0011 1000:1111 0000 : mod reg r/m.
* register to memory 0000 1111 : 0011 1000:1111 0001 : mod reg r/m.
*/
STRING_MOVBE,
// MOVS/MOVSB/MOVSW/MOVSD – Move Data from String to String 1010 010w
STRING_MOVS,
STRING_MOVSB = STRING_MOVS,
STRING_MOVSW = STRING_MOVS,
STRING_MOVSD = STRING_MOVS,
/*
* MOVSX – Move with Sign-Extend.
* memory to reg 0000 1111 : 1011 111w : mod reg r/m.
*/
STRING_MOVSX,
/*
* MOVZX – Move with Zero-Extend.
* register2 to register1 0000 1111 : 1011 011w : 11 reg1 reg2.
* memory to register 0000 1111 : 1011 011w : mod reg r/m.
*/
STRING_MOVZX,
/*
* MUL – Unsigned Multiply.
* AL, AX, or EAX with register 1111 011w : 11 100 reg.
* AL, AX, or EAX with memory 1111 011w : mod 100 r/m.
*/
STRING_MUL,
/*
* NEG – Two's Complement Negation.
* register 1111 011w : 11 011 reg.
* memory 1111 011w : mod 011 r/m.
*/
STRING_NEG,
/*
* NOP – No Operation 1001 0000.
* NOP – Multi-byte No Operation1.
* register 0000 1111 0001 1111 : 11 000 reg.
* memory 0000 1111 0001 1111 : mod 000 r/m.
*/
STRING_NOP,
/*
* NOT – One's Complement Negation.
* register 1111 011w : 11 010 reg.
* memory 1111 011w : mod 010 r/m.
*/
STRING_NOT,
/*
* OR – Logical Inclusive OR.
* register1 to register2 0000 100w : 11 reg1 reg2.
* register2 to register1 0000 101w : 11 reg1 reg2.
* memory to register 0000 101w : mod reg r/m.
* register to memory 0000 100w : mod reg r/m.
* immediate to register 1000 00sw : 11 001 reg : immediate data.
* immediate to AL, AX, or EAX 0000 110w : immediate data.
* immediate to memory 1000 00sw : mod 001 r/m : immediate data.
*/
STRING_OR,
/*
* OUT – Output to Port.
* fixed port 1110 011w : port number.
* variable port 1110 111w.
*/
STRING_OUT,
// OUTS – Output to DX Port 0110 111w
STRING_OUTS,
/*
* POP – Pop a Word from the Stack.
* register 1000 1111 : 11 000 reg.
* register (alternate encoding) 0101 1 reg.
* memory 1000 1111 : mod 000 r/m.
*
* POP – Pop a Segment Register from the Stack (Note: CS cannot be sreg2 in this usage.).
* segment register DS, ES 000 sreg2 111.
* segment register SS 000 sreg2 111.
* segment register FS, GS 0000 1111: 10 sreg3 001.
*/
STRING_POP,
// POPA/POPAD – Pop All General Registers 0110 0001
STRING_POPA,
STRING_POPAD = STRING_POPA,
// POPF/POPFD – Pop Stack into FLAGS or EFLAGS Register 1001 1101
STRING_POPF,
STRING_POPFD = STRING_POPF,
/*
* PUSH – Push Operand onto the Stack.
* register 1111 1111 : 11 110 reg.
* register (alternate encoding) 0101 0 reg.
* memory 1111 1111 : mod 110 r/m.
* immediate 0110 10s0 : immediate data.
*
* PUSH – Push Segment Register onto the Stack.
* segment register CS,DS,ES,SS 000 sreg2 110.
* segment register FS,GS 0000 1111: 10 sreg3 000.
*/
STRING_PUSH,
// PUSHA/PUSHAD – Push All General Registers 0110 0000
STRING_PUSHA,
STRING_PUSHAD = STRING_PUSHA,
// PUSHF/PUSHFD – Push Flags Register onto the Stack 1001 1100
STRING_PUSHF,
STRING_PUSHFD = STRING_PUSHF,
/*
* RCL – Rotate thru Carry Left.
* register by 1 1101 000w : 11 010 reg.
* memory by 1 1101 000w : mod 010 r/m.
* register by CL 1101 001w : 11 010 reg.
* memory by CL 1101 001w : mod 010 r/m.
* register by immediate count 1100 000w : 11 010 reg : imm8 data.
* memory by immediate count 1100 000w : mod 010 r/m : imm8 data.
*/
STRING_RCL,
/*
* RCR – Rotate thru Carry Right.
* register by 1 1101 000w : 11 011 reg.
* memory by 1 1101 000w : mod 011 r/m.
* register by CL 1101 001w : 11 011 reg.
* memory by CL 1101 001w : mod 011 r/m.
* register by immediate count 1100 000w : 11 011 reg : imm8 data.
* memory by immediate count 1100 000w : mod 011 r/m : imm8 data.
*/
STRING_RCR,
// RDMSR – Read from Model-Specific Register 0000 1111 : 0011 0010
STRING_RDMSR,
// RDPMC – Read Performance Monitoring Counters 0000 1111 : 0011 0011
STRING_RDPMC,
// RDTSC – Read Time-Stamp Counter 0000 1111 : 0011 0001
STRING_RDTSC,
// RDTSCP – Read Time-Stamp Counter and Processor ID 0000 1111 : 0000 0001: 1111 1001
STRING_RDTSCP,
// REP INS – Input String 1111 0011 : 0110 110w
STRING_REP_INS,
// REP LODS – Load String 1111 0011 : 1010 110w
STRING_REP_LODS,
// REP MOVS – Move String 1111 0011 : 1010 010w
STRING_REP_MOVS,
// REP OUTS – Output String 1111 0011 : 0110 111w
STRING_REP_OUTS,
// REP STOS – Store String 1111 0011 : 1010 101w
STRING_REP_STOS,
// REPE CMPS – Compare String 1111 0011 : 1010 011w
STRING_REPE_CMPS,
// REPE SCAS – Scan String 1111 0011 : 1010 111w
STRING_REPE_SCAS,
// REPNE CMPS – Compare String 1111 0010 : 1010 011w
STRING_REPNE_CMPS,
// REPNE SCAS – Scan String 1111 0010 : 1010 111w
STRING_REPNE_SCAS,
/*
* RET – Return from Procedure (to same segment).
* no argument 1100 0011.
* adding immediate to SP 1100 0010 : 16-bit displacement.
*/
STRING_RET,
/*
* RET – Return from Procedure (to other segment).
* intersegment -> 1100 1011.
* adding immediate to SP -> 1100 1010 : 16-bit displacement.
*/
STRING_RETF,
/*
* ROL – Rotate Left.
* register by 1 1101 000w : 11 000 reg.
* memory by 1 1101 000w : mod 000 r/m.
* register by CL 1101 001w : 11 000 reg.
* memory by CL 1101 001w : mod 000 r/m.
* register by immediate count 1100 000w : 11 000 reg : imm8 data.
* memory by immediate count 1100 000w : mod 000 r/m : imm8 data.
*/
STRING_ROL,
/*
* ROR – Rotate Right.
* register by 1 1101 000w : 11 001 reg.
* memory by 1 1101 000w : mod 001 r/m.
* register by CL 1101 001w : 11 001 reg.
* memory by CL 1101 001w : mod 001 r/m.
* register by immediate count 1100 000w : 11 001 reg : imm8 data.
* memory by immediate count 1100 000w : mod 001 r/m : imm8 data.
*/
STRING_ROR,
// RSM – Resume from System Management Mode 0000 1111 : 1010 1010
STRING_RSM,
// SAHF – Store AH into Flags 1001 1110
STRING_SAHF,
// SAL – Shift Arithmetic Left same instruction as SHL
STRING_SAL,
/*
* SAR – Shift Arithmetic Right.
* register by 1 1101 000w : 11 111 reg.
* memory by 1 1101 000w : mod 111 r/m.
* register by CL 1101 001w : 11 111 reg.
* memory by CL 1101 001w : mod 111 r/m.
* register by immediate count 1100 000w : 11 111 reg : imm8 data.
* memory by immediate count 1100 000w : mod 111 r/m : imm8 data.
*/
STRING_SAR,
/*
* SBB – Integer Subtraction with Borrow.
* register1 to register2 0001 100w : 11 reg1 reg2.
* register2 to register1 0001 101w : 11 reg1 reg2.
* memory to register 0001 101w : mod reg r/m.
* register to memory 0001 100w : mod reg r/m.
* immediate to register 1000 00sw : 11 011 reg : immediate data.
* immediate to AL, AX, or EAX 0001 110w : immediate data.
* immediate to memory 1000 00sw : mod 011 r/m : immediate data.
*/
STRING_SBB,
// SCAS/SCASB/SCASW/SCASD – Scan String 1010 111w
STRING_SCAS,
STRING_SCASB = STRING_SCAS,
STRING_SCASW = STRING_SCAS,
STRING_SCASD = STRING_SCAS,
/*
* SETcc – Byte Set on Condition.
* register 0000 1111 : 1001 tttn : 11 000 reg.
* memory 0000 1111 : 1001 tttn : mod 000 r/m.
*/
STRING_SETcc,
// SGDT – Store Global Descriptor Table Register 0000 1111 : 0000 0001 : modA 000 r/m
STRING_SGDT,
/*
* SHL – Shift Left.
* register by 1 1101 000w : 11 100 reg.
* memory by 1 1101 000w : mod 100 r/m.
* register by CL 1101 001w : 11 100 reg.
* memory by CL 1101 001w : mod 100 r/m.
* register by immediate count 1100 000w : 11 100 reg : imm8 data.
* memory by immediate count 1100 000w : mod 100 r/m : imm8 data.
*/
STRING_SHL,
/*
* SHLD – Double Precision Shift Left.
* register by immediate count 0000 1111 : 1010 0100 : 11 reg2 reg1 : imm8.
* memory by immediate count 0000 1111 : 1010 0100 : mod reg r/m : imm8.
* register by CL 0000 1111 : 1010 0101 : 11 reg2 reg1.
* memory by CL 0000 1111 : 1010 0101 : mod reg r/m.
*/
STRING_SHLD,
/*
* SHR – Shift Right.
* register by 1 1101 000w : 11 101 reg.
* memory by 1 1101 000w : mod 101 r/m.
* register by CL 1101 001w : 11 101 reg.
* memory by CL 1101 001w : mod 101 r/m.
* register by immediate count 1100 000w : 11 101 reg : imm8 data.
* memory by immediate count 1100 000w : mod 101 r/m : imm8 data.
*/
STRING_SHR,
/*
* SHRD – Double Precision Shift Right.
* register by immediate count 0000 1111 : 1010 1100 : 11 reg2 reg1 : imm8.
* memory by immediate count 0000 1111 : 1010 1100 : mod reg r/m : imm8.
* register by CL 0000 1111 : 1010 1101 : 11 reg2 reg1.
* memory by CL 0000 1111 : 1010 1101 : mod reg r/m.
*/
STRING_SHRD,
// SIDT – Store Interrupt Descriptor Table Register 0000 1111 : 0000 0001 : modA 001 r/m
STRING_SIDT,
/*
* SLDT – Store Local Descriptor Table Register.
* to register 0000 1111 : 0000 0000 : 11 000 reg.
* to memory 0000 1111 : 0000 0000 : mod 000 r/m.
*/
STRING_SLDT,
/*
* SMSW – Store Machine Status Word.
* to register 0000 1111 : 0000 0001 : 11 100 reg.
* to memory 0000 1111 : 0000 0001 : mod 100 r/m.
*/
STRING_SMSW,
// STC – Set Carry Flag 1111 1001
STRING_STC,
// STD – Set Direction Flag 1111 1101
STRING_STD,
// STI – Set Interrupt Flag 1111 1011
STRING_STI,
// STOS/STOSB/STOSW/STOSD – Store String Data 1010 101w
STRING_STOS,
STRING_STOSB = STRING_STOS,
STRING_STOSW = STRING_STOS,
STRING_STOSD = STRING_STOS,
/*
* STR – Store Task Register.
* to register 0000 1111 : 0000 0000 : 11 001 reg.
* to memory 0000 1111 : 0000 0000 : mod 001 r/m.
*/
STRING_STR,
/*
* SUB – Integer Subtraction.
* register1 to register2 0010 100w : 11 reg1 reg2.
* register2 to register1 0010 101w : 11 reg1 reg2.
* memory to register 0010 101w : mod reg r/m.
* register to memory 0010 100w : mod reg r/m.
* immediate to register 1000 00sw : 11 101 reg : immediate data.
* immediate to AL, AX, or EAX 0010 110w : immediate data.
* immediate to memory 1000 00sw : mod 101 r/m : immediate data.
*/
STRING_SUB,
/*
* TEST – Logical Compare.
* register1 and register2 1000 010w : 11 reg1 reg2.
* memory and register 1000 010w : mod reg r/m.
* immediate and register 1111 011w : 11 000 reg : immediate data.
* immediate and AL, AX, or EAX 1010 100w : immediate data.
* immediate and memory 1111 011w : mod 000 r/m : immediate data.
*/
STRING_TEST,
/*
* UD0 – Undefined instruction 0000 1111 : 1111 1111. -> 0f ff.
* UD1 – Undefined instruction 0000 1111 : 1011 1001. -> 0f b9.
* UD2 – Undefined instruction 0000 1111 : 0000 1011. -> 0f 0b.
*/
STRING_UD,
/*
* VERR – Verify a Segment for Reading.
* register 0000 1111 : 0000 0000 : 11 100 reg.
* memory 0000 1111 : 0000 0000 : mod 100 r/m.
*/
STRING_VERR,
/*
* VERW – Verify a Segment for Writing.
* register 0000 1111 : 0000 0000 : 11 101 reg.
* memory 0000 1111 : 0000 0000 : mod 101 r/m.
*/
STRING_VERW,
// WAIT – Wait 1001 1011
STRING_WAIT,
// WBINVD – Writeback and Invalidate Data Cache 0000 1111 : 0000 1001
STRING_WBINVD,
// WRMSR – Write to Model-Specific Register 0000 1111 : 0011 0000
STRING_WRMSR,
/*
* XADD – Exchange and Add.
* register1, register2 0000 1111 : 1100 000w : 11 reg2 reg1.
* memory, reg 0000 1111 : 1100 000w : mod reg r/m.
*/
STRING_XADD,
/*
* XCHG – Exchange Register/Memory with Register.
* register1 with register2 1000 011w : 11 reg1 reg2.
* AX or EAX with reg 1001 0 reg.
* memory with reg 1000 011w : mod reg r/m.
* XLAT/XLATB – Table Look-up Translation 1101 0111.
*/
STRING_XCHG,
/*
* XOR – Logical Exclusive OR.
* register1 to register2 0011 000w : 11 reg1 reg2.
* register2 to register1 0011 001w : 11 reg1 reg2.
* memory to register 0011 001w : mod reg r/m.
* register to memory 0011 000w : mod reg r/m.
* immediate to register 1000 00sw : 11 110 reg : immediate data.
* immediate to AL, AX, or EAX 0011 010w : immediate data.
* immediate to memory 1000 00sw : mod 110 r/m : immediate data.
*/
STRING_XOR
} string_instrution_id;
typedef enum encoder_x86 { // se especifica el formato a encodificar / descodificar las instrucciones
ENCODER_IN_16bits,
ENCODER_IN_32bits,
ENCODER_IN_64bits
} encoder_x86;
typedef enum register_id { // se especifica los registros
REGISTER_A = 0b000, // AL, AX, EAX = 000
REGISTER_C = 0b001, // CL, CX, ECX = 001
REGISTER_B = 0b010, // BL, BX, EBX = 010
REGISTER_D = 0b011, // DL, DX, EDX = 011
REGISTER_S = 0b100, // AH, SP, ESX = 100
REGISTER_BP = 0b101, // CH, BP, EBX = 101
REGISTER_SP = 0b110, // DH, SI, ESX = 110
REGISTER_DI = 0b111, // BH, DI, EDX = 111
} register_id;
#pragma pack(push, 1)
typedef union opcode // estructura para representar los opcodes
{
struct opcode_bits_final {
uint8_t s:1; /*
* Longitud del operando(size?):
* Si s = 0, los operandos son registros de 8 bits y posiciones de memoria.
* Si s = 1, los operandos son de 16 bits o de 32 bits:
*/
uint8_t d:1; /*
* direcion:
* Si d=0, REG es la fuente, MOD R/M <- REG.
* Si d=1, REG es el destino, REG <- MOD R/M.
*/
uint8_t opcode:6; // opcode de 1 byte
} opcode_bits_final; // no siempre se da este formato en las instrucciones
struct opcode_bits {
uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1;
uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; uint8_t b8:1;
} opcode_bits; // acceso a los campos de bit a bit (para los casos en los que el campo "w", "d", "s", puedan estar en diferentes ubicaciones)
struct opcode_byte {
uint8_t byte; // acceder al opcode en forma de byte
} opcode_byte; // acceso a todos los campos en forma de byte
} opcode;
typedef struct Mod_rm { // estructura para representar el mod/rm (¿Register/Memory?)
uint8_t R_M:3; /*
* campo ¿Registro/Memoria? (reg2?).
* dependiendo de la instruccion este campo se usa como indicar de un segundo registro fuente o destino.
* En caso contrario se usa para el acceso a memoria o indicar el uso de un campo SIB.
*
* Formas con el Byte ModR/M solo para 32bits:
* |===|===|=================================.
* |MOD|R/M| Addressing Mode .
* |===|===|=================================.
* | 00|000|[ eax ] .
* | 01|000|[ eax + disp8 ] (1).
* | 10|000|[ eax + disp32 ] .
* | 11|000|register ( al / ax / eax ) (2).
* | 00|001|[ ecx ] .
* | 01|001|[ ecx + disp8 ] .
* | 10|001|[ ecx + disp32 ] .
* | 11|001|register ( cl / cx / ecx ) .
* | 00|010|[ edx ] .
* | 01|010|[ edx + disp8 ] .
* | 10|010|[ edx + disp32 ] .
* | 11|010|register ( dl / dx / edx ) .
* | 00|011|[ ebx ] .
* | 01|011|[ ebx + disp8 ] .
* | 10|011|[ ebx + disp32 ] .
* | 11|011|register ( bl / bx / ebx ) .
* | 00|100|SIB Mode (3).
* | 01|100|SIB + disp8 Mode .
* | 10|100|SIB + disp32 Mode .
* | 11|100|register ( ah / sp / esp ) .
* | 00|101|32-bit Displacement-Only Mode (4).
* | 01|101|[ ebp + disp8 ] .
* | 10|101|[ ebp + disp32 ] .
* | 11|101|register ( ch / bp / ebp ) .
* | 00|110|[ esi ] .
* | 01|110|[ esi + disp8 ] .
* | 10|110|[ esi + disp32 ] .
* | 11|110|register ( dh / si / esi ) .
* | 00|111|[ edi ] .
* | 01|111|[ edi + disp8 ] .
* | 10|111|[ edi + disp32 ] .
* | 11|111|register ( bh / di / edi ) .
* |===|===|=================================.
*/
uint8_t reg:3; /*
* campo registro (reg1?).
* - 000 -> al(si w = 0) / ax(si w = 1) / eax (32bits) / rax.
* - 001 -> cl(si w = 0) / cx(si w = 1) / ecx (32bits) / rcx.
* - 010 -> dl(si w = 0) / dx(si w = 1) / edx (32bits) / rdx.
* - 011 -> bl(si w = 0) / bx(si w = 1) / ebx (32bits) / rbx.
* - 100 -> ah(si w = 0) / sp(si w = 1) / esp (32bits) / rsp.
* - 101 -> ch(si w = 0) / bp(si w = 1) / ebp (32bits) / rbp.
* - 110 -> dh(si w = 0) / si(si w = 1) / esi (32bits) / rsi.
* - 111 -> bh(si w = 0) / di(si w = 1) / edi (32bits) / rdi.
*/
uint8_t mod:2; /*
* campo MOD:
* - 00 Modo de direccionamiento indirecto de registro o SIB sin desplazamiento (cuando R/M = 100) o