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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
|
/* $Id: DisasmFormatYasm.cpp $ */
/** @file
* VBox Disassembler - Yasm(/Nasm) Style Formatter.
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <VBox/dis.h>
#include "DisasmInternal.h"
#include <iprt/string.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static const char g_szSpaces[] =
" ";
static const char g_aszYasmRegGen8[20][5] =
{
"al\0\0", "cl\0\0", "dl\0\0", "bl\0\0", "ah\0\0", "ch\0\0", "dh\0\0", "bh\0\0", "r8b\0", "r9b\0", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b", "spl\0", "bpl\0", "sil\0", "dil\0"
};
static const char g_aszYasmRegGen16[16][5] =
{
"ax\0\0", "cx\0\0", "dx\0\0", "bx\0\0", "sp\0\0", "bp\0\0", "si\0\0", "di\0\0", "r8w\0", "r9w\0", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
};
static const char g_aszYasmRegGen1616[8][6] =
{
"bx+si", "bx+di", "bp+si", "bp+di", "si\0\0\0", "di\0\0\0", "bp\0\0\0", "bx\0\0\0"
};
static const char g_aszYasmRegGen32[16][5] =
{
"eax\0", "ecx\0", "edx\0", "ebx\0", "esp\0", "ebp\0", "esi\0", "edi\0", "r8d\0", "r9d\0", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
};
static const char g_aszYasmRegGen64[16][4] =
{
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8\0", "r9\0", "r10", "r11", "r12", "r13", "r14", "r15"
};
static const char g_aszYasmRegSeg[6][3] =
{
"es", "cs", "ss", "ds", "fs", "gs"
};
static const char g_aszYasmRegFP[8][4] =
{
"st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7"
};
static const char g_aszYasmRegMMX[8][4] =
{
"mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7"
};
static const char g_aszYasmRegXMM[16][6] =
{
"xmm0\0", "xmm1\0", "xmm2\0", "xmm3\0", "xmm4\0", "xmm5\0", "xmm6\0", "xmm7\0", "xmm8\0", "xmm9\0", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
};
static const char g_aszYasmRegCRx[16][5] =
{
"cr0\0", "cr1\0", "cr2\0", "cr3\0", "cr4\0", "cr5\0", "cr6\0", "cr7\0", "cr8\0", "cr9\0", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15"
};
static const char g_aszYasmRegDRx[16][5] =
{
"dr0\0", "dr1\0", "dr2\0", "dr3\0", "dr4\0", "dr5\0", "dr6\0", "dr7\0", "dr8\0", "dr9\0", "dr10", "dr11", "dr12", "dr13", "dr14", "dr15"
};
static const char g_aszYasmRegTRx[16][5] =
{
"tr0\0", "tr1\0", "tr2\0", "tr3\0", "tr4\0", "tr5\0", "tr6\0", "tr7\0", "tr8\0", "tr9\0", "tr10", "tr11", "tr12", "tr13", "tr14", "tr15"
};
/**
* Gets the base register name for the given parameter.
*
* @returns Pointer to the register name.
* @param pCpu The disassembler cpu state.
* @param pParam The parameter.
* @param pcchReg Where to store the length of the name.
*/
static const char *disasmFormatYasmBaseReg(PCDISCPUSTATE pCpu, PCOP_PARAMETER pParam, size_t *pcchReg)
{
switch (pParam->flags & ( USE_REG_GEN8 | USE_REG_GEN16 | USE_REG_GEN32 | USE_REG_GEN64
| USE_REG_FP | USE_REG_MMX | USE_REG_XMM | USE_REG_CR
| USE_REG_DBG | USE_REG_SEG | USE_REG_TEST))
{
case USE_REG_GEN8:
{
Assert(pParam->base.reg_gen < RT_ELEMENTS(g_aszYasmRegGen8));
const char *psz = g_aszYasmRegGen8[pParam->base.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case USE_REG_GEN16:
{
Assert(pParam->base.reg_gen < RT_ELEMENTS(g_aszYasmRegGen16));
const char *psz = g_aszYasmRegGen16[pParam->base.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case USE_REG_GEN32:
{
Assert(pParam->base.reg_gen < RT_ELEMENTS(g_aszYasmRegGen32));
const char *psz = g_aszYasmRegGen32[pParam->base.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case USE_REG_GEN64:
{
Assert(pParam->base.reg_gen < RT_ELEMENTS(g_aszYasmRegGen64));
const char *psz = g_aszYasmRegGen64[pParam->base.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case USE_REG_FP:
{
Assert(pParam->base.reg_fp < RT_ELEMENTS(g_aszYasmRegFP));
const char *psz = g_aszYasmRegFP[pParam->base.reg_fp];
*pcchReg = 3;
return psz;
}
case USE_REG_MMX:
{
Assert(pParam->base.reg_mmx < RT_ELEMENTS(g_aszYasmRegMMX));
const char *psz = g_aszYasmRegMMX[pParam->base.reg_mmx];
*pcchReg = 3;
return psz;
}
case USE_REG_XMM:
{
Assert(pParam->base.reg_xmm < RT_ELEMENTS(g_aszYasmRegXMM));
const char *psz = g_aszYasmRegXMM[pParam->base.reg_mmx];
*pcchReg = 4 + !!psz[4];
return psz;
}
case USE_REG_CR:
{
Assert(pParam->base.reg_ctrl < RT_ELEMENTS(g_aszYasmRegCRx));
const char *psz = g_aszYasmRegCRx[pParam->base.reg_ctrl];
*pcchReg = 3;
return psz;
}
case USE_REG_DBG:
{
Assert(pParam->base.reg_dbg < RT_ELEMENTS(g_aszYasmRegDRx));
const char *psz = g_aszYasmRegDRx[pParam->base.reg_dbg];
*pcchReg = 3;
return psz;
}
case USE_REG_SEG:
{
Assert(pParam->base.reg_seg < (DIS_SELREG)RT_ELEMENTS(g_aszYasmRegCRx));
const char *psz = g_aszYasmRegSeg[pParam->base.reg_seg];
*pcchReg = 2;
return psz;
}
case USE_REG_TEST:
{
Assert(pParam->base.reg_test < RT_ELEMENTS(g_aszYasmRegTRx));
const char *psz = g_aszYasmRegTRx[pParam->base.reg_test];
*pcchReg = 3;
return psz;
}
default:
AssertMsgFailed(("%#x\n", pParam->flags));
*pcchReg = 3;
return "r??";
}
}
/**
* Gets the index register name for the given parameter.
*
* @returns The index register name.
* @param pCpu The disassembler cpu state.
* @param pParam The parameter.
* @param pcchReg Where to store the length of the name.
*/
static const char *disasmFormatYasmIndexReg(PCDISCPUSTATE pCpu, PCOP_PARAMETER pParam, size_t *pcchReg)
{
switch (pCpu->addrmode)
{
case CPUMODE_16BIT:
{
Assert(pParam->index.reg_gen < RT_ELEMENTS(g_aszYasmRegGen16));
const char *psz = g_aszYasmRegGen16[pParam->index.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case CPUMODE_32BIT:
{
Assert(pParam->index.reg_gen < RT_ELEMENTS(g_aszYasmRegGen32));
const char *psz = g_aszYasmRegGen32[pParam->index.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
case CPUMODE_64BIT:
{
Assert(pParam->index.reg_gen < RT_ELEMENTS(g_aszYasmRegGen64));
const char *psz = g_aszYasmRegGen64[pParam->index.reg_gen];
*pcchReg = 2 + !!psz[2] + !!psz[3];
return psz;
}
default:
AssertMsgFailed(("%#x %#x\n", pParam->flags, pCpu->addrmode));
*pcchReg = 3;
return "r??";
}
}
/**
* Formats the current instruction in Yasm (/ Nasm) style.
*
*
* @returns The number of output characters. If this is >= cchBuf, then the content
* of pszBuf will be truncated.
* @param pCpu Pointer to the disassembler CPU state.
* @param pszBuf The output buffer.
* @param cchBuf The size of the output buffer.
* @param fFlags Format flags, see DIS_FORMAT_FLAGS_*.
* @param pfnGetSymbol Get symbol name for a jmp or call target address. Optional.
* @param pvUser User argument for pfnGetSymbol.
*/
DISDECL(size_t) DISFormatYasmEx(PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf, uint32_t fFlags,
PFNDISGETSYMBOL pfnGetSymbol, void *pvUser)
{
/*
* Input validation and massaging.
*/
AssertPtr(pCpu);
AssertPtrNull(pszBuf);
Assert(pszBuf || !cchBuf);
AssertPtrNull(pfnGetSymbol);
AssertMsg(DIS_FMT_FLAGS_IS_VALID(fFlags), ("%#x\n", fFlags));
if (fFlags & DIS_FMT_FLAGS_ADDR_COMMENT)
fFlags = (fFlags & ~DIS_FMT_FLAGS_ADDR_LEFT) | DIS_FMT_FLAGS_ADDR_RIGHT;
if (fFlags & DIS_FMT_FLAGS_BYTES_COMMENT)
fFlags = (fFlags & ~DIS_FMT_FLAGS_BYTES_LEFT) | DIS_FMT_FLAGS_BYTES_RIGHT;
PCOPCODE const pOp = pCpu->pCurInstr;
/*
* Output macros
*/
char *pszDst = pszBuf;
size_t cchDst = cchBuf;
size_t cchOutput = 0;
#define PUT_C(ch) \
do { \
cchOutput++; \
if (cchDst > 1) \
{ \
cchDst--; \
*pszDst++ = (ch); \
} \
} while (0)
#define PUT_STR(pszSrc, cchSrc) \
do { \
cchOutput += (cchSrc); \
if (cchDst > (cchSrc)) \
{ \
memcpy(pszDst, (pszSrc), (cchSrc)); \
pszDst += (cchSrc); \
cchDst -= (cchSrc); \
} \
else if (cchDst > 1) \
{ \
memcpy(pszDst, (pszSrc), cchDst - 1); \
pszDst += cchDst - 1; \
cchDst = 1; \
} \
} while (0)
#define PUT_SZ(sz) \
PUT_STR((sz), sizeof(sz) - 1)
#define PUT_SZ_STRICT(szStrict, szRelaxed) \
do { if (fFlags & DIS_FMT_FLAGS_STRICT) PUT_SZ(szStrict); else PUT_SZ(szRelaxed); } while (0)
#define PUT_PSZ(psz) \
do { const size_t cchTmp = strlen(psz); PUT_STR((psz), cchTmp); } while (0)
#define PUT_NUM(cch, fmt, num) \
do { \
cchOutput += (cch); \
if (cchDst > 1) \
{ \
const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
pszDst += cchTmp; \
cchDst -= cchTmp; \
Assert(cchTmp == (cch) || cchDst == 1); \
} \
} while (0)
/** @todo add two flags for choosing between %X / %x and h / 0x. */
#define PUT_NUM_8(num) PUT_NUM(4, "0%02xh", (uint8_t)(num))
#define PUT_NUM_16(num) PUT_NUM(6, "0%04xh", (uint16_t)(num))
#define PUT_NUM_32(num) PUT_NUM(10, "0%08xh", (uint32_t)(num))
#define PUT_NUM_64(num) PUT_NUM(18, "0%016RX64h", (uint64_t)(num))
#define PUT_NUM_SIGN(cch, fmt, num, stype, utype) \
do { \
if ((stype)(num) >= 0) \
{ \
PUT_C('+'); \
PUT_NUM(cch, fmt, (utype)(num)); \
} \
else \
{ \
PUT_C('-'); \
PUT_NUM(cch, fmt, (utype)-(stype)(num)); \
} \
} while (0)
#define PUT_NUM_S8(num) PUT_NUM_SIGN(4, "0%02xh", num, int8_t, uint8_t)
#define PUT_NUM_S16(num) PUT_NUM_SIGN(6, "0%04xh", num, int16_t, uint16_t)
#define PUT_NUM_S32(num) PUT_NUM_SIGN(10, "0%08xh", num, int32_t, uint32_t)
#define PUT_NUM_S64(num) PUT_NUM_SIGN(18, "0%016RX64h", num, int64_t, uint64_t)
/*
* The address?
*/
if (fFlags & DIS_FMT_FLAGS_ADDR_LEFT)
{
#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
if (pCpu->opaddr >= _4G)
PUT_NUM(9, "%08x`", (uint32_t)(pCpu->opaddr >> 32));
#endif
PUT_NUM(8, "%08x", (uint32_t)pCpu->opaddr);
PUT_C(' ');
}
/*
* The opcode bytes?
*/
if (fFlags & DIS_FMT_FLAGS_BYTES_LEFT)
{
size_t cchTmp = disFormatBytes(pCpu, pszDst, cchDst, fFlags);
cchOutput += cchTmp;
if (cchDst > 1)
{
if (cchTmp <= cchDst)
{
cchDst -= cchTmp;
pszDst += cchTmp;
}
else
{
pszDst += cchDst - 1;
cchDst = 1;
}
}
/* Some padding to align the instruction. */
size_t cchPadding = (7 * (2 + !!(fFlags & DIS_FMT_FLAGS_BYTES_SPACED)))
+ !!(fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS) * 2
+ 2;
cchPadding = cchTmp + 1 >= cchPadding ? 1 : cchPadding - cchTmp;
PUT_STR(g_szSpaces, cchPadding);
}
/*
* Filter out invalid opcodes first as they need special
* treatment. UD2 is an exception and should be handled normally.
*/
size_t const offInstruction = cchOutput;
if ( pOp->opcode == OP_INVALID
|| ( pOp->opcode == OP_ILLUD2
&& (pCpu->prefix & PREFIX_LOCK)))
{
}
else
{
/*
* Prefixes
*/
if (pCpu->prefix & PREFIX_LOCK)
PUT_SZ("lock ");
if(pCpu->prefix & PREFIX_REP)
PUT_SZ("rep ");
else if(pCpu->prefix & PREFIX_REPNE)
PUT_SZ("repne ");
/*
* Adjust the format string to the correct mnemonic
* or to avoid things the assembler cannot handle correctly.
*/
char szTmpFmt[48];
const char *pszFmt = pOp->pszOpcode;
switch (pOp->opcode)
{
case OP_JECXZ:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "jcxz %Jb" : pCpu->opmode == CPUMODE_32BIT ? "jecxz %Jb" : "jrcxz %Jb";
break;
case OP_PUSHF:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "pushfw" : pCpu->opmode == CPUMODE_32BIT ? "pushfd" : "pushfq";
break;
case OP_POPF:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "popfw" : pCpu->opmode == CPUMODE_32BIT ? "popfd" : "popfq";
break;
case OP_PUSHA:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "pushaw" : "pushad";
break;
case OP_POPA:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "popaw" : "popad";
break;
case OP_INSB:
pszFmt = "insb";
break;
case OP_INSWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "insw" : pCpu->opmode == CPUMODE_32BIT ? "insd" : "insq";
break;
case OP_OUTSB:
pszFmt = "outsb";
break;
case OP_OUTSWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "outsw" : pCpu->opmode == CPUMODE_32BIT ? "outsd" : "outsq";
break;
case OP_MOVSB:
pszFmt = "movsb";
break;
case OP_MOVSWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "movsw" : pCpu->opmode == CPUMODE_32BIT ? "movsd" : "movsq";
break;
case OP_CMPSB:
pszFmt = "cmpsb";
break;
case OP_CMPWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "cmpsw" : pCpu->opmode == CPUMODE_32BIT ? "cmpsd" : "cmpsq";
break;
case OP_SCASB:
pszFmt = "scasb";
break;
case OP_SCASWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "scasw" : pCpu->opmode == CPUMODE_32BIT ? "scasd" : "scasq";
break;
case OP_LODSB:
pszFmt = "lodsb";
break;
case OP_LODSWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "lodsw" : pCpu->opmode == CPUMODE_32BIT ? "lodsd" : "lodsq";
break;
case OP_STOSB:
pszFmt = "stosb";
break;
case OP_STOSWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "stosw" : pCpu->opmode == CPUMODE_32BIT ? "stosd" : "stosq";
break;
case OP_CBW:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "cbw" : pCpu->opmode == CPUMODE_32BIT ? "cwde" : "cdqe";
break;
case OP_CWD:
pszFmt = pCpu->opmode == CPUMODE_16BIT ? "cwd" : pCpu->opmode == CPUMODE_32BIT ? "cdq" : "cqo";
break;
case OP_SHL:
Assert(pszFmt[3] == '/');
pszFmt += 4;
break;
case OP_XLAT:
pszFmt = "xlatb";
break;
case OP_INT3:
pszFmt = "int3";
break;
/*
* Don't know how to tell yasm to generate complicated nop stuff, so 'db' it.
*/
case OP_NOP:
if (pCpu->opcode == 0x90)
/* fine, fine */;
else if (pszFmt[sizeof("nop %Ev") - 1] == '/' && pszFmt[sizeof("nop %Ev")] == 'p')
pszFmt = "prefetch %Eb";
else if (pCpu->opcode == 0x1f)
{
Assert(pCpu->opsize >= 3);
PUT_SZ("db 00fh, 01fh,");
PUT_NUM_8(pCpu->ModRM.u);
for (unsigned i = 3; i < pCpu->opsize; i++)
{
PUT_C(',');
PUT_NUM_8(0x90); ///@todo fixme.
}
pszFmt = "";
}
break;
default:
/* ST(X) -> stX (floating point) */
if (*pszFmt == 'f' && strchr(pszFmt, '('))
{
char *pszFmtDst = szTmpFmt;
char ch;
do
{
ch = *pszFmt++;
if (ch == 'S' && pszFmt[0] == 'T' && pszFmt[1] == '(')
{
*pszFmtDst++ = 's';
*pszFmtDst++ = 't';
pszFmt += 2;
ch = *pszFmt;
Assert(pszFmt[1] == ')');
pszFmt += 2;
*pszFmtDst++ = ch;
}
else
*pszFmtDst++ = ch;
} while (ch != '\0');
pszFmt = szTmpFmt;
}
break;
/*
* Horrible hacks.
*/
case OP_FLD:
if (pCpu->opcode == 0xdb) /* m80fp workaround. */
*(int *)&pCpu->param1.param &= ~0x1f; /* make it pure OP_PARM_M */
break;
case OP_LAR: /* hack w -> v, probably not correct. */
*(int *)&pCpu->param2.param &= ~0x1f;
*(int *)&pCpu->param2.param |= OP_PARM_v;
break;
}
/*
* Formatting context and associated macros.
*/
PCOP_PARAMETER pParam = &pCpu->param1;
int iParam = 1;
#define PUT_FAR() \
do { \
if ( OP_PARM_VSUBTYPE(pParam->param) == OP_PARM_p \
&& pOp->opcode != OP_LDS /* table bugs? */ \
&& pOp->opcode != OP_LES \
&& pOp->opcode != OP_LFS \
&& pOp->opcode != OP_LGS \
&& pOp->opcode != OP_LSS ) \
PUT_SZ("far "); \
} while (0)
/** @todo mov ah,ch ends up with a byte 'override'... - check if this wasn't fixed. */
/** @todo drop the work/dword/qword override when the src/dst is a register (except for movsx/movzx). */
#define PUT_SIZE_OVERRIDE() \
do { \
switch (OP_PARM_VSUBTYPE(pParam->param)) \
{ \
case OP_PARM_v: \
switch (pCpu->opmode) \
{ \
case CPUMODE_16BIT: PUT_SZ("word "); break; \
case CPUMODE_32BIT: PUT_SZ("dword "); break; \
case CPUMODE_64BIT: PUT_SZ("qword "); break; \
default: break; \
} \
break; \
case OP_PARM_b: PUT_SZ("byte "); break; \
case OP_PARM_w: PUT_SZ("word "); break; \
case OP_PARM_d: PUT_SZ("dword "); break; \
case OP_PARM_q: PUT_SZ("qword "); break; \
case OP_PARM_dq: \
if (OP_PARM_VTYPE(pParam->param) != OP_PARM_W) /* these are 128 bit, pray they are all unambiguous.. */ \
PUT_SZ("qword "); \
break; \
case OP_PARM_p: break; /* see PUT_FAR */ \
case OP_PARM_s: if (pParam->flags & USE_REG_FP) PUT_SZ("tword "); break; /* ?? */ \
case OP_PARM_z: break; \
case OP_PARM_NONE: \
if ( OP_PARM_VTYPE(pParam->param) == OP_PARM_M \
&& ((pParam->flags & USE_REG_FP) || pOp->opcode == OP_FLD)) \
PUT_SZ("tword "); \
break; \
default: break; /*no pointer type specified/necessary*/ \
} \
} while (0)
static const char s_szSegPrefix[6][4] = { "es:", "cs:", "ss:", "ds:", "fs:", "gs:" };
#define PUT_SEGMENT_OVERRIDE() \
do { \
if (pCpu->prefix & PREFIX_SEG) \
PUT_STR(s_szSegPrefix[pCpu->enmPrefixSeg], 3); \
} while (0)
/*
* Segment prefixing for instructions that doesn't do memory access.
*/
if ( (pCpu->prefix & PREFIX_SEG)
&& !DIS_IS_EFFECTIVE_ADDR(pCpu->param1.flags)
&& !DIS_IS_EFFECTIVE_ADDR(pCpu->param2.flags)
&& !DIS_IS_EFFECTIVE_ADDR(pCpu->param3.flags))
{
PUT_STR(s_szSegPrefix[pCpu->enmPrefixSeg], 2);
PUT_C(' ');
}
/*
* The formatting loop.
*/
RTINTPTR off;
char szSymbol[128];
char ch;
while ((ch = *pszFmt++) != '\0')
{
if (ch == '%')
{
ch = *pszFmt++;
switch (ch)
{
/*
* ModRM - Register only.
*/
case 'C': /* Control register (ParseModRM / UseModRM). */
case 'D': /* Debug register (ParseModRM / UseModRM). */
case 'G': /* ModRM selects general register (ParseModRM / UseModRM). */
case 'S': /* ModRM byte selects a segment register (ParseModRM / UseModRM). */
case 'T': /* ModRM byte selects a test register (ParseModRM / UseModRM). */
case 'V': /* ModRM byte selects an XMM/SSE register (ParseModRM / UseModRM). */
case 'P': /* ModRM byte selects MMX register (ParseModRM / UseModRM). */
{
pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
Assert(!(pParam->flags & (USE_INDEX | USE_SCALE) /* No SIB here... */));
Assert(!(pParam->flags & (USE_DISPLACEMENT8 | USE_DISPLACEMENT16 | USE_DISPLACEMENT32 | USE_DISPLACEMENT64 | USE_RIPDISPLACEMENT32)));
size_t cchReg;
const char *pszReg = disasmFormatYasmBaseReg(pCpu, pParam, &cchReg);
PUT_STR(pszReg, cchReg);
break;
}
/*
* ModRM - Register or memory.
*/
case 'E': /* ModRM specifies parameter (ParseModRM / UseModRM / UseSIB). */
case 'Q': /* ModRM byte selects MMX register or memory address (ParseModRM / UseModRM). */
case 'R': /* ModRM byte may only refer to a general register (ParseModRM / UseModRM). */
case 'W': /* ModRM byte selects an XMM/SSE register or a memory address (ParseModRM / UseModRM). */
case 'M': /* ModRM may only refer to memory (ParseModRM / UseModRM). */
{
pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
PUT_FAR();
if (DIS_IS_EFFECTIVE_ADDR(pParam->flags))
{
/* Work around mov seg,[mem16] and mov [mem16],seg as these always make a 16-bit mem
while the register variants deals with 16, 32 & 64 in the normal fashion. */
if ( pParam->param != OP_PARM_Ev
|| pOp->opcode != OP_MOV
|| ( pOp->param1 != OP_PARM_Sw
&& pOp->param2 != OP_PARM_Sw))
PUT_SIZE_OVERRIDE();
PUT_C('[');
}
if ( (fFlags & DIS_FMT_FLAGS_STRICT)
&& (pParam->flags & (USE_DISPLACEMENT8 | USE_DISPLACEMENT16 | USE_DISPLACEMENT32 | USE_DISPLACEMENT64 | USE_RIPDISPLACEMENT32)))
{
if ( (pParam->flags & USE_DISPLACEMENT8)
&& !pParam->disp8)
PUT_SZ("byte ");
else if ( (pParam->flags & USE_DISPLACEMENT16)
&& (int8_t)pParam->disp16 == (int16_t)pParam->disp16)
PUT_SZ("word ");
else if ( (pParam->flags & USE_DISPLACEMENT32)
&& (int8_t)pParam->disp32 == (int32_t)pParam->disp32)
PUT_SZ("dword ");
else if ( (pParam->flags & USE_DISPLACEMENT64)
&& (int8_t)pParam->disp64 == (int64_t)pParam->disp32)
PUT_SZ("qword ");
}
if (DIS_IS_EFFECTIVE_ADDR(pParam->flags))
PUT_SEGMENT_OVERRIDE();
bool fBase = (pParam->flags & USE_BASE) /* When exactly is USE_BASE supposed to be set? disasmModRMReg doesn't set it. */
|| ( (pParam->flags & (USE_REG_GEN8 | USE_REG_GEN16 | USE_REG_GEN32 | USE_REG_GEN64))
&& !DIS_IS_EFFECTIVE_ADDR(pParam->flags));
if (fBase)
{
size_t cchReg;
const char *pszReg = disasmFormatYasmBaseReg(pCpu, pParam, &cchReg);
PUT_STR(pszReg, cchReg);
}
if (pParam->flags & USE_INDEX)
{
if (fBase)
PUT_C('+');
size_t cchReg;
const char *pszReg = disasmFormatYasmIndexReg(pCpu, pParam, &cchReg);
PUT_STR(pszReg, cchReg);
if (pParam->flags & USE_SCALE)
{
PUT_C('*');
PUT_C('0' + pParam->scale);
}
}
else
Assert(!(pParam->flags & USE_SCALE));
if (pParam->flags & (USE_DISPLACEMENT8 | USE_DISPLACEMENT16 | USE_DISPLACEMENT32 | USE_DISPLACEMENT64 | USE_RIPDISPLACEMENT32))
{
int64_t off;
if (pParam->flags & USE_DISPLACEMENT8)
off = pParam->disp8;
else if (pParam->flags & USE_DISPLACEMENT16)
off = pParam->disp16;
else if (pParam->flags & (USE_DISPLACEMENT32 | USE_RIPDISPLACEMENT32))
off = pParam->disp32;
else if (pParam->flags & USE_DISPLACEMENT64)
off = pParam->disp64;
else
{
AssertFailed();
off = 0;
}
if (fBase || (pParam->flags & USE_INDEX))
{
PUT_C(off >= 0 ? '+' : '-');
if (off < 0)
off = -off;
}
if (pParam->flags & USE_DISPLACEMENT8)
PUT_NUM_8( off);
else if (pParam->flags & USE_DISPLACEMENT16)
PUT_NUM_16(off);
else if (pParam->flags & USE_DISPLACEMENT32)
PUT_NUM_32(off);
else if (pParam->flags & USE_DISPLACEMENT64)
PUT_NUM_64(off);
else
{
PUT_NUM_32(off);
PUT_SZ(" wrt rip"); //??
}
}
if (DIS_IS_EFFECTIVE_ADDR(pParam->flags))
PUT_C(']');
break;
}
case 'F': /* Eflags register (0 - popf/pushf only, avoided in adjustments above). */
AssertFailed();
break;
case 'I': /* Immediate data (ParseImmByte, ParseImmByteSX, ParseImmV, ParseImmUshort, ParseImmZ). */
Assert(*pszFmt == 'b' || *pszFmt == 'v' || *pszFmt == 'w' || *pszFmt == 'z'); pszFmt++;
switch (pParam->flags & ( USE_IMMEDIATE8 | USE_IMMEDIATE16 | USE_IMMEDIATE32 | USE_IMMEDIATE64
| USE_IMMEDIATE16_SX8 | USE_IMMEDIATE32_SX8 | USE_IMMEDIATE64_SX8))
{
case USE_IMMEDIATE8:
if ( (fFlags & DIS_FMT_FLAGS_STRICT)
&& ( (pOp->param1 >= OP_PARM_REG_GEN8_START && pOp->param1 <= OP_PARM_REG_GEN8_END)
|| (pOp->param2 >= OP_PARM_REG_GEN8_START && pOp->param2 <= OP_PARM_REG_GEN8_END))
)
PUT_SZ("strict byte ");
PUT_NUM_8(pParam->parval);
break;
case USE_IMMEDIATE16:
if ( pCpu->mode != pCpu->opmode
|| ( (fFlags & DIS_FMT_FLAGS_STRICT)
&& ( (int8_t)pParam->parval == (int16_t)pParam->parval
|| (pOp->param1 >= OP_PARM_REG_GEN16_START && pOp->param1 <= OP_PARM_REG_GEN16_END)
|| (pOp->param2 >= OP_PARM_REG_GEN16_START && pOp->param2 <= OP_PARM_REG_GEN16_END))
)
)
{
if (OP_PARM_VSUBTYPE(pParam->param) == OP_PARM_b)
PUT_SZ_STRICT("strict byte ", "byte ");
else if (OP_PARM_VSUBTYPE(pParam->param) == OP_PARM_v)
PUT_SZ_STRICT("strict word ", "word ");
}
PUT_NUM_16(pParam->parval);
break;
case USE_IMMEDIATE16_SX8:
PUT_SZ_STRICT("strict byte ", "byte ");
PUT_NUM_16(pParam->parval);
break;
case USE_IMMEDIATE32:
if ( pCpu->opmode != (pCpu->mode == CPUMODE_16BIT ? CPUMODE_16BIT : CPUMODE_32BIT) /* not perfect */
|| ( (fFlags & DIS_FMT_FLAGS_STRICT)
&& ( (int8_t)pParam->parval == (int32_t)pParam->parval
|| (pOp->param1 >= OP_PARM_REG_GEN32_START && pOp->param1 <= OP_PARM_REG_GEN32_END)
|| (pOp->param2 >= OP_PARM_REG_GEN32_START && pOp->param2 <= OP_PARM_REG_GEN32_END))
)
)
{
if (OP_PARM_VSUBTYPE(pParam->param) == OP_PARM_b)
PUT_SZ_STRICT("strict byte ", "byte ");
else if (OP_PARM_VSUBTYPE(pParam->param) == OP_PARM_v)
PUT_SZ_STRICT("strict dword ", "dword ");
}
PUT_NUM_32(pParam->parval);
break;
case USE_IMMEDIATE32_SX8:
PUT_SZ_STRICT("strict byte ", "byte ");
PUT_NUM_32(pParam->parval);
break;
case USE_IMMEDIATE64_SX8:
PUT_SZ_STRICT("strict byte ", "byte ");
PUT_NUM_64(pParam->parval);
break;
case USE_IMMEDIATE64:
PUT_NUM_64(pParam->parval);
break;
default:
AssertFailed();
break;
}
break;
case 'J': /* Relative jump offset (ParseImmBRel + ParseImmVRel). */
{
int32_t offDisplacement;
Assert(iParam == 1);
bool fPrefix = (fFlags & DIS_FMT_FLAGS_STRICT)
&& pOp->opcode != OP_CALL
&& pOp->opcode != OP_LOOP
&& pOp->opcode != OP_LOOPE
&& pOp->opcode != OP_LOOPNE
&& pOp->opcode != OP_JECXZ;
if (pOp->opcode == OP_CALL)
fFlags &= ~DIS_FMT_FLAGS_RELATIVE_BRANCH;
if (pParam->flags & USE_IMMEDIATE8_REL)
{
if (fPrefix)
PUT_SZ("short ");
offDisplacement = (int8_t)pParam->parval;
Assert(*pszFmt == 'b'); pszFmt++;
if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
PUT_NUM_S8(offDisplacement);
}
else if (pParam->flags & USE_IMMEDIATE16_REL)
{
if (fPrefix)
PUT_SZ("near ");
offDisplacement = (int16_t)pParam->parval;
Assert(*pszFmt == 'v'); pszFmt++;
if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
PUT_NUM_S16(offDisplacement);
}
else
{
if (fPrefix)
PUT_SZ("near ");
offDisplacement = (int32_t)pParam->parval;
Assert(pParam->flags & (USE_IMMEDIATE32_REL|USE_IMMEDIATE64_REL));
Assert(*pszFmt == 'v'); pszFmt++;
if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
PUT_NUM_S32(offDisplacement);
}
if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
PUT_SZ(" (");
RTUINTPTR uTrgAddr = pCpu->opaddr + pCpu->opsize + offDisplacement;
if (pCpu->mode == CPUMODE_16BIT)
PUT_NUM_16(uTrgAddr);
else if (pCpu->mode == CPUMODE_32BIT)
PUT_NUM_32(uTrgAddr);
else
PUT_NUM_64(uTrgAddr);
if (pfnGetSymbol)
{
int rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), uTrgAddr, szSymbol, sizeof(szSymbol), &off, pvUser);
if (RT_SUCCESS(rc))
{
PUT_SZ(" [");
PUT_PSZ(szSymbol);
if (off != 0)
{
if ((int8_t)off == off)
PUT_NUM_S8(off);
else if ((int16_t)off == off)
PUT_NUM_S16(off);
else if ((int32_t)off == off)
PUT_NUM_S32(off);
else
PUT_NUM_S64(off);
}
PUT_C(']');
}
}
if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
PUT_C(')');
break;
}
case 'A': /* Direct (jump/call) address (ParseImmAddr). */
{
Assert(*pszFmt == 'p'); pszFmt++;
PUT_FAR();
PUT_SIZE_OVERRIDE();
PUT_SEGMENT_OVERRIDE();
int rc = VERR_SYMBOL_NOT_FOUND;
switch (pParam->flags & (USE_IMMEDIATE_ADDR_16_16 | USE_IMMEDIATE_ADDR_16_32 | USE_DISPLACEMENT64 | USE_DISPLACEMENT32 | USE_DISPLACEMENT16))
{
case USE_IMMEDIATE_ADDR_16_16:
PUT_NUM_16(pParam->parval >> 16);
PUT_C(':');
PUT_NUM_16(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_VALUE(pParam->parval >> 16), (uint16_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_IMMEDIATE_ADDR_16_32:
PUT_NUM_16(pParam->parval >> 32);
PUT_C(':');
PUT_NUM_32(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_VALUE(pParam->parval >> 16), (uint32_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT16:
PUT_NUM_16(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint16_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT32:
PUT_NUM_32(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint32_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT64:
PUT_NUM_64(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint64_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
default:
AssertFailed();
break;
}
if (RT_SUCCESS(rc))
{
PUT_SZ(" [");
PUT_PSZ(szSymbol);
if (off != 0)
{
if ((int8_t)off == off)
PUT_NUM_S8(off);
else if ((int16_t)off == off)
PUT_NUM_S16(off);
else if ((int32_t)off == off)
PUT_NUM_S32(off);
else
PUT_NUM_S64(off);
}
PUT_C(']');
}
break;
}
case 'O': /* No ModRM byte (ParseImmAddr). */
{
Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
PUT_FAR();
PUT_SIZE_OVERRIDE();
PUT_C('[');
PUT_SEGMENT_OVERRIDE();
int rc = VERR_SYMBOL_NOT_FOUND;
switch (pParam->flags & (USE_IMMEDIATE_ADDR_16_16 | USE_IMMEDIATE_ADDR_16_32 | USE_DISPLACEMENT64 | USE_DISPLACEMENT32 | USE_DISPLACEMENT16))
{
case USE_IMMEDIATE_ADDR_16_16:
PUT_NUM_16(pParam->parval >> 16);
PUT_C(':');
PUT_NUM_16(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_VALUE(pParam->parval >> 16), (uint16_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_IMMEDIATE_ADDR_16_32:
PUT_NUM_16(pParam->parval >> 32);
PUT_C(':');
PUT_NUM_32(pParam->parval);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_VALUE(pParam->parval >> 16), (uint32_t)pParam->parval, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT16:
PUT_NUM_16(pParam->disp16);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint16_t)pParam->disp16, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT32:
PUT_NUM_32(pParam->disp32);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint32_t)pParam->disp32, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
case USE_DISPLACEMENT64:
PUT_NUM_64(pParam->disp64);
if (pfnGetSymbol)
rc = pfnGetSymbol(pCpu, DIS_FMT_SEL_FROM_REG(DIS_SELREG_CS), (uint64_t)pParam->disp64, szSymbol, sizeof(szSymbol), &off, pvUser);
break;
default:
AssertFailed();
break;
}
PUT_C(']');
if (RT_SUCCESS(rc))
{
PUT_SZ(" (");
PUT_PSZ(szSymbol);
if (off != 0)
{
if ((int8_t)off == off)
PUT_NUM_S8(off);
else if ((int16_t)off == off)
PUT_NUM_S16(off);
else if ((int32_t)off == off)
PUT_NUM_S32(off);
else
PUT_NUM_S64(off);
}
PUT_C(')');
}
break;
}
case 'X': /* DS:SI (ParseXb, ParseXv). */
case 'Y': /* ES:DI (ParseYb, ParseYv). */
{
Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
PUT_FAR();
PUT_SIZE_OVERRIDE();
PUT_C('[');
if (pParam->flags & USE_POINTER_DS_BASED)
PUT_SZ("ds:");
else
PUT_SZ("es:");
size_t cchReg;
const char *pszReg = disasmFormatYasmBaseReg(pCpu, pParam, &cchReg);
PUT_STR(pszReg, cchReg);
PUT_C(']');
break;
}
case 'e': /* Register based on operand size (e.g. %eAX) (ParseFixedReg). */
{
Assert(RT_C_IS_ALPHA(pszFmt[0]) && RT_C_IS_ALPHA(pszFmt[1]) && !RT_C_IS_ALPHA(pszFmt[2])); pszFmt += 2;
size_t cchReg;
const char *pszReg = disasmFormatYasmBaseReg(pCpu, pParam, &cchReg);
PUT_STR(pszReg, cchReg);
break;
}
default:
AssertMsgFailed(("%c%s!\n", ch, pszFmt));
break;
}
AssertMsg(*pszFmt == ',' || *pszFmt == '\0', ("%c%s\n", ch, pszFmt));
}
else
{
PUT_C(ch);
if (ch == ',')
{
Assert(*pszFmt != ' ');
PUT_C(' ');
switch (++iParam)
{
case 2: pParam = &pCpu->param2; break;
case 3: pParam = &pCpu->param3; break;
default: pParam = NULL; break;
}
}
}
} /* while more to format */
}
/*
* Any additional output to the right of the instruction?
*/
if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
{
/* some up front padding. */
size_t cchPadding = cchOutput - offInstruction;
cchPadding = cchPadding + 1 >= 42 ? 1 : 42 - cchPadding;
PUT_STR(g_szSpaces, cchPadding);
/* comment? */
if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
PUT_SZ(";");
/*
* The address?
*/
if (fFlags & DIS_FMT_FLAGS_ADDR_RIGHT)
{
PUT_C(' ');
#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
if (pCpu->opaddr >= _4G)
PUT_NUM(9, "%08x`", (uint32_t)(pCpu->opaddr >> 32));
#endif
PUT_NUM(8, "%08x", (uint32_t)pCpu->opaddr);
}
/*
* Opcode bytes?
*/
if (fFlags & DIS_FMT_FLAGS_BYTES_RIGHT)
{
PUT_C(' ');
size_t cchTmp = disFormatBytes(pCpu, pszDst, cchDst, fFlags);
cchOutput += cchTmp;
if (cchTmp >= cchDst)
cchTmp = cchDst - (cchDst != 0);
cchDst -= cchTmp;
pszDst += cchTmp;
}
}
/*
* Terminate it - on overflow we'll have reserved one byte for this.
*/
if (cchDst > 0)
*pszDst = '\0';
else
Assert(!cchBuf);
/* clean up macros */
#undef PUT_PSZ
#undef PUT_SZ
#undef PUT_STR
#undef PUT_C
return cchOutput;
}
/**
* Formats the current instruction in Yasm (/ Nasm) style.
*
* This is a simplified version of DISFormatYasmEx() provided for your convenience.
*
*
* @returns The number of output characters. If this is >= cchBuf, then the content
* of pszBuf will be truncated.
* @param pCpu Pointer to the disassembler CPU state.
* @param pszBuf The output buffer.
* @param cchBuf The size of the output buffer.
*/
DISDECL(size_t) DISFormatYasm(PCDISCPUSTATE pCpu, char *pszBuf, size_t cchBuf)
{
return DISFormatYasmEx(pCpu, pszBuf, cchBuf, 0 /* fFlags */, NULL /* pfnGetSymbol */, NULL /* pvUser */);
}
|