VirtualBox

source: vbox/trunk/src/VBox/Disassembler/DisasmFormatYasm.cpp@ 61409

Last change on this file since 61409 was 60442, checked in by vboxsync, 9 years ago

DISFormatYasmEx: More insb prefix hacking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.1 KB
Line 
1/* $Id: DisasmFormatYasm.cpp 60442 2016-04-11 21:30:08Z vboxsync $ */
2/** @file
3 * VBox Disassembler - Yasm(/Nasm) Style Formatter.
4 */
5
6/*
7 * Copyright (C) 2008-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/dis.h>
23#include "DisasmInternal.h"
24#include <iprt/string.h>
25#include <iprt/assert.h>
26#include <iprt/ctype.h>
27
28
29/*********************************************************************************************************************************
30* Global Variables *
31*********************************************************************************************************************************/
32static const char g_szSpaces[] =
33" ";
34static const char g_aszYasmRegGen8[20][5] =
35{
36 "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"
37};
38static const char g_aszYasmRegGen16[16][5] =
39{
40 "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"
41};
42static const char g_aszYasmRegGen1616[8][6] =
43{
44 "bx+si", "bx+di", "bp+si", "bp+di", "si\0\0\0", "di\0\0\0", "bp\0\0\0", "bx\0\0\0"
45};
46static const char g_aszYasmRegGen32[16][5] =
47{
48 "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"
49};
50static const char g_aszYasmRegGen64[16][4] =
51{
52 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8\0", "r9\0", "r10", "r11", "r12", "r13", "r14", "r15"
53};
54static const char g_aszYasmRegSeg[6][3] =
55{
56 "es", "cs", "ss", "ds", "fs", "gs"
57};
58static const char g_aszYasmRegFP[8][4] =
59{
60 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7"
61};
62static const char g_aszYasmRegMMX[8][4] =
63{
64 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7"
65};
66static const char g_aszYasmRegXMM[16][6] =
67{
68 "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"
69};
70static const char g_aszYasmRegYMM[16][6] =
71{
72 "ymm0\0", "ymm1\0", "ymm2\0", "ymm3\0", "ymm4\0", "ymm5\0", "ymm6\0", "ymm7\0", "ymm8\0", "ymm9\0", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15"
73};
74static const char g_aszYasmRegCRx[16][5] =
75{
76 "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"
77};
78static const char g_aszYasmRegDRx[16][5] =
79{
80 "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"
81};
82static const char g_aszYasmRegTRx[16][5] =
83{
84 "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"
85};
86
87
88
89/**
90 * Gets the base register name for the given parameter.
91 *
92 * @returns Pointer to the register name.
93 * @param pDis The disassembler state.
94 * @param pParam The parameter.
95 * @param pcchReg Where to store the length of the name.
96 */
97static const char *disasmFormatYasmBaseReg(PCDISSTATE pDis, PCDISOPPARAM pParam, size_t *pcchReg)
98{
99 switch (pParam->fUse & ( DISUSE_REG_GEN8 | DISUSE_REG_GEN16 | DISUSE_REG_GEN32 | DISUSE_REG_GEN64
100 | DISUSE_REG_FP | DISUSE_REG_MMX | DISUSE_REG_XMM | DISUSE_REG_YMM
101 | DISUSE_REG_CR | DISUSE_REG_DBG | DISUSE_REG_SEG | DISUSE_REG_TEST))
102
103 {
104 case DISUSE_REG_GEN8:
105 {
106 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen8));
107 const char *psz = g_aszYasmRegGen8[pParam->Base.idxGenReg];
108 *pcchReg = 2 + !!psz[2] + !!psz[3];
109 return psz;
110 }
111
112 case DISUSE_REG_GEN16:
113 {
114 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen16));
115 const char *psz = g_aszYasmRegGen16[pParam->Base.idxGenReg];
116 *pcchReg = 2 + !!psz[2] + !!psz[3];
117 return psz;
118 }
119
120 // VSIB
121 case DISUSE_REG_XMM | DISUSE_REG_GEN32:
122 case DISUSE_REG_YMM | DISUSE_REG_GEN32:
123 case DISUSE_REG_GEN32:
124 {
125 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen32));
126 const char *psz = g_aszYasmRegGen32[pParam->Base.idxGenReg];
127 *pcchReg = 2 + !!psz[2] + !!psz[3];
128 return psz;
129 }
130
131 // VSIB
132 case DISUSE_REG_XMM | DISUSE_REG_GEN64:
133 case DISUSE_REG_YMM | DISUSE_REG_GEN64:
134 case DISUSE_REG_GEN64:
135 {
136 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen64));
137 const char *psz = g_aszYasmRegGen64[pParam->Base.idxGenReg];
138 *pcchReg = 2 + !!psz[2] + !!psz[3];
139 return psz;
140 }
141
142 case DISUSE_REG_FP:
143 {
144 Assert(pParam->Base.idxFpuReg < RT_ELEMENTS(g_aszYasmRegFP));
145 const char *psz = g_aszYasmRegFP[pParam->Base.idxFpuReg];
146 *pcchReg = 3;
147 return psz;
148 }
149
150 case DISUSE_REG_MMX:
151 {
152 Assert(pParam->Base.idxMmxReg < RT_ELEMENTS(g_aszYasmRegMMX));
153 const char *psz = g_aszYasmRegMMX[pParam->Base.idxMmxReg];
154 *pcchReg = 3;
155 return psz;
156 }
157
158 case DISUSE_REG_XMM:
159 {
160 Assert(pParam->Base.idxXmmReg < RT_ELEMENTS(g_aszYasmRegXMM));
161 const char *psz = g_aszYasmRegXMM[pParam->Base.idxXmmReg];
162 *pcchReg = 4 + !!psz[4];
163 return psz;
164 }
165
166 case DISUSE_REG_YMM:
167 {
168 Assert(pParam->Base.idxYmmReg < RT_ELEMENTS(g_aszYasmRegYMM));
169 const char *psz = g_aszYasmRegYMM[pParam->Base.idxYmmReg];
170 *pcchReg = 4 + !!psz[4];
171 return psz;
172 }
173
174 case DISUSE_REG_CR:
175 {
176 Assert(pParam->Base.idxCtrlReg < RT_ELEMENTS(g_aszYasmRegCRx));
177 const char *psz = g_aszYasmRegCRx[pParam->Base.idxCtrlReg];
178 *pcchReg = 3;
179 return psz;
180 }
181
182 case DISUSE_REG_DBG:
183 {
184 Assert(pParam->Base.idxDbgReg < RT_ELEMENTS(g_aszYasmRegDRx));
185 const char *psz = g_aszYasmRegDRx[pParam->Base.idxDbgReg];
186 *pcchReg = 3;
187 return psz;
188 }
189
190 case DISUSE_REG_SEG:
191 {
192 Assert(pParam->Base.idxSegReg < RT_ELEMENTS(g_aszYasmRegCRx));
193 const char *psz = g_aszYasmRegSeg[pParam->Base.idxSegReg];
194 *pcchReg = 2;
195 return psz;
196 }
197
198 case DISUSE_REG_TEST:
199 {
200 Assert(pParam->Base.idxTestReg < RT_ELEMENTS(g_aszYasmRegTRx));
201 const char *psz = g_aszYasmRegTRx[pParam->Base.idxTestReg];
202 *pcchReg = 3;
203 return psz;
204 }
205
206 default:
207 AssertMsgFailed(("%#x\n", pParam->fUse));
208 *pcchReg = 3;
209 return "r??";
210 }
211}
212
213
214/**
215 * Gets the index register name for the given parameter.
216 *
217 * @returns The index register name.
218 * @param pDis The disassembler state.
219 * @param pParam The parameter.
220 * @param pcchReg Where to store the length of the name.
221 */
222static const char *disasmFormatYasmIndexReg(PCDISSTATE pDis, PCDISOPPARAM pParam, size_t *pcchReg)
223{
224 if (pParam->fUse & DISUSE_REG_XMM)
225 {
226 Assert(pParam->Index.idxXmmReg < RT_ELEMENTS(g_aszYasmRegXMM));
227 const char *psz = g_aszYasmRegXMM[pParam->Index.idxXmmReg];
228 *pcchReg = 4 + !!psz[4];
229 return psz;
230 }
231 else if (pParam->fUse & DISUSE_REG_YMM)
232 {
233 Assert(pParam->Index.idxYmmReg < RT_ELEMENTS(g_aszYasmRegYMM));
234 const char *psz = g_aszYasmRegYMM[pParam->Index.idxYmmReg];
235 *pcchReg = 4 + !!psz[4];
236 return psz;
237
238 }
239 else
240 switch (pDis->uAddrMode)
241 {
242 case DISCPUMODE_16BIT:
243 {
244 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen16));
245 const char *psz = g_aszYasmRegGen16[pParam->Index.idxGenReg];
246 *pcchReg = 2 + !!psz[2] + !!psz[3];
247 return psz;
248 }
249
250 case DISCPUMODE_32BIT:
251 {
252 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen32));
253 const char *psz = g_aszYasmRegGen32[pParam->Index.idxGenReg];
254 *pcchReg = 2 + !!psz[2] + !!psz[3];
255 return psz;
256 }
257
258 case DISCPUMODE_64BIT:
259 {
260 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen64));
261 const char *psz = g_aszYasmRegGen64[pParam->Index.idxGenReg];
262 *pcchReg = 2 + !!psz[2] + !!psz[3];
263 return psz;
264 }
265
266 default:
267 AssertMsgFailed(("%#x %#x\n", pParam->fUse, pDis->uAddrMode));
268 *pcchReg = 3;
269 return "r??";
270 }
271}
272
273
274/**
275 * Formats the current instruction in Yasm (/ Nasm) style.
276 *
277 *
278 * @returns The number of output characters. If this is >= cchBuf, then the content
279 * of pszBuf will be truncated.
280 * @param pDis Pointer to the disassembler state.
281 * @param pszBuf The output buffer.
282 * @param cchBuf The size of the output buffer.
283 * @param fFlags Format flags, see DIS_FORMAT_FLAGS_*.
284 * @param pfnGetSymbol Get symbol name for a jmp or call target address. Optional.
285 * @param pvUser User argument for pfnGetSymbol.
286 */
287DISDECL(size_t) DISFormatYasmEx(PCDISSTATE pDis, char *pszBuf, size_t cchBuf, uint32_t fFlags,
288 PFNDISGETSYMBOL pfnGetSymbol, void *pvUser)
289{
290/** @todo monitor and mwait aren't formatted correctly in 64-bit mode. */
291 /*
292 * Input validation and massaging.
293 */
294 AssertPtr(pDis);
295 AssertPtrNull(pszBuf);
296 Assert(pszBuf || !cchBuf);
297 AssertPtrNull(pfnGetSymbol);
298 AssertMsg(DIS_FMT_FLAGS_IS_VALID(fFlags), ("%#x\n", fFlags));
299 if (fFlags & DIS_FMT_FLAGS_ADDR_COMMENT)
300 fFlags = (fFlags & ~DIS_FMT_FLAGS_ADDR_LEFT) | DIS_FMT_FLAGS_ADDR_RIGHT;
301 if (fFlags & DIS_FMT_FLAGS_BYTES_COMMENT)
302 fFlags = (fFlags & ~DIS_FMT_FLAGS_BYTES_LEFT) | DIS_FMT_FLAGS_BYTES_RIGHT;
303
304 PCDISOPCODE const pOp = pDis->pCurInstr;
305
306 /*
307 * Output macros
308 */
309 char *pszDst = pszBuf;
310 size_t cchDst = cchBuf;
311 size_t cchOutput = 0;
312#define PUT_C(ch) \
313 do { \
314 cchOutput++; \
315 if (cchDst > 1) \
316 { \
317 cchDst--; \
318 *pszDst++ = (ch); \
319 } \
320 } while (0)
321#define PUT_STR(pszSrc, cchSrc) \
322 do { \
323 cchOutput += (cchSrc); \
324 if (cchDst > (cchSrc)) \
325 { \
326 memcpy(pszDst, (pszSrc), (cchSrc)); \
327 pszDst += (cchSrc); \
328 cchDst -= (cchSrc); \
329 } \
330 else if (cchDst > 1) \
331 { \
332 memcpy(pszDst, (pszSrc), cchDst - 1); \
333 pszDst += cchDst - 1; \
334 cchDst = 1; \
335 } \
336 } while (0)
337#define PUT_SZ(sz) \
338 PUT_STR((sz), sizeof(sz) - 1)
339#define PUT_SZ_STRICT(szStrict, szRelaxed) \
340 do { if (fFlags & DIS_FMT_FLAGS_STRICT) PUT_SZ(szStrict); else PUT_SZ(szRelaxed); } while (0)
341#define PUT_PSZ(psz) \
342 do { const size_t cchTmp = strlen(psz); PUT_STR((psz), cchTmp); } while (0)
343#define PUT_NUM(cch, fmt, num) \
344 do { \
345 cchOutput += (cch); \
346 if (cchDst > 1) \
347 { \
348 const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
349 pszDst += cchTmp; \
350 cchDst -= cchTmp; \
351 Assert(cchTmp == (cch) || cchDst == 1); \
352 } \
353 } while (0)
354/** @todo add two flags for choosing between %X / %x and h / 0x. */
355#define PUT_NUM_8(num) PUT_NUM(4, "0%02xh", (uint8_t)(num))
356#define PUT_NUM_16(num) PUT_NUM(6, "0%04xh", (uint16_t)(num))
357#define PUT_NUM_32(num) PUT_NUM(10, "0%08xh", (uint32_t)(num))
358#define PUT_NUM_64(num) PUT_NUM(18, "0%016RX64h", (uint64_t)(num))
359
360#define PUT_NUM_SIGN(cch, fmt, num, stype, utype) \
361 do { \
362 if ((stype)(num) >= 0) \
363 { \
364 PUT_C('+'); \
365 PUT_NUM(cch, fmt, (utype)(num)); \
366 } \
367 else \
368 { \
369 PUT_C('-'); \
370 PUT_NUM(cch, fmt, (utype)-(stype)(num)); \
371 } \
372 } while (0)
373#define PUT_NUM_S8(num) PUT_NUM_SIGN(4, "0%02xh", num, int8_t, uint8_t)
374#define PUT_NUM_S16(num) PUT_NUM_SIGN(6, "0%04xh", num, int16_t, uint16_t)
375#define PUT_NUM_S32(num) PUT_NUM_SIGN(10, "0%08xh", num, int32_t, uint32_t)
376#define PUT_NUM_S64(num) PUT_NUM_SIGN(18, "0%016RX64h", num, int64_t, uint64_t)
377
378#define PUT_SYMBOL_TWO(a_rcSym, a_szStart, a_chEnd) \
379 do { \
380 if (RT_SUCCESS(a_rcSym)) \
381 { \
382 PUT_SZ(a_szStart); \
383 PUT_PSZ(szSymbol); \
384 if (off != 0) \
385 { \
386 if ((int8_t)off == off) \
387 PUT_NUM_S8(off); \
388 else if ((int16_t)off == off) \
389 PUT_NUM_S16(off); \
390 else if ((int32_t)off == off) \
391 PUT_NUM_S32(off); \
392 else \
393 PUT_NUM_S64(off); \
394 } \
395 PUT_C(a_chEnd); \
396 } \
397 } while (0)
398
399#define PUT_SYMBOL(a_uSeg, a_uAddr, a_szStart, a_chEnd) \
400 do { \
401 if (pfnGetSymbol) \
402 { \
403 int rcSym = pfnGetSymbol(pDis, a_uSeg, a_uAddr, szSymbol, sizeof(szSymbol), &off, pvUser); \
404 PUT_SYMBOL_TWO(rcSym, a_szStart, a_chEnd); \
405 } \
406 } while (0)
407
408
409 /*
410 * The address?
411 */
412 if (fFlags & DIS_FMT_FLAGS_ADDR_LEFT)
413 {
414#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
415 if (pDis->uInstrAddr >= _4G)
416 PUT_NUM(9, "%08x`", (uint32_t)(pDis->uInstrAddr >> 32));
417#endif
418 PUT_NUM(8, "%08x", (uint32_t)pDis->uInstrAddr);
419 PUT_C(' ');
420 }
421
422 /*
423 * The opcode bytes?
424 */
425 if (fFlags & DIS_FMT_FLAGS_BYTES_LEFT)
426 {
427 size_t cchTmp = disFormatBytes(pDis, pszDst, cchDst, fFlags);
428 cchOutput += cchTmp;
429 if (cchDst > 1)
430 {
431 if (cchTmp <= cchDst)
432 {
433 cchDst -= cchTmp;
434 pszDst += cchTmp;
435 }
436 else
437 {
438 pszDst += cchDst - 1;
439 cchDst = 1;
440 }
441 }
442
443 /* Some padding to align the instruction. */
444 size_t cchPadding = (7 * (2 + !!(fFlags & DIS_FMT_FLAGS_BYTES_SPACED)))
445 + !!(fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS) * 2
446 + 2;
447 cchPadding = cchTmp + 1 >= cchPadding ? 1 : cchPadding - cchTmp;
448 PUT_STR(g_szSpaces, cchPadding);
449 }
450
451
452 /*
453 * Filter out invalid opcodes first as they need special
454 * treatment. UD2 is an exception and should be handled normally.
455 */
456 size_t const offInstruction = cchOutput;
457 if ( pOp->uOpcode == OP_INVALID
458 || ( pOp->uOpcode == OP_ILLUD2
459 && (pDis->fPrefix & DISPREFIX_LOCK)))
460 PUT_SZ("Illegal opcode");
461 else
462 {
463 /*
464 * Prefixes
465 */
466 if (pDis->fPrefix & DISPREFIX_LOCK)
467 PUT_SZ("lock ");
468 if (pDis->fPrefix & DISPREFIX_REP)
469 PUT_SZ("rep ");
470 else if(pDis->fPrefix & DISPREFIX_REPNE)
471 PUT_SZ("repne ");
472
473 /*
474 * Adjust the format string to the correct mnemonic
475 * or to avoid things the assembler cannot handle correctly.
476 */
477 char szTmpFmt[48];
478 const char *pszFmt = pOp->pszOpcode;
479 bool fIgnoresOpSize = false;
480 bool fMayNeedAddrSize = false;
481 switch (pOp->uOpcode)
482 {
483 case OP_JECXZ:
484 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "jcxz %Jb" : pDis->uOpMode == DISCPUMODE_32BIT ? "jecxz %Jb" : "jrcxz %Jb";
485 break;
486 case OP_PUSHF:
487 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "pushfw" : pDis->uOpMode == DISCPUMODE_32BIT ? "pushfd" : "pushfq";
488 break;
489 case OP_POPF:
490 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "popfw" : pDis->uOpMode == DISCPUMODE_32BIT ? "popfd" : "popfq";
491 break;
492 case OP_PUSHA:
493 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "pushaw" : "pushad";
494 break;
495 case OP_POPA:
496 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "popaw" : "popad";
497 break;
498 case OP_INSB:
499 pszFmt = "insb";
500 fIgnoresOpSize = fMayNeedAddrSize = true;
501 break;
502 case OP_INSWD:
503 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "insw" : pDis->uOpMode == DISCPUMODE_32BIT ? "insd" : "insq";
504 fMayNeedAddrSize = true;
505 break;
506 case OP_OUTSB:
507 pszFmt = "outsb";
508 fIgnoresOpSize = fMayNeedAddrSize = true;
509 break;
510 case OP_OUTSWD:
511 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "outsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "outsd" : "outsq";
512 fMayNeedAddrSize = true;
513 break;
514 case OP_MOVSB:
515 pszFmt = "movsb";
516 fIgnoresOpSize = fMayNeedAddrSize = true;
517 break;
518 case OP_MOVSWD:
519 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "movsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "movsd" : "movsq";
520 fMayNeedAddrSize = true;
521 break;
522 case OP_CMPSB:
523 pszFmt = "cmpsb";
524 fIgnoresOpSize = fMayNeedAddrSize = true;
525 break;
526 case OP_CMPWD:
527 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cmpsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "cmpsd" : "cmpsq";
528 fMayNeedAddrSize = true;
529 break;
530 case OP_SCASB:
531 pszFmt = "scasb";
532 fIgnoresOpSize = fMayNeedAddrSize = true;
533 break;
534 case OP_SCASWD:
535 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "scasw" : pDis->uOpMode == DISCPUMODE_32BIT ? "scasd" : "scasq";
536 fMayNeedAddrSize = true;
537 break;
538 case OP_LODSB:
539 pszFmt = "lodsb";
540 fIgnoresOpSize = fMayNeedAddrSize = true;
541 break;
542 case OP_LODSWD:
543 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "lodsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "lodsd" : "lodsq";
544 fMayNeedAddrSize = true;
545 break;
546 case OP_STOSB:
547 pszFmt = "stosb";
548 fIgnoresOpSize = fMayNeedAddrSize = true;
549 break;
550 case OP_STOSWD:
551 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "stosw" : pDis->uOpMode == DISCPUMODE_32BIT ? "stosd" : "stosq";
552 fMayNeedAddrSize = true;
553 break;
554 case OP_CBW:
555 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cbw" : pDis->uOpMode == DISCPUMODE_32BIT ? "cwde" : "cdqe";
556 break;
557 case OP_CWD:
558 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cwd" : pDis->uOpMode == DISCPUMODE_32BIT ? "cdq" : "cqo";
559 break;
560 case OP_SHL:
561 Assert(pszFmt[3] == '/');
562 pszFmt += 4;
563 break;
564 case OP_XLAT:
565 pszFmt = "xlatb";
566 break;
567 case OP_INT3:
568 pszFmt = "int3";
569 break;
570
571 /*
572 * Don't know how to tell yasm to generate complicated nop stuff, so 'db' it.
573 */
574 case OP_NOP:
575 if (pDis->bOpCode == 0x90)
576 /* fine, fine */;
577 else if (pszFmt[sizeof("nop %Ev") - 1] == '/' && pszFmt[sizeof("nop %Ev")] == 'p')
578 pszFmt = "prefetch %Eb";
579 else if (pDis->bOpCode == 0x1f)
580 {
581 Assert(pDis->cbInstr >= 3);
582 PUT_SZ("db 00fh, 01fh,");
583 PUT_NUM_8(MAKE_MODRM(pDis->ModRM.Bits.Mod, pDis->ModRM.Bits.Reg, pDis->ModRM.Bits.Rm));
584 for (unsigned i = 3; i < pDis->cbInstr; i++)
585 {
586 PUT_C(',');
587 PUT_NUM_8(0x90); ///@todo fixme.
588 }
589 pszFmt = "";
590 }
591 break;
592
593 default:
594 /* ST(X) -> stX (floating point) */
595 if (*pszFmt == 'f' && strchr(pszFmt, '('))
596 {
597 char *pszFmtDst = szTmpFmt;
598 char ch;
599 do
600 {
601 ch = *pszFmt++;
602 if (ch == 'S' && pszFmt[0] == 'T' && pszFmt[1] == '(')
603 {
604 *pszFmtDst++ = 's';
605 *pszFmtDst++ = 't';
606 pszFmt += 2;
607 ch = *pszFmt;
608 Assert(pszFmt[1] == ')');
609 pszFmt += 2;
610 *pszFmtDst++ = ch;
611 }
612 else
613 *pszFmtDst++ = ch;
614 } while (ch != '\0');
615 pszFmt = szTmpFmt;
616 }
617 if (strchr ("#@&", *pszFmt))
618 {
619 const char *pszDelim = strchr(pszFmt, '/');
620 const char *pszSpace = (pszDelim ? strchr(pszDelim, ' ') : NULL);
621 if (pszDelim != NULL)
622 {
623 char *pszFmtDst = szTmpFmt;
624 if (pszSpace == NULL) pszSpace = strchr(pszDelim, 0);
625 if ( (*pszFmt == '#' && pDis->bVexWFlag)
626 || (*pszFmt == '@' && !VEXREG_IS256B(pDis->bVexDestReg))
627 || (*pszFmt == '&' && ( DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
628 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
629 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse)
630 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param4.fUse))))
631 {
632 strncpy(pszFmtDst, pszFmt + 1, pszDelim - pszFmt - 1);
633 pszFmtDst += pszDelim - pszFmt - 1;
634 }
635 else
636 {
637 strncpy(pszFmtDst, pszDelim + 1, pszSpace - pszDelim - 1);
638 pszFmtDst += pszSpace - pszDelim - 1;
639 }
640 strcpy (pszFmtDst, pszSpace);
641 pszFmt = szTmpFmt;
642 }
643 }
644 break;
645
646 /*
647 * Horrible hacks.
648 */
649 case OP_FLD:
650 if (pDis->bOpCode == 0xdb) /* m80fp workaround. */
651 *(int *)&pDis->Param1.fParam &= ~0x1f; /* make it pure OP_PARM_M */
652 break;
653 case OP_LAR: /* hack w -> v, probably not correct. */
654 *(int *)&pDis->Param2.fParam &= ~0x1f;
655 *(int *)&pDis->Param2.fParam |= OP_PARM_v;
656 break;
657 }
658
659 /*
660 * Add operand size and address prefixes for outsb, movsb, etc.
661 */
662 if (pDis->fPrefix & (DISPREFIX_OPSIZE | DISPREFIX_ADDRSIZE))
663 {
664 if (fIgnoresOpSize && (pDis->fPrefix & DISPREFIX_OPSIZE) )
665 {
666 if (pDis->uCpuMode == DISCPUMODE_16BIT)
667 PUT_SZ("o32 ");
668 else
669 PUT_SZ("o16 ");
670 }
671 if (fMayNeedAddrSize && (pDis->fPrefix & DISPREFIX_ADDRSIZE) )
672 {
673 if (pDis->uCpuMode == DISCPUMODE_16BIT)
674 PUT_SZ("a32 ");
675 else
676 PUT_SZ("a16 ");
677 }
678 }
679
680 /*
681 * Formatting context and associated macros.
682 */
683 PCDISOPPARAM pParam = &pDis->Param1;
684 int iParam = 1;
685
686#define PUT_FAR() \
687 do { \
688 if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_p \
689 && pOp->uOpcode != OP_LDS /* table bugs? */ \
690 && pOp->uOpcode != OP_LES \
691 && pOp->uOpcode != OP_LFS \
692 && pOp->uOpcode != OP_LGS \
693 && pOp->uOpcode != OP_LSS ) \
694 PUT_SZ("far "); \
695 } while (0)
696 /** @todo mov ah,ch ends up with a byte 'override'... - check if this wasn't fixed. */
697 /** @todo drop the work/dword/qword override when the src/dst is a register (except for movsx/movzx). */
698#define PUT_SIZE_OVERRIDE() \
699 do { \
700 switch (OP_PARM_VSUBTYPE(pParam->fParam)) \
701 { \
702 case OP_PARM_v: \
703 case OP_PARM_y: \
704 switch (pDis->uOpMode) \
705 { \
706 case DISCPUMODE_16BIT: if (OP_PARM_VSUBTYPE(pParam->fParam) != OP_PARM_y) PUT_SZ("word "); break; \
707 case DISCPUMODE_32BIT: \
708 if (pDis->pCurInstr->uOpcode != OP_GATHER || pDis->bVexWFlag) { PUT_SZ("dword "); break; } \
709 case DISCPUMODE_64BIT: PUT_SZ("qword "); break; \
710 default: break; \
711 } \
712 break; \
713 case OP_PARM_b: PUT_SZ("byte "); break; \
714 case OP_PARM_w: \
715 if (OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W || \
716 OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
717 { \
718 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("dword "); \
719 else PUT_SZ("word "); \
720 } \
721 break; \
722 case OP_PARM_d: \
723 if (OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W || \
724 OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
725 { \
726 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("qword "); \
727 else PUT_SZ("dword "); \
728 } \
729 break; \
730 case OP_PARM_q: \
731 if (OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W || \
732 OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
733 { \
734 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("oword "); \
735 else PUT_SZ("qword "); \
736 } \
737 break; \
738 case OP_PARM_ps: \
739 case OP_PARM_pd: \
740 case OP_PARM_x: if (VEXREG_IS256B(pDis->bVexDestReg)) { PUT_SZ("yword "); break; } \
741 case OP_PARM_ss: \
742 case OP_PARM_sd: \
743 case OP_PARM_dq: PUT_SZ("oword "); break; \
744 case OP_PARM_qq: PUT_SZ("yword "); break; \
745 case OP_PARM_p: break; /* see PUT_FAR */ \
746 case OP_PARM_s: if (pParam->fUse & DISUSE_REG_FP) PUT_SZ("tword "); break; /* ?? */ \
747 case OP_PARM_z: break; \
748 case OP_PARM_NONE: \
749 if ( OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M \
750 && ((pParam->fUse & DISUSE_REG_FP) || pOp->uOpcode == OP_FLD)) \
751 PUT_SZ("tword "); \
752 break; \
753 default: break; /*no pointer type specified/necessary*/ \
754 } \
755 } while (0)
756 static const char s_szSegPrefix[6][4] = { "es:", "cs:", "ss:", "ds:", "fs:", "gs:" };
757#define PUT_SEGMENT_OVERRIDE() \
758 do { \
759 if (pDis->fPrefix & DISPREFIX_SEG) \
760 PUT_STR(s_szSegPrefix[pDis->idxSegPrefix], 3); \
761 } while (0)
762
763
764 /*
765 * Segment prefixing for instructions that doesn't do memory access.
766 */
767 if ( (pDis->fPrefix & DISPREFIX_SEG)
768 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
769 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
770 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse))
771 {
772 PUT_STR(s_szSegPrefix[pDis->idxSegPrefix], 2);
773 PUT_C(' ');
774 }
775
776
777 /*
778 * The formatting loop.
779 */
780 RTINTPTR off;
781 char szSymbol[128];
782 char ch;
783 while ((ch = *pszFmt++) != '\0')
784 {
785 if (ch == '%')
786 {
787 ch = *pszFmt++;
788 switch (ch)
789 {
790 /*
791 * ModRM - Register only.
792 */
793 case 'C': /* Control register (ParseModRM / UseModRM). */
794 case 'D': /* Debug register (ParseModRM / UseModRM). */
795 case 'G': /* ModRM selects general register (ParseModRM / UseModRM). */
796 case 'S': /* ModRM byte selects a segment register (ParseModRM / UseModRM). */
797 case 'T': /* ModRM byte selects a test register (ParseModRM / UseModRM). */
798 case 'V': /* ModRM byte selects an XMM/SSE register (ParseModRM / UseModRM). */
799 case 'P': /* ModRM byte selects MMX register (ParseModRM / UseModRM). */
800 case 'H': /* The VEX.vvvv field of the VEX prefix selects a XMM/YMM register. */
801 case 'L': /* The upper 4 bits of the 8-bit immediate selects a XMM/YMM register. */
802 {
803 pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
804 Assert(!(pParam->fUse & (DISUSE_INDEX | DISUSE_SCALE) /* No SIB here... */));
805 Assert(!(pParam->fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32)));
806
807 size_t cchReg;
808 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
809 PUT_STR(pszReg, cchReg);
810 break;
811 }
812
813 /*
814 * ModRM - Register or memory.
815 */
816 case 'E': /* ModRM specifies parameter (ParseModRM / UseModRM / UseSIB). */
817 case 'Q': /* ModRM byte selects MMX register or memory address (ParseModRM / UseModRM). */
818 case 'R': /* ModRM byte may only refer to a general register (ParseModRM / UseModRM). */
819 case 'W': /* ModRM byte selects an XMM/SSE register or a memory address (ParseModRM / UseModRM). */
820 case 'M': /* ModRM may only refer to memory (ParseModRM / UseModRM). */
821 {
822 pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
823
824 PUT_FAR();
825 uint32_t const fUse = pParam->fUse;
826 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
827 {
828 /* Work around mov seg,[mem16] and mov [mem16],seg as these always make a 16-bit mem
829 while the register variants deals with 16, 32 & 64 in the normal fashion. */
830 if ( pParam->fParam != OP_PARM_Ev
831 || pOp->uOpcode != OP_MOV
832 || ( pOp->fParam1 != OP_PARM_Sw
833 && pOp->fParam2 != OP_PARM_Sw))
834 PUT_SIZE_OVERRIDE();
835 PUT_C('[');
836 }
837 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
838 && (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32)))
839 {
840 if ( (fUse & DISUSE_DISPLACEMENT8)
841 && !pParam->uDisp.i8)
842 PUT_SZ("byte ");
843 else if ( (fUse & DISUSE_DISPLACEMENT16)
844 && (int8_t)pParam->uDisp.i16 == (int16_t)pParam->uDisp.i16)
845 PUT_SZ("word ");
846 else if ( (fUse & DISUSE_DISPLACEMENT32)
847 && (int16_t)pParam->uDisp.i32 == (int32_t)pParam->uDisp.i32) //??
848 PUT_SZ("dword ");
849 else if ( (fUse & DISUSE_DISPLACEMENT64)
850 && (pDis->SIB.Bits.Base != 5 || pDis->ModRM.Bits.Mod != 0)
851 && (int32_t)pParam->uDisp.i64 == (int64_t)pParam->uDisp.i64) //??
852 PUT_SZ("qword ");
853 }
854 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
855 PUT_SEGMENT_OVERRIDE();
856
857 bool fBase = (fUse & DISUSE_BASE) /* When exactly is DISUSE_BASE supposed to be set? disasmModRMReg doesn't set it. */
858 || ( (fUse & ( DISUSE_REG_GEN8
859 | DISUSE_REG_GEN16
860 | DISUSE_REG_GEN32
861 | DISUSE_REG_GEN64
862 | DISUSE_REG_FP
863 | DISUSE_REG_MMX
864 | DISUSE_REG_XMM
865 | DISUSE_REG_YMM
866 | DISUSE_REG_CR
867 | DISUSE_REG_DBG
868 | DISUSE_REG_SEG
869 | DISUSE_REG_TEST ))
870 && !DISUSE_IS_EFFECTIVE_ADDR(fUse));
871 if (fBase)
872 {
873 size_t cchReg;
874 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
875 PUT_STR(pszReg, cchReg);
876 }
877
878 if (fUse & DISUSE_INDEX)
879 {
880 if (fBase)
881 PUT_C('+');
882
883 size_t cchReg;
884 const char *pszReg = disasmFormatYasmIndexReg(pDis, pParam, &cchReg);
885 PUT_STR(pszReg, cchReg);
886
887 if (fUse & DISUSE_SCALE)
888 {
889 PUT_C('*');
890 PUT_C('0' + pParam->uScale);
891 }
892 }
893 else
894 Assert(!(fUse & DISUSE_SCALE));
895
896 int64_t off2 = 0;
897 if (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32))
898 {
899 if (fUse & DISUSE_DISPLACEMENT8)
900 off2 = pParam->uDisp.i8;
901 else if (fUse & DISUSE_DISPLACEMENT16)
902 off2 = pParam->uDisp.i16;
903 else if (fUse & (DISUSE_DISPLACEMENT32 | DISUSE_RIPDISPLACEMENT32))
904 off2 = pParam->uDisp.i32;
905 else if (fUse & DISUSE_DISPLACEMENT64)
906 off2 = pParam->uDisp.i64;
907 else
908 {
909 AssertFailed();
910 off2 = 0;
911 }
912
913 int64_t off3 = off2;
914 if (fBase || (fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)))
915 {
916 PUT_C(off3 >= 0 ? '+' : '-');
917 if (off3 < 0)
918 off3 = -off3;
919 }
920 if (fUse & DISUSE_DISPLACEMENT8)
921 PUT_NUM_8( off3);
922 else if (fUse & DISUSE_DISPLACEMENT16)
923 PUT_NUM_16(off3);
924 else if (fUse & DISUSE_DISPLACEMENT32)
925 PUT_NUM_32(off3);
926 else if (fUse & DISUSE_DISPLACEMENT64)
927 PUT_NUM_64(off3);
928 else
929 {
930 PUT_NUM_32(off3);
931 PUT_SZ(" wrt rip (");
932 off2 += pDis->uInstrAddr + pDis->cbInstr;
933 PUT_NUM_64(off2);
934 if (pfnGetSymbol)
935 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
936 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
937 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
938 pDis->uAddrMode == DISCPUMODE_64BIT
939 ? (uint64_t)off2
940 : pDis->uAddrMode == DISCPUMODE_32BIT
941 ? (uint32_t)off2
942 : (uint16_t)off2,
943 " = ",
944 ')');
945 else
946 PUT_C(')');
947 }
948 }
949
950 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
951 {
952 if (pfnGetSymbol && !fBase && !(fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)) && off2 != 0)
953 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
954 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
955 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
956 pDis->uAddrMode == DISCPUMODE_64BIT
957 ? (uint64_t)off2
958 : pDis->uAddrMode == DISCPUMODE_32BIT
959 ? (uint32_t)off2
960 : (uint16_t)off2,
961 " (=",
962 ')');
963 PUT_C(']');
964 }
965 break;
966 }
967
968 case 'F': /* Eflags register (0 - popf/pushf only, avoided in adjustments above). */
969 AssertFailed();
970 break;
971
972 case 'I': /* Immediate data (ParseImmByte, ParseImmByteSX, ParseImmV, ParseImmUshort, ParseImmZ). */
973 Assert(*pszFmt == 'b' || *pszFmt == 'v' || *pszFmt == 'w' || *pszFmt == 'z'); pszFmt++;
974 switch (pParam->fUse & ( DISUSE_IMMEDIATE8 | DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64
975 | DISUSE_IMMEDIATE16_SX8 | DISUSE_IMMEDIATE32_SX8 | DISUSE_IMMEDIATE64_SX8))
976 {
977 case DISUSE_IMMEDIATE8:
978 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
979 && ( (pOp->fParam1 >= OP_PARM_REG_GEN8_START && pOp->fParam1 <= OP_PARM_REG_GEN8_END)
980 || (pOp->fParam2 >= OP_PARM_REG_GEN8_START && pOp->fParam2 <= OP_PARM_REG_GEN8_END))
981 )
982 PUT_SZ("strict byte ");
983 PUT_NUM_8(pParam->uValue);
984 break;
985
986 case DISUSE_IMMEDIATE16:
987 if ( pDis->uCpuMode != pDis->uOpMode
988 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
989 && ( (int8_t)pParam->uValue == (int16_t)pParam->uValue
990 || (pOp->fParam1 >= OP_PARM_REG_GEN16_START && pOp->fParam1 <= OP_PARM_REG_GEN16_END)
991 || (pOp->fParam2 >= OP_PARM_REG_GEN16_START && pOp->fParam2 <= OP_PARM_REG_GEN16_END))
992 )
993 )
994 {
995 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
996 PUT_SZ_STRICT("strict byte ", "byte ");
997 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
998 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
999 PUT_SZ_STRICT("strict word ", "word ");
1000 }
1001 PUT_NUM_16(pParam->uValue);
1002 break;
1003
1004 case DISUSE_IMMEDIATE16_SX8:
1005 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1006 || pDis->pCurInstr->uOpcode != OP_PUSH)
1007 PUT_SZ_STRICT("strict byte ", "byte ");
1008 else
1009 PUT_SZ("word ");
1010 PUT_NUM_16(pParam->uValue);
1011 break;
1012
1013 case DISUSE_IMMEDIATE32:
1014 if ( pDis->uOpMode != (pDis->uCpuMode == DISCPUMODE_16BIT ? DISCPUMODE_16BIT : DISCPUMODE_32BIT) /* not perfect */
1015 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
1016 && ( (int8_t)pParam->uValue == (int32_t)pParam->uValue
1017 || (pOp->fParam1 >= OP_PARM_REG_GEN32_START && pOp->fParam1 <= OP_PARM_REG_GEN32_END)
1018 || (pOp->fParam2 >= OP_PARM_REG_GEN32_START && pOp->fParam2 <= OP_PARM_REG_GEN32_END))
1019 )
1020 )
1021 {
1022 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
1023 PUT_SZ_STRICT("strict byte ", "byte ");
1024 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
1025 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
1026 PUT_SZ_STRICT("strict dword ", "dword ");
1027 }
1028 PUT_NUM_32(pParam->uValue);
1029 if (pDis->uCpuMode == DISCPUMODE_32BIT)
1030 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uValue, " (=", ')');
1031 break;
1032
1033 case DISUSE_IMMEDIATE32_SX8:
1034 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1035 || pDis->pCurInstr->uOpcode != OP_PUSH)
1036 PUT_SZ_STRICT("strict byte ", "byte ");
1037 else
1038 PUT_SZ("dword ");
1039 PUT_NUM_32(pParam->uValue);
1040 break;
1041
1042 case DISUSE_IMMEDIATE64_SX8:
1043 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1044 || pDis->pCurInstr->uOpcode != OP_PUSH)
1045 PUT_SZ_STRICT("strict byte ", "byte ");
1046 else
1047 PUT_SZ("qword ");
1048 PUT_NUM_64(pParam->uValue);
1049 break;
1050
1051 case DISUSE_IMMEDIATE64:
1052 PUT_NUM_64(pParam->uValue);
1053 break;
1054
1055 default:
1056 AssertFailed();
1057 break;
1058 }
1059 break;
1060
1061 case 'J': /* Relative jump offset (ParseImmBRel + ParseImmVRel). */
1062 {
1063 int32_t offDisplacement;
1064 Assert(iParam == 1);
1065 bool fPrefix = (fFlags & DIS_FMT_FLAGS_STRICT)
1066 && pOp->uOpcode != OP_CALL
1067 && pOp->uOpcode != OP_LOOP
1068 && pOp->uOpcode != OP_LOOPE
1069 && pOp->uOpcode != OP_LOOPNE
1070 && pOp->uOpcode != OP_JECXZ;
1071 if (pOp->uOpcode == OP_CALL)
1072 fFlags &= ~DIS_FMT_FLAGS_RELATIVE_BRANCH;
1073
1074 if (pParam->fUse & DISUSE_IMMEDIATE8_REL)
1075 {
1076 if (fPrefix)
1077 PUT_SZ("short ");
1078 offDisplacement = (int8_t)pParam->uValue;
1079 Assert(*pszFmt == 'b'); pszFmt++;
1080
1081 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1082 PUT_NUM_S8(offDisplacement);
1083 }
1084 else if (pParam->fUse & DISUSE_IMMEDIATE16_REL)
1085 {
1086 if (fPrefix)
1087 PUT_SZ("near ");
1088 offDisplacement = (int16_t)pParam->uValue;
1089 Assert(*pszFmt == 'v'); pszFmt++;
1090
1091 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1092 PUT_NUM_S16(offDisplacement);
1093 }
1094 else
1095 {
1096 if (fPrefix)
1097 PUT_SZ("near ");
1098 offDisplacement = (int32_t)pParam->uValue;
1099 Assert(pParam->fUse & (DISUSE_IMMEDIATE32_REL | DISUSE_IMMEDIATE64_REL));
1100 Assert(*pszFmt == 'v'); pszFmt++;
1101
1102 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1103 PUT_NUM_S32(offDisplacement);
1104 }
1105 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1106 PUT_SZ(" (");
1107
1108 RTUINTPTR uTrgAddr = pDis->uInstrAddr + pDis->cbInstr + offDisplacement;
1109 if (pDis->uCpuMode == DISCPUMODE_16BIT)
1110 PUT_NUM_16(uTrgAddr);
1111 else if (pDis->uCpuMode == DISCPUMODE_32BIT)
1112 PUT_NUM_32(uTrgAddr);
1113 else
1114 PUT_NUM_64(uTrgAddr);
1115
1116 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1117 {
1118 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " = ", ' ');
1119 PUT_C(')');
1120 }
1121 else
1122 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " (", ')');
1123 break;
1124 }
1125
1126 case 'A': /* Direct (jump/call) address (ParseImmAddr). */
1127 {
1128 Assert(*pszFmt == 'p'); pszFmt++;
1129 PUT_FAR();
1130 PUT_SIZE_OVERRIDE();
1131 PUT_SEGMENT_OVERRIDE();
1132 int rc = VERR_SYMBOL_NOT_FOUND;
1133 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1134 {
1135 case DISUSE_IMMEDIATE_ADDR_16_16:
1136 PUT_NUM_16(pParam->uValue >> 16);
1137 PUT_C(':');
1138 PUT_NUM_16(pParam->uValue);
1139 if (pfnGetSymbol)
1140 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1141 break;
1142 case DISUSE_IMMEDIATE_ADDR_16_32:
1143 PUT_NUM_16(pParam->uValue >> 32);
1144 PUT_C(':');
1145 PUT_NUM_32(pParam->uValue);
1146 if (pfnGetSymbol)
1147 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1148 break;
1149 case DISUSE_DISPLACEMENT16:
1150 PUT_NUM_16(pParam->uValue);
1151 if (pfnGetSymbol)
1152 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1153 break;
1154 case DISUSE_DISPLACEMENT32:
1155 PUT_NUM_32(pParam->uValue);
1156 if (pfnGetSymbol)
1157 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1158 break;
1159 case DISUSE_DISPLACEMENT64:
1160 PUT_NUM_64(pParam->uValue);
1161 if (pfnGetSymbol)
1162 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint64_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1163 break;
1164 default:
1165 AssertFailed();
1166 break;
1167 }
1168
1169 PUT_SYMBOL_TWO(rc, " [", ']');
1170 break;
1171 }
1172
1173 case 'O': /* No ModRM byte (ParseImmAddr). */
1174 {
1175 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1176 PUT_FAR();
1177 PUT_SIZE_OVERRIDE();
1178 PUT_C('[');
1179 PUT_SEGMENT_OVERRIDE();
1180 int rc = VERR_SYMBOL_NOT_FOUND;
1181 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1182 {
1183 case DISUSE_IMMEDIATE_ADDR_16_16:
1184 PUT_NUM_16(pParam->uValue >> 16);
1185 PUT_C(':');
1186 PUT_NUM_16(pParam->uValue);
1187 if (pfnGetSymbol)
1188 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1189 break;
1190 case DISUSE_IMMEDIATE_ADDR_16_32:
1191 PUT_NUM_16(pParam->uValue >> 32);
1192 PUT_C(':');
1193 PUT_NUM_32(pParam->uValue);
1194 if (pfnGetSymbol)
1195 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1196 break;
1197 case DISUSE_DISPLACEMENT16:
1198 PUT_NUM_16(pParam->uDisp.i16);
1199 if (pfnGetSymbol)
1200 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u16, szSymbol, sizeof(szSymbol), &off, pvUser);
1201 break;
1202 case DISUSE_DISPLACEMENT32:
1203 PUT_NUM_32(pParam->uDisp.i32);
1204 if (pfnGetSymbol)
1205 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u32, szSymbol, sizeof(szSymbol), &off, pvUser);
1206 break;
1207 case DISUSE_DISPLACEMENT64:
1208 PUT_NUM_64(pParam->uDisp.i64);
1209 if (pfnGetSymbol)
1210 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u64, szSymbol, sizeof(szSymbol), &off, pvUser);
1211 break;
1212 default:
1213 AssertFailed();
1214 break;
1215 }
1216 PUT_C(']');
1217
1218 PUT_SYMBOL_TWO(rc, " (", ')');
1219 break;
1220 }
1221
1222 case 'X': /* DS:SI (ParseXb, ParseXv). */
1223 case 'Y': /* ES:DI (ParseYb, ParseYv). */
1224 {
1225 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1226 PUT_FAR();
1227 PUT_SIZE_OVERRIDE();
1228 PUT_C('[');
1229 if (pParam->fUse & DISUSE_POINTER_DS_BASED)
1230 PUT_SZ("ds:");
1231 else
1232 PUT_SZ("es:");
1233
1234 size_t cchReg;
1235 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1236 PUT_STR(pszReg, cchReg);
1237 PUT_C(']');
1238 break;
1239 }
1240
1241 case 'e': /* Register based on operand size (e.g. %eAX, %eAH) (ParseFixedReg). */
1242 {
1243 Assert(RT_C_IS_ALPHA(pszFmt[0]) && RT_C_IS_ALPHA(pszFmt[1]) && !RT_C_IS_ALPHA(pszFmt[2]));
1244 pszFmt += 2;
1245 size_t cchReg;
1246 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1247 PUT_STR(pszReg, cchReg);
1248 break;
1249 }
1250
1251 default:
1252 AssertMsgFailed(("%c%s!\n", ch, pszFmt));
1253 break;
1254 }
1255 AssertMsg(*pszFmt == ',' || *pszFmt == '\0', ("%c%s\n", ch, pszFmt));
1256 }
1257 else
1258 {
1259 PUT_C(ch);
1260 if (ch == ',')
1261 {
1262 Assert(*pszFmt != ' ');
1263 PUT_C(' ');
1264 switch (++iParam)
1265 {
1266 case 2: pParam = &pDis->Param2; break;
1267 case 3: pParam = &pDis->Param3; break;
1268 case 4: pParam = &pDis->Param4; break;
1269 default: pParam = NULL; break;
1270 }
1271 }
1272 }
1273 } /* while more to format */
1274 }
1275
1276 /*
1277 * Any additional output to the right of the instruction?
1278 */
1279 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1280 {
1281 /* some up front padding. */
1282 size_t cchPadding = cchOutput - offInstruction;
1283 cchPadding = cchPadding + 1 >= 42 ? 1 : 42 - cchPadding;
1284 PUT_STR(g_szSpaces, cchPadding);
1285
1286 /* comment? */
1287 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1288 PUT_SZ(";");
1289
1290 /*
1291 * The address?
1292 */
1293 if (fFlags & DIS_FMT_FLAGS_ADDR_RIGHT)
1294 {
1295 PUT_C(' ');
1296#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
1297 if (pDis->uInstrAddr >= _4G)
1298 PUT_NUM(9, "%08x`", (uint32_t)(pDis->uInstrAddr >> 32));
1299#endif
1300 PUT_NUM(8, "%08x", (uint32_t)pDis->uInstrAddr);
1301 }
1302
1303 /*
1304 * Opcode bytes?
1305 */
1306 if (fFlags & DIS_FMT_FLAGS_BYTES_RIGHT)
1307 {
1308 PUT_C(' ');
1309 size_t cchTmp = disFormatBytes(pDis, pszDst, cchDst, fFlags);
1310 cchOutput += cchTmp;
1311 if (cchTmp >= cchDst)
1312 cchTmp = cchDst - (cchDst != 0);
1313 cchDst -= cchTmp;
1314 pszDst += cchTmp;
1315 }
1316 }
1317
1318 /*
1319 * Terminate it - on overflow we'll have reserved one byte for this.
1320 */
1321 if (cchDst > 0)
1322 *pszDst = '\0';
1323 else
1324 Assert(!cchBuf);
1325
1326 /* clean up macros */
1327#undef PUT_PSZ
1328#undef PUT_SZ
1329#undef PUT_STR
1330#undef PUT_C
1331 return cchOutput;
1332}
1333
1334
1335/**
1336 * Formats the current instruction in Yasm (/ Nasm) style.
1337 *
1338 * This is a simplified version of DISFormatYasmEx() provided for your convenience.
1339 *
1340 *
1341 * @returns The number of output characters. If this is >= cchBuf, then the content
1342 * of pszBuf will be truncated.
1343 * @param pDis Pointer to the disassembler state.
1344 * @param pszBuf The output buffer.
1345 * @param cchBuf The size of the output buffer.
1346 */
1347DISDECL(size_t) DISFormatYasm(PCDISSTATE pDis, char *pszBuf, size_t cchBuf)
1348{
1349 return DISFormatYasmEx(pDis, pszBuf, cchBuf, 0 /* fFlags */, NULL /* pfnGetSymbol */, NULL /* pvUser */);
1350}
1351
1352
1353/**
1354 * Checks if the encoding of the given disassembled instruction is something we
1355 * can never get YASM to produce.
1356 *
1357 * @returns true if it's odd, false if it isn't.
1358 * @param pDis The disassembler output. The byte fetcher callback will
1359 * be used if present as we might need to fetch opcode
1360 * bytes.
1361 */
1362DISDECL(bool) DISFormatYasmIsOddEncoding(PDISSTATE pDis)
1363{
1364 /*
1365 * Mod rm + SIB: Check for duplicate EBP encodings that yasm won't use for very good reasons.
1366 */
1367 if ( pDis->uAddrMode != DISCPUMODE_16BIT ///@todo correct?
1368 && pDis->ModRM.Bits.Rm == 4
1369 && pDis->ModRM.Bits.Mod != 3)
1370 {
1371 /* No scaled index SIB (index=4), except for ESP. */
1372 if ( pDis->SIB.Bits.Index == 4
1373 && pDis->SIB.Bits.Base != 4)
1374 return true;
1375
1376 /* EBP + displacement */
1377 if ( pDis->ModRM.Bits.Mod != 0
1378 && pDis->SIB.Bits.Base == 5
1379 && pDis->SIB.Bits.Scale == 0)
1380 return true;
1381 }
1382
1383 /*
1384 * Seems to be an instruction alias here, but I cannot find any docs on it... hrmpf!
1385 */
1386 if ( pDis->pCurInstr->uOpcode == OP_SHL
1387 && pDis->ModRM.Bits.Reg == 6)
1388 return true;
1389
1390 /*
1391 * Check for multiple prefixes of the same kind.
1392 */
1393 uint8_t off1stSeg = UINT8_MAX;
1394 uint8_t offOpSize = UINT8_MAX;
1395 uint8_t offAddrSize = UINT8_MAX;
1396 uint32_t fPrefixes = 0;
1397 for (uint32_t offOpcode = 0; offOpcode < RT_ELEMENTS(pDis->abInstr); offOpcode++)
1398 {
1399 uint32_t f;
1400 switch (pDis->abInstr[offOpcode])
1401 {
1402 case 0xf0:
1403 f = DISPREFIX_LOCK;
1404 break;
1405
1406 case 0xf2:
1407 case 0xf3:
1408 f = DISPREFIX_REP; /* yes, both */
1409 break;
1410
1411 case 0x2e:
1412 case 0x3e:
1413 case 0x26:
1414 case 0x36:
1415 case 0x64:
1416 case 0x65:
1417 if (off1stSeg == UINT8_MAX)
1418 off1stSeg = offOpcode;
1419 f = DISPREFIX_SEG;
1420 break;
1421
1422 case 0x66:
1423 if (offOpSize == UINT8_MAX)
1424 offOpSize = offOpcode;
1425 f = DISPREFIX_OPSIZE;
1426 break;
1427
1428 case 0x67:
1429 if (offAddrSize == UINT8_MAX)
1430 offAddrSize = offOpcode;
1431 f = DISPREFIX_ADDRSIZE;
1432 break;
1433
1434 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1435 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1436 f = pDis->uCpuMode == DISCPUMODE_64BIT ? DISPREFIX_REX : 0;
1437 break;
1438
1439 default:
1440 f = 0;
1441 break;
1442 }
1443 if (!f)
1444 break; /* done */
1445 if (fPrefixes & f)
1446 return true;
1447 fPrefixes |= f;
1448 }
1449
1450 /* segment overrides are fun */
1451 if (fPrefixes & DISPREFIX_SEG)
1452 {
1453 /* no effective address which it may apply to. */
1454 Assert((pDis->fPrefix & DISPREFIX_SEG) || pDis->uCpuMode == DISCPUMODE_64BIT);
1455 if ( !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
1456 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
1457 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse))
1458 return true;
1459
1460 /* Yasm puts the segment prefixes before the operand prefix with no
1461 way of overriding it. */
1462 if (offOpSize < off1stSeg)
1463 return true;
1464 }
1465
1466 /* fixed register + addr override doesn't go down all that well. */
1467 if (fPrefixes & DISPREFIX_ADDRSIZE)
1468 {
1469 Assert(pDis->fPrefix & DISPREFIX_ADDRSIZE);
1470 if ( pDis->pCurInstr->fParam3 == OP_PARM_NONE
1471 && pDis->pCurInstr->fParam2 == OP_PARM_NONE
1472 && ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1473 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END))
1474 return true;
1475 }
1476
1477 /* Almost all prefixes are bad for jumps. */
1478 if (fPrefixes)
1479 {
1480 switch (pDis->pCurInstr->uOpcode)
1481 {
1482 /* nop w/ prefix(es). */
1483 case OP_NOP:
1484 return true;
1485
1486 case OP_JMP:
1487 if ( pDis->pCurInstr->fParam1 != OP_PARM_Jb
1488 && pDis->pCurInstr->fParam1 != OP_PARM_Jv)
1489 break;
1490 /* fall thru */
1491 case OP_JO:
1492 case OP_JNO:
1493 case OP_JC:
1494 case OP_JNC:
1495 case OP_JE:
1496 case OP_JNE:
1497 case OP_JBE:
1498 case OP_JNBE:
1499 case OP_JS:
1500 case OP_JNS:
1501 case OP_JP:
1502 case OP_JNP:
1503 case OP_JL:
1504 case OP_JNL:
1505 case OP_JLE:
1506 case OP_JNLE:
1507 /** @todo branch hinting 0x2e/0x3e... */
1508 return true;
1509 }
1510
1511 }
1512
1513 /* All but the segment prefix is bad news for push/pop. */
1514 if (fPrefixes & ~DISPREFIX_SEG)
1515 {
1516 switch (pDis->pCurInstr->uOpcode)
1517 {
1518 case OP_POP:
1519 case OP_PUSH:
1520 if ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_SEG_START
1521 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_SEG_END)
1522 return true;
1523 if ( (fPrefixes & ~DISPREFIX_OPSIZE)
1524 && pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1525 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END)
1526 return true;
1527 break;
1528
1529 case OP_POPA:
1530 case OP_POPF:
1531 case OP_PUSHA:
1532 case OP_PUSHF:
1533 if (fPrefixes & ~DISPREFIX_OPSIZE)
1534 return true;
1535 break;
1536 }
1537 }
1538
1539 /* Implicit 8-bit register instructions doesn't mix with operand size. */
1540 if ( (fPrefixes & DISPREFIX_OPSIZE)
1541 && ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1542 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1543 || ( pDis->pCurInstr->fParam2 == OP_PARM_Gb /* r8 */
1544 && pDis->pCurInstr->fParam1 == OP_PARM_Eb /* r8/mem8 */))
1545 )
1546 {
1547 switch (pDis->pCurInstr->uOpcode)
1548 {
1549 case OP_ADD:
1550 case OP_OR:
1551 case OP_ADC:
1552 case OP_SBB:
1553 case OP_AND:
1554 case OP_SUB:
1555 case OP_XOR:
1556 case OP_CMP:
1557 return true;
1558 default:
1559 break;
1560 }
1561 }
1562
1563 /* Instructions taking no address or operand which thus may be annoyingly
1564 difficult to format for yasm. */
1565 if (fPrefixes)
1566 {
1567 switch (pDis->pCurInstr->uOpcode)
1568 {
1569 case OP_STI:
1570 case OP_STC:
1571 case OP_CLI:
1572 case OP_CLD:
1573 case OP_CLC:
1574 case OP_INT:
1575 case OP_INT3:
1576 case OP_INTO:
1577 case OP_HLT:
1578 /** @todo Many more to can be added here. */
1579 return true;
1580 default:
1581 break;
1582 }
1583 }
1584
1585 /* FPU and other instructions that ignores operand size override. */
1586 if (fPrefixes & DISPREFIX_OPSIZE)
1587 {
1588 switch (pDis->pCurInstr->uOpcode)
1589 {
1590 /* FPU: */
1591 case OP_FIADD:
1592 case OP_FIMUL:
1593 case OP_FISUB:
1594 case OP_FISUBR:
1595 case OP_FIDIV:
1596 case OP_FIDIVR:
1597 /** @todo there are many more. */
1598 return true;
1599
1600 case OP_MOV:
1601 /** @todo could be that we're not disassembling these correctly. */
1602 if (pDis->pCurInstr->fParam1 == OP_PARM_Sw)
1603 return true;
1604 /** @todo what about the other way? */
1605 break;
1606
1607 default:
1608 break;
1609 }
1610 }
1611
1612
1613 /*
1614 * Check for the version of xyz reg,reg instruction that the assembler doesn't use.
1615 *
1616 * For example:
1617 * expected: 1aee sbb ch, dh ; SBB r8, r/m8
1618 * yasm: 18F5 sbb ch, dh ; SBB r/m8, r8
1619 */
1620 if (pDis->ModRM.Bits.Mod == 3 /* reg,reg */)
1621 {
1622 switch (pDis->pCurInstr->uOpcode)
1623 {
1624 case OP_ADD:
1625 case OP_OR:
1626 case OP_ADC:
1627 case OP_SBB:
1628 case OP_AND:
1629 case OP_SUB:
1630 case OP_XOR:
1631 case OP_CMP:
1632 if ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1633 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1634 || ( pDis->pCurInstr->fParam1 == OP_PARM_Gv /* rX */
1635 && pDis->pCurInstr->fParam2 == OP_PARM_Ev /* rX/memX */))
1636 return true;
1637
1638 /* 82 (see table A-6). */
1639 if (pDis->bOpCode == 0x82)
1640 return true;
1641 break;
1642
1643 /* ff /0, fe /0, ff /1, fe /0 */
1644 case OP_DEC:
1645 case OP_INC:
1646 return true;
1647
1648 case OP_POP:
1649 case OP_PUSH:
1650 Assert(pDis->bOpCode == 0x8f);
1651 return true;
1652
1653 case OP_MOV:
1654 if ( pDis->bOpCode == 0x8a
1655 || pDis->bOpCode == 0x8b)
1656 return true;
1657 break;
1658
1659 default:
1660 break;
1661 }
1662 }
1663
1664 /* shl eax,1 will be assembled to the form without the immediate byte. */
1665 if ( pDis->pCurInstr->fParam2 == OP_PARM_Ib
1666 && (uint8_t)pDis->Param2.uValue == 1)
1667 {
1668 switch (pDis->pCurInstr->uOpcode)
1669 {
1670 case OP_SHL:
1671 case OP_SHR:
1672 case OP_SAR:
1673 case OP_RCL:
1674 case OP_RCR:
1675 case OP_ROL:
1676 case OP_ROR:
1677 return true;
1678 }
1679 }
1680
1681 /* And some more - see table A-6. */
1682 if (pDis->bOpCode == 0x82)
1683 {
1684 switch (pDis->pCurInstr->uOpcode)
1685 {
1686 case OP_ADD:
1687 case OP_OR:
1688 case OP_ADC:
1689 case OP_SBB:
1690 case OP_AND:
1691 case OP_SUB:
1692 case OP_XOR:
1693 case OP_CMP:
1694 return true;
1695 break;
1696 }
1697 }
1698
1699
1700 /* check for REX.X = 1 without SIB. */
1701
1702 /* Yasm encodes setnbe al with /2 instead of /0 like the AMD manual
1703 says (intel doesn't appear to care). */
1704 switch (pDis->pCurInstr->uOpcode)
1705 {
1706 case OP_SETO:
1707 case OP_SETNO:
1708 case OP_SETC:
1709 case OP_SETNC:
1710 case OP_SETE:
1711 case OP_SETNE:
1712 case OP_SETBE:
1713 case OP_SETNBE:
1714 case OP_SETS:
1715 case OP_SETNS:
1716 case OP_SETP:
1717 case OP_SETNP:
1718 case OP_SETL:
1719 case OP_SETNL:
1720 case OP_SETLE:
1721 case OP_SETNLE:
1722 AssertMsg(pDis->bOpCode >= 0x90 && pDis->bOpCode <= 0x9f, ("%#x\n", pDis->bOpCode));
1723 if (pDis->ModRM.Bits.Reg != 2)
1724 return true;
1725 break;
1726 }
1727
1728 /*
1729 * The MOVZX reg32,mem16 instruction without an operand size prefix
1730 * doesn't quite make sense...
1731 */
1732 if ( pDis->pCurInstr->uOpcode == OP_MOVZX
1733 && pDis->bOpCode == 0xB7
1734 && (pDis->uCpuMode == DISCPUMODE_16BIT) != !!(fPrefixes & DISPREFIX_OPSIZE))
1735 return true;
1736
1737 return false;
1738}
1739
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette