Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

764 lines
21 KiB

  1. /* disasm.c
  2. Future Features -
  3. Current bugs -
  4. Data32 for
  5. (callf fword ptr [mem]), (jmpf fword ptr [mem])
  6. Floating point insns
  7. Call not tested
  8. jecxz disassembled as large_address, not large_data
  9. lidt/lgdt are 6-byte operands
  10. segload doesn't set memXxxxx vars
  11. some 0x0f opcodes should set gpSafe flag
  12. bt, bts, btr, btc
  13. SetBcc [mem]
  14. SHD[l,r]
  15. Usage:
  16. Call DisAsm86(code ptr)
  17. gpRegs = 0
  18. gpSafe = 0
  19. If we can continue,
  20. set gpSafe = 1
  21. if instruction is POP SEGREG
  22. gpRegs |= POPSEG
  23. else
  24. gpInsLen = length of instruction
  25. gpRegs |= regs modified (SegLoad or String)
  26. endif
  27. endif
  28. */
  29. /* #include <string.h> */
  30. #include <windows.h> /* wsprintf() */
  31. /* Disasm.h - definitions for Don's Tiny Disassembler */
  32. typedef unsigned long dword;
  33. typedef unsigned short word;
  34. typedef unsigned char byte;
  35. extern word memOp; /* actual operation performed */
  36. extern char *memName[]; /* name corresponding to memOp */
  37. enum { memNOP, memRead, memWrite, memRMW, memSegReg, memSegMem};
  38. extern word memSeg; /* value of segment of memory address */
  39. extern dword memLinear, /* offset of operand */
  40. memLinear2;
  41. extern word memSeg2, /* duplicate of above if dual mem op */
  42. memSize2, memOp2,
  43. memDouble; /* true if two-mem-operand instruction */
  44. extern word memSize; /* bytes of memory of operation */
  45. enum { MemByte=1, MemWord=2, MemDWord=4, MemQWord=8, MemTword=10,
  46. Adr4, Adr6=6};
  47. enum { memNoSeg, memES, memCS, memSS, memDS, memFS, memGS};
  48. enum {strCX=1, strSI=2, strDI=4, segDS=8, segES=16, segFS=32, segGS=64};
  49. extern word gpSafe, /* 1 if may continue instruction */
  50. gpRegs, /* regs which instruction modifies as side effect */
  51. gpStack; /* amount stack is changed by */
  52. #define SHERLOCK 1
  53. #if SHERLOCK
  54. #define STATIC /*static*/
  55. #ifdef WOW
  56. // Note: The functions in this file were moved to the _MISCTEXT code segment
  57. // because _TEXT was exceeding the 64K segment limit a-craigj
  58. STATIC void InitDisAsm86(void);
  59. STATIC byte GetByte(void);
  60. STATIC word GetWord(void);
  61. STATIC long GetDWord(void);
  62. STATIC int GetImmAdr(int w);
  63. STATIC int GetImmData(int w);
  64. void PopSeg(int seg);
  65. STATIC void ModRMGeneral(byte op);
  66. STATIC void F(void);
  67. STATIC void DisAsmF(void);
  68. int IsPrefix(byte op0);
  69. int FAR DisAsm86(byte far *codeParm);
  70. #pragma alloc_text(_MISCTEXT,DisAsm86)
  71. #pragma alloc_text(_MISCTEXT,IsPrefix)
  72. #pragma alloc_text(_MISCTEXT,DisAsmF)
  73. #pragma alloc_text(_MISCTEXT,F)
  74. #pragma alloc_text(_MISCTEXT,ModRMGeneral)
  75. #pragma alloc_text(_MISCTEXT,PopSeg)
  76. #pragma alloc_text(_MISCTEXT,GetImmData)
  77. #pragma alloc_text(_MISCTEXT,GetImmAdr)
  78. #pragma alloc_text(_MISCTEXT,GetDWord)
  79. #pragma alloc_text(_MISCTEXT,GetWord)
  80. #pragma alloc_text(_MISCTEXT,GetByte)
  81. #pragma alloc_text(_MISCTEXT,InitDisAsm86)
  82. #endif
  83. #define NO_MEM
  84. /* int gpTrying = 0, gpEnable = 1, gpInsLen = 0; */
  85. extern int gpTrying, gpEnable, gpInsLen;
  86. extern word gpSafe, gpRegs, gpStack; /* indicate side effects of instruction */
  87. STATIC byte lookup[256] = {0}; /* lookup table for first byte of opcode */
  88. STATIC int dataSize=0, adrSize=0, /* flag to indicate 32 bit data/code */
  89. segSize=0; /* flag if 32 bit code segment */
  90. enum { /* operand decoding classes */
  91. UNK, /*NOOP, BREG, VREG, SREG, */ BWI, /*BRI, WRI,*/
  92. SMOV, IMOV, /*IBYTE, IWORD, JMPW, JMPB, LEA, JCond,
  93. GrpF,*/ Grp1, Grp2, Grp3, Grp4, Grp5, /*IADR, */ MOVABS,
  94. RRM, RRMW, /*IMUL,*/ POPMEM, /*TEST, ENTER, FLOP, ARPL,
  95. INOUT, IWORD1, ASCII, */ XLAT,
  96. };
  97. #define opBase 0
  98. STATIC struct {
  99. /* char *name; /* opcode mnemonic */
  100. byte base, count; /* first table entry, number of entries */
  101. byte operand; /* operand class */
  102. } ops[] = {
  103. #define NoText(n, b, c, o) b, c, o
  104. NoText("?UNKNOWN", 0, 0, UNK),
  105. NoText("add", 0x00, 6, BWI),
  106. NoText("or", 0x08, 6, BWI),
  107. /* NoText("FGrp", 0x0f, 1, GrpF), */
  108. NoText("adc", 0x10, 6, BWI),
  109. NoText("sbb", 0x18, 6, BWI),
  110. NoText("and", 0x20, 6, BWI),
  111. NoText("sub", 0x28, 6, BWI),
  112. NoText("xor", 0x30, 6, BWI),
  113. NoText("cmp", 0x38, 6, BWI),
  114. /* NoText("inc", 0x40, 8, VREG), */
  115. /* "dec", 0x48, 8, VREG, */
  116. /* NoText("push", 0x50, 8, VREG), */
  117. /* "pop", 0x58, 8, VREG, */
  118. NoText("bound", 0x62, 1, RRMW),
  119. /* "arpl", 0x63, 1, ARPL, */
  120. /* NoText("push", 0x68, 1, IWORD), */
  121. /* "imul", 0x69, 3, IMUL, */
  122. /* NoText("push", 0x6a, 1, IBYTE), */
  123. /* "jcond", 0x70, 16, JCond, */
  124. NoText("Grp1", 0x80, 4, Grp1),
  125. NoText("test", 0x84, 2, RRM),
  126. NoText("xchg", 0x86, 2, RRM),
  127. NoText("mov", 0x88, 4, BWI),
  128. NoText("mov", 0x8c, 3, SMOV),
  129. /* NoText("lea", 0x8d, 1, LEA), */
  130. NoText("pop", 0x8f, 1, POPMEM),
  131. /* NoText("xchg", 0x90, 8, VREG), */
  132. /* NoText("callf", 0x9a, 1, IADR), */
  133. NoText("mov", 0xa0, 4, MOVABS),
  134. /* NoText("test", 0xa8, 2, TEST), */
  135. /* NoText("mov", 0xb0, 8, BRI), */
  136. /* NoText("mov", 0xb8, 8, WRI), */
  137. NoText("Grp2", 0xc0, 2, Grp2),
  138. /* NoText("retn", 0xc2, 1, IWORD1), */
  139. NoText("les", 0xc4, 1, RRMW),
  140. NoText("lds", 0xc5, 1, RRMW),
  141. NoText("mov", 0xc6, 2, IMOV),
  142. /* NoText("enter", 0xc8, 1, ENTER), */
  143. /* NoText("retf", 0xca, 1, IWORD1), */
  144. /* NoText("int", 0xcd, 1, IBYTE), */
  145. NoText("Grp2", 0xd0, 4, Grp2),
  146. /* NoText("aam", 0xd4, 1, ASCII), */
  147. /* NoText("aad", 0xd5, 1, ASCII), */
  148. NoText("xlat", 0xd7, 1, XLAT),
  149. /* NoText("float", 0xd8, 8, FLOP), */
  150. /* NoText("loopne", 0xe0, 1, JMPB), */
  151. /* NoText("loope", 0xe1, 1, JMPB), */
  152. /* NoText("loop", 0xe2, 1, JMPB), */
  153. /* NoText("jcxz", 0xe3, 1, JMPB), */
  154. /* NoText("in", 0xe4, 2, INOUT), */
  155. /* NoText("out", 0xe6, 2, INOUT), */
  156. /* NoText("call", 0xe8, 1, JMPW), */
  157. /* NoText("jmp", 0xe9, 1, JMPW), */
  158. /* NoText("jmpf", 0xea, 1, IADR), */
  159. /* NoText("jmp", 0xeb, 1, JMPB), */
  160. NoText("Grp3", 0xf6, 2, Grp3),
  161. NoText("Grp4", 0xfe, 1, Grp4),
  162. NoText("Grp5", 0xff, 1, Grp5),
  163. };
  164. #define opCnt (sizeof(ops)/sizeof(ops[0]))
  165. #define simpleBase (opBase + opCnt)
  166. STATIC struct { /* these are single byte opcodes, no decode */
  167. byte val;
  168. /* char *name; */
  169. } simple[] = {
  170. #define NoText2(v, n) v
  171. /* NoText2(0x06, "push es"), */
  172. NoText2(0x07, "pop es"),
  173. /* NoText2(0x0e, "push cs"), */
  174. /* NoText2(0x16, "push ss"), */
  175. /* NoText2(0x17, "pop ss"), */
  176. /* NoText2(0x1e, "push ds"), */
  177. NoText2(0x1f, "pop ds"),
  178. /* NoText2(0x27, "daa"), */
  179. /* NoText2(0x2f, "das"), */
  180. /* NoText2(0x37, "aaa"), */
  181. /* NoText2(0x3f, "aas"), */
  182. /* NoText2(0x90, "nop"), */
  183. /* NoText2(0x9b, "wait"), */
  184. /* NoText2(0x9e, "sahf"), */
  185. /* NoText2(0x9f, "lahf"), */
  186. /* NoText2(0xc3, "retn"), */
  187. /* NoText2(0xc9, "leave"), */
  188. /* NoText2(0xcb, "retf"), */
  189. /* NoText2(0xcc, "int 3"), */
  190. /* NoText2(0xce, "into"), */
  191. /* NoText2(0xec, "in al), dx", */
  192. /* NoText2(0xee, "out dx), al", */
  193. /* NoText2(0xf0, "lock"), */
  194. /* NoText2(0xf2, "repne"), */
  195. /* NoText2(0xf3, "rep/repe"), */
  196. /* NoText2(0xf4, "hlt"), */
  197. /* NoText2(0xf5, "cmc"), */
  198. /* NoText2(0xf8, "clc"), */
  199. /* NoText2(0xf9, "stc"), */
  200. /* NoText2(0xfa, "cli"), */
  201. /* NoText2(0xfb, "sti"), */
  202. /* NoText2(0xfc, "cld"), */
  203. /* NoText2(0xfd, "std"), */
  204. };
  205. #define simpleCnt (sizeof(simple)/sizeof(simple[0]))
  206. #define dSimpleBase (simpleBase + simpleCnt)
  207. #if 0
  208. STATIC struct { /* these are simple opcodes that change */
  209. byte val; /* based on current data size */
  210. char *name, *name32;
  211. } dsimple[] = {
  212. /* 0x60, "pusha", "pushad", */
  213. /* 0x61, "popa", "popad", */
  214. /* 0x98, "cbw", "cwde", */
  215. /* 0x99, "cwd", "cdq", */
  216. /* 0x9c, "pushf", "pushfd", */
  217. /* 0x9d, "popf", "popfd", */
  218. /* 0xcf, "iret", "iretd", */
  219. /* 0xed, "in ax, dx", "in eax, dx", */
  220. /* 0xef, "out dx, ax", "out dx, eax", */
  221. };
  222. #define dSimpleCnt (sizeof(dsimple)/sizeof(dsimple[0]))
  223. #endif
  224. #define dSimpleCnt 0
  225. #define STR_S 1 /* string op, source regs */
  226. #define STR_D 2 /* string op, dest regs */
  227. #define STR_D_Read 4 /* string op, reads from dest regs */
  228. #define STR_NO_COND 8 /* rep ignores flags */
  229. #define stringOpBase (dSimpleBase+ dSimpleCnt)
  230. STATIC struct {
  231. byte val;
  232. /* char *name; */
  233. byte flag; /* should be 'next' to op, to pack nicely */
  234. } stringOp[] = {
  235. #define NoText3(v, n, f) v, f
  236. NoText3(0x6c, "ins", STR_D | STR_NO_COND),
  237. NoText3(0x6e, "outs", STR_S | STR_NO_COND),
  238. NoText3(0xa4, "movs", STR_S | STR_D | STR_NO_COND),
  239. NoText3(0xa6, "cmps", STR_S | STR_D | STR_D_Read),
  240. NoText3(0xaa, "stos", STR_D | STR_NO_COND),
  241. NoText3(0xac, "lods", STR_S | STR_NO_COND),
  242. NoText3(0xae, "scas", STR_D | STR_D_Read),
  243. };
  244. #define stringOpCnt (sizeof(stringOp)/sizeof(stringOp[0]))
  245. STATIC void InitDisAsm86(void) {
  246. int i, j;
  247. for (i=0; i<opCnt; i++) { /* Init complex entries */
  248. for (j=0; j<(int)ops[i].count; j++)
  249. lookup[ops[i].base+j] = (byte)i + opBase;
  250. }
  251. for (i=0; i<simpleCnt; i++) /* Init simple entries */
  252. lookup[simple[i].val] = (byte)(i + simpleBase);
  253. for (i=0; i<stringOpCnt; i++) { /* Init string op table */
  254. lookup[stringOp[i].val] = (byte)(i + stringOpBase);
  255. lookup[stringOp[i].val+1] = (byte)(i + stringOpBase);
  256. }
  257. } /* InitDisAsm86 */
  258. STATIC byte far *code = 0; /* this is ugly - it saves passing current */
  259. /* code position to all the GetByte() funcs */
  260. #define Mid(v) (((v) >> 3) & 7) /* extract middle 3 bits from a byte */
  261. /* If you don't want to return memory access info, #def NO_MEM */
  262. #if !defined(NO_MEM)
  263. /* global vars set by DisAsm() to indicate current instruction's memory */
  264. /* access type. */
  265. word memSeg, memSize, memOp; /* segment value, operand size, operation */
  266. word memSeg2, memSize2, memOp2, /* instruction may have two memory accesses */
  267. memDouble;
  268. dword memLinear, memLinear2; /* offset from segment of access */
  269. STATIC dword memReg, memDisp; /* used to pass information from GetReg()... */
  270. char *memName[] = { /* used to convert 'enum memOp' to ascii */
  271. "NOP",
  272. "Read",
  273. "Write",
  274. "RMW",
  275. "MovStr",
  276. };
  277. #define SetMemSize(s) memSize = s
  278. #define SetMemSeg(s) memSeg = regs[s+9]
  279. #define SetMemOp(o) memOp = o
  280. #define SetMemLinear(l) memLinear = l
  281. #define SetMemSeg2(s) memSeg2 = regs[s+9]
  282. #define SetMemOp2(o) memOp2 = o
  283. #define SetMemLinear2(l) memLinear2 = l
  284. #define ModMemLinear(l) memLinear += l
  285. #define SetMemReg(r) memReg = r
  286. #define SetMemDisp(d) memDisp = d
  287. #define Read_RMW(o) ((o) ? memRead : memRMW)
  288. #else
  289. #define SetMemSeg(s)
  290. #define SetMemSize(s)
  291. #define StMemOp(o)
  292. #define SetMemLinear(l)
  293. #define SetMemSeg2(s)
  294. #define StMemOp2(o)
  295. #define SetMemLinear2(l)
  296. #define ModMemLinear(l)
  297. #define SetMemReg(r)
  298. #define SetMemDisp(d)
  299. #define Read_RMW(o) 0
  300. #endif
  301. /******************** Register Decode *******************************/
  302. /* These helper functions return char pointers to register names.
  303. They are safe to call multiple times, as the return values are not
  304. stored in a single buffer. The ?Reg() functions are passed a register
  305. number. They mask this with 7, so you can pass in the raw opcode.
  306. The ?Mid() functions extract the register field from e.g. a ModRM byte.
  307. The Vxxx() functions look at dataSize to choose between 16 and 32 bit
  308. registers. The Xxxx() functions look at the passed in W bit, and then
  309. the dataSize global, do decide between 8, 16, and 32 bit registers.
  310. */
  311. /************************* Opcode Fetch ***************************/
  312. /* GetByte(), GetWord(), and GetDWord() read from the code segment */
  313. /* and increment the pointer appropriately. They also add the current */
  314. /* value to the hexData display, and set the MemDisp global in case the */
  315. /* value fetched was a memory displacement */
  316. STATIC byte GetByte(void) { /* Read one byte from code segment */
  317. return *code++;
  318. } /* GetByte */
  319. STATIC word GetWord(void) { /* Read two bytes from code seg */
  320. word w = *(word far *)code;
  321. code += 2;
  322. return w;
  323. } /* GetWord */
  324. STATIC long GetDWord(void) { /* Read four bytes from code seg */
  325. unsigned long l = *(long far *)code;
  326. code += 4;
  327. return l;
  328. } /* GetDWord */
  329. #define GetImmByte() GetByte()
  330. #define GetImmWord() GetWord()
  331. #define GetImmDWord() GetDWord()
  332. STATIC int GetImmAdr(int w) { /* Get an immediate address value */
  333. if (!w) return GetImmByte();
  334. else if (!adrSize) return GetImmWord();
  335. return (int)GetImmDWord();
  336. } /* GetImmAdr */
  337. STATIC int GetImmData(int w) { /* Get an immediate data value */
  338. if (!w) return GetImmByte();
  339. else if (!dataSize) return GetImmWord();
  340. return (int)GetImmDWord();
  341. } /* GetImmData */
  342. /************************* Helper Functions **************************/
  343. void PopSeg(int seg) {
  344. gpSafe = 1;
  345. gpRegs = seg;
  346. gpStack = 1;
  347. } /* PopSeg */
  348. enum {
  349. RegAX, RegCX, RegDX, RegBX, RegSP, RegBP, RegSI, RegDI
  350. };
  351. /* Based on the second byte of opcode, width flag, adrSize and dataSize, */
  352. /* determine the disassembly of the current instruction, and what */
  353. /* memory address was referenced */
  354. /* needinfo indicates that we need a size override on a memory operand */
  355. /* for example, "mov [bx], ax" is obviously a 16 bit move, while */
  356. /* "mov [bx], 0" could be 8, 16, or 32 bit. We add the proper */
  357. /* "mov word ptr [bx], 0" information. */
  358. /* The 'mem' parameter indicates the kind of operation, Read, Write, RMW */
  359. /* don't bother trying to understand this code without an Intel manual */
  360. /* and assembler nearby. :-) */
  361. STATIC void ModRMGeneral(byte op) {
  362. int mod = op >> 6;
  363. int rm = op & 7;
  364. if (adrSize) { /* do 32 bit addressing */
  365. if (mod == 3) return; /*XReg(w, rm); /* register operand */
  366. if (rm == 4) /* [esp+?] is special S-I-B style */
  367. GetByte();
  368. if (mod==1) GetImmAdr(0);
  369. else if (mod == 2) GetImmAdr(1);
  370. } else { /* do 16 bit addressing */
  371. if (mod == 3) return;/* XReg(w, rm); /* register operand */
  372. if (mod == 0 && rm == 6) /* [bp] becomes [mem16] */
  373. GetImmAdr(1);
  374. else if (mod) /* (mod3 already returned) */
  375. GetImmAdr(mod-1); /* mod==1 is byte, mod==2 is (d)word */
  376. }
  377. } /* ModRMGeneral */
  378. #define ModRMInfo(op, w, mem) ModRMGeneral(op)
  379. #define ModRM(op, w, mem) ModRMGeneral(op)
  380. STATIC void F(void) {
  381. ModRMGeneral(GetByte());
  382. gpSafe = 1;
  383. } /* F */
  384. #define ModRMF(m) F()
  385. /* Disassemble the 386 instructions whose first opcode is 0x0f */
  386. /* Sorry, but this is just too ugly to comment */
  387. STATIC void DisAsmF(void) {
  388. byte op0;
  389. op0 = GetByte();
  390. switch (op0 >> 4) { /* switch on top 4 bits of opcode */
  391. case 0:
  392. #if 0
  393. switch (op0 & 0xf) {
  394. case 0: /* grp6 - scary */
  395. case 1: /* grp7 - scary */
  396. case 2: /* lar */
  397. case 3: /* lsl */
  398. default:
  399. }
  400. #endif
  401. break;
  402. case 9: /* byte set on condition */
  403. ModRMF(memWrite);
  404. return;
  405. case 0xa:
  406. switch (op0 & 0xf) {
  407. case 0: return; /* "push fs"; */
  408. case 1:
  409. PopSeg(segFS);
  410. return; /* "pop fs"; */
  411. case 3: case 0xb: /* bts, bt */
  412. ModRMF(memRMW);
  413. return;
  414. case 4: case 0xc: /* shrd, shld */
  415. ModRMF(memRMW);
  416. GetImmData(0);
  417. return;
  418. case 5: case 0xd: /* shrd, shld */
  419. ModRMF(memRMW);
  420. return;
  421. case 6: /* cmpxchg */
  422. gpSafe = 1;
  423. ModRM(GetByte(), 0, memRMW);
  424. return;
  425. case 7: /* cmpxchg */
  426. ModRMF(memRMW);
  427. return;
  428. case 8: return; /*"push gs";*/
  429. case 9:
  430. PopSeg(segGS);
  431. return; /*"pop gs";*/
  432. case 0xf: /* imul */
  433. ModRMF(memRead);
  434. return;
  435. }
  436. break;
  437. case 0xb:
  438. switch (op0 & 0xf) {
  439. case 2: case 4: case 5:
  440. if (op0 & 2) {
  441. /* "lss"*/
  442. } else { /* : (op0 &1) ? "lgs" : "lfs"; */
  443. ModRMF(memRead);
  444. }
  445. return;
  446. case 3: case 0xb: /* btc, btr */
  447. ModRMF(memRMW);
  448. return;
  449. case 6: case 7: case 0xe: case 0xf: /* movsx, movzx */
  450. dataSize = 0;
  451. ModRMF(memRead);
  452. return;
  453. case 0xa:
  454. ModRMF(memRMW);
  455. GetImmData(0);
  456. return;
  457. case 0xc: case 0xd: /* bsr, bsf */
  458. ModRMF(memRead);
  459. return;
  460. }
  461. break;
  462. case 0xc:
  463. if (op0 > 0xc7) { /* bswap */
  464. return;
  465. }
  466. if (op0 < 0xc2) { /* xadd */
  467. ModRMF(memRMW);
  468. return;
  469. }
  470. break;
  471. default:
  472. break;
  473. }
  474. return;
  475. } /* DisAsmF */
  476. int IsPrefix(byte op0) {
  477. switch (op0) { /* check for prefix bytes */
  478. #define CSEG 0x2e
  479. #define DSEG 0x3e
  480. #define ESEG 0x26
  481. #define SSEG 0x36
  482. #define FSEG 0x64
  483. #define GSEG 0x65
  484. #define REP 0xf3
  485. #define REPNE 0xf2
  486. #define DATA32 0x66
  487. #define ADR32 0x67
  488. case CSEG: SetMemSeg(memCS); break;
  489. case DSEG: SetMemSeg(memDS); break;
  490. case ESEG: SetMemSeg(memES); break;
  491. case SSEG: SetMemSeg(memSS); break;
  492. case FSEG: SetMemSeg(memFS); break;
  493. case GSEG: SetMemSeg(memGS); break;
  494. case REP: gpRegs |= strCX; break;
  495. case REPNE: gpRegs |= strCX; break;
  496. case ADR32: adrSize = !adrSize; break;
  497. case DATA32:dataSize = !dataSize; break;
  498. default:
  499. return 0;
  500. }
  501. return 1;
  502. } /* IsPrefix */
  503. /* like, call this with a pointer to the instruction, it will return */
  504. /* the opcode bytes used in *len, and a pointer to the disassembled insn */
  505. int FAR DisAsm86(byte far *codeParm) {
  506. byte far *oldcode;
  507. byte op0, op1;
  508. byte opclass;
  509. static int init =0;
  510. if (!init) {
  511. InitDisAsm86();
  512. init = 1;
  513. }
  514. adrSize = dataSize = segSize;
  515. gpSafe = gpRegs = gpStack = 0;
  516. code = oldcode = codeParm;
  517. do {
  518. op0 = GetByte();
  519. } while (IsPrefix(op0));
  520. opclass = lookup[op0];
  521. StMemOp(memNOP);
  522. if (opclass >= simpleBase) { /* is it special */
  523. if (opclass >= stringOpBase) { /* string operations? */
  524. char cmd;
  525. opclass -= stringOpBase;
  526. cmd = stringOp[opclass].flag;
  527. if (cmd & STR_S) {
  528. gpRegs |= strSI;
  529. StMemOp(memRead);
  530. /* DS already set */
  531. SetMemLinear(memReg);
  532. if (cmd & STR_D) {
  533. gpRegs |= strDI;
  534. StMemOp2(cmd & STR_D_Read ? memRead : memWrite);
  535. SetMemSeg2(memES);
  536. SetMemLinear2(memReg);
  537. /* memDouble = 1; */
  538. }
  539. } else {
  540. gpRegs |= strDI;
  541. StMemOp(cmd & STR_D_Read ? memRead : memWrite);
  542. SetMemSeg(memES);
  543. SetMemLinear(memReg);
  544. }
  545. if (op0 & 1) {
  546. if (dataSize) SetMemSize(4);
  547. else SetMemSize(2);
  548. } else SetMemSize(1);
  549. } else if (opclass >= dSimpleBase) {
  550. opclass -= dSimpleBase;
  551. } else {
  552. if (op0 == 7)
  553. PopSeg(segES); /* pop ES */
  554. else if (op0 == 0x1f)
  555. PopSeg(segDS); /* pop DS */
  556. }
  557. goto DisAsmDone;
  558. }
  559. if (op0 == 0x0f) { /* is it an extended opcode? */
  560. DisAsmF();
  561. goto DisAsmDone;
  562. }
  563. switch (ops[opclass].operand) {
  564. case BWI: /* byte/word/immediate */
  565. gpSafe = 1;
  566. if (op0 & 4) {
  567. GetImmData(op0&1);
  568. } else {
  569. int i;
  570. op1 = GetByte();
  571. /* if ((op0 & 0xf8) == 0x38) i = memRead;
  572. else if ((op0 & 0xfe) == 0x88) i = memWrite;
  573. else Read_RMW(op0 & 2); */
  574. ModRM(op1, op0&1, i);
  575. }
  576. break;
  577. case Grp1: /* group 1 instructions */
  578. gpSafe = 1;
  579. op1 = GetByte();
  580. ModRMInfo(op1, op0&1, Mid(op1) == 7 ? memRead : memRMW);
  581. GetImmData((op0&3)==1);
  582. break;
  583. case Grp2: /* group 2 instructions */
  584. gpSafe = 1;
  585. op1 = GetByte();
  586. ModRMInfo(op1, op0&1, memRMW);
  587. if (!(op0 & 0x10)) GetImmData(0);
  588. break;
  589. case Grp3: /* group 3 instructions */
  590. gpSafe = 1;
  591. op1 = GetByte();
  592. ModRMInfo(op1, op0&1, Read_RMW(Mid(op1) <2 || Mid(op1) >3));
  593. if (Mid(op1) < 2) GetImmData(op0&1);
  594. break;
  595. case Grp4: /* group 4 instructions */
  596. op1 = GetByte();
  597. if (Mid(op1) > 1) ;
  598. else {
  599. ModRMInfo(op1, op0&1, memRMW);
  600. gpSafe = 1;
  601. }
  602. break;
  603. case Grp5: /* group 5 instructions */
  604. op1 = GetByte();
  605. if (Mid(op1) < 3) {
  606. gpSafe = 1;
  607. if (Mid(op1) == 2) {
  608. gpStack = -1 << dataSize;
  609. }
  610. }
  611. ModRMInfo(op1, op0&1, Read_RMW(Mid(op1) >= 2));
  612. break;
  613. case SMOV: /* segment move */
  614. gpSafe = 1;
  615. op1 = GetByte();
  616. dataSize = 0;
  617. ModRM(op1, 1, Read_RMW(op0&2));
  618. if (op0 & 2) { /* if moving _to_ SREG */
  619. switch (Mid(op1)) {
  620. case 0: gpRegs = segES; break;
  621. case 3: gpRegs = segDS; break;
  622. case 4: gpRegs = segFS; break;
  623. case 5: gpRegs = segGS; break;
  624. default: gpSafe = 0;
  625. }
  626. }
  627. break;
  628. case IMOV: /* immediate move to reg/mem */
  629. gpSafe = 1;
  630. op1 = GetByte();
  631. ModRMInfo(op1, op0&1, memWrite);
  632. GetImmData(op0&1);
  633. break;
  634. case MOVABS: /* move between accum and abs mem address */
  635. gpSafe = 1;
  636. GetImmAdr(1);
  637. StMemOp(op0&2 ? memWrite : memRead);
  638. break;
  639. case POPMEM:
  640. gpSafe = 1;
  641. gpStack = 1 << dataSize;
  642. ModRMInfo(GetByte(), 1, memWrite);
  643. break;
  644. case RRM: /* test and xchg */
  645. gpSafe = 1;
  646. op1 = GetByte();
  647. ModRM(op1, op0&1, memRMW);
  648. break;
  649. case RRMW: /* bound, les, lds */
  650. op1 = GetByte();
  651. switch (op0) {
  652. case 0xc4: /* les reg, [mem] */
  653. gpRegs = segES;
  654. gpSafe = 1;
  655. break;
  656. case 0xc5: /* lds reg, [mem] */
  657. gpRegs = segDS;
  658. gpSafe = 1;
  659. break;
  660. }
  661. ModRM(op1, 1, memRead);
  662. break;
  663. case XLAT:
  664. gpSafe = 1;
  665. StMemOp(memRead);
  666. break;
  667. default: ;
  668. }
  669. DisAsmDone:
  670. return (int)(code - oldcode);
  671. } /* DisAsm86 */
  672. #endif /* SHERLOCK */