MgDisasmBase.cpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  1. // MgDisasmBase.cpp: implementation of the CMgDisasmBase class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "MgAsmCom.h"
  6. #include "MgAsmComDef.h"
  7. //-------------------------------------------------------------------------------------------------------------------------
  8. //全局变量声明:
  9. // Bit combinations that can be potentially dangerous when executed:
  10. t_cmddata g_Dangerous[] =
  11. {
  12. { 0x00FFFF, 0x00DCF7, 0, 0, 0, 0, 0, C_DANGER95, "Win95/98 may crash when NEG ESP is executed" },
  13. { 0x00FFFF, 0x00D4F7, 0, 0, 0, 0, 0, C_DANGER95, "Win95/98 may crash when NOT ESP is executed" },
  14. { 0x00FFFF, 0x0020CD, 0, 0, 0, 0, 0, C_DANGER95, "Win95/98 may crash when VxD call is executed in user mode" },
  15. { 0xF8FFFF, 0xC8C70F, 0, 0, 0, 0, 1, C_DANGERLOCK, "LOCK CMPXCHG8B may crash some processors when executed" },
  16. { 0x000000, 0x000000, 0, 0, 0, 0, 0, 0, "" }
  17. };
  18. //-------------------------------------------------------------------------------------------------------------------------
  19. //
  20. //////////////////////////////////////////////////////////////////////
  21. // Construction/Destruction
  22. //////////////////////////////////////////////////////////////////////
  23. CMgDisasmBase::CMgDisasmBase()
  24. {
  25. //
  26. m_pDisasm = NULL ;
  27. //
  28. m_pCmd = NULL ; // Pointer to binary data
  29. m_pPFixup = NULL ; // Pointer to possible fixups or NULL
  30. m_ulSize = 0 ; // Remaining size of the command buffer
  31. m_nMode = 0 ; // Disassembly mode (DISASM_xxx)
  32. //
  33. m_ulDataSize = 0 ; // Size of data (1,2,4 bytes)
  34. m_ulAddrSize = 0 ; // Size of address (2 or 4 bytes)
  35. //
  36. m_nSegPrefix = 0 ; // Segment override prefix or SEG_UNDEF
  37. m_nHasRM = 0 ; // Command has ModR/M byte
  38. m_nHasSIB = 0 ; // Command has SIB byte
  39. m_nDispSize = 0 ; // Size of displacement (if any)
  40. m_nImmSize = 0 ; // Size of immediate data (if any)
  41. m_nSoftError = 0 ; // Noncritical disassembler error
  42. m_nDump = 0 ; // Current length of command dump
  43. m_nResult = 0 ; // Current length of disassembly
  44. m_nAddComment = 0 ; // Comment value of operand
  45. //
  46. m_nIDEAL = 0 ; // Force IDEAL decoding mode
  47. m_nLowercase = 0 ; // Force lowercase display
  48. m_nTabArguments = 1 ; // Tab between mnemonic and arguments
  49. m_nExtraSpace = 0 ; // Extra space between arguments
  50. m_nPutDefSeg = 0 ; // Display default segments in listing
  51. m_nShowMemSize = 0 ; // Always show memory size
  52. m_nShowNEAR = 0 ; // Show NEAR modifiers
  53. m_nShortStringCmds = 0 ; // Use short form of string commands
  54. m_nSizeSens = 0 ; // How to decode size-sensitive mnemonics
  55. m_nSymbolic = 0 ; // Show symbolic addresses in disasm
  56. m_nFarCalls = 0 ; // Accept far calls, returns & addresses
  57. m_nDecodeVxd = 0 ; // Decode VxD calls (Win95/98)
  58. m_nPrivileged = 0 ; // Accept privileged commands
  59. m_nIOCommand = 0 ; // Accept I/O commands
  60. m_nBadShift = 0 ; // Accept shift out of range 1..31
  61. m_nExtraPrefix = 0 ; // Accept superfluous prefixes
  62. m_nLockedbus = 0 ; // Accept LOCK prefixes
  63. m_nStackAlign = 0 ; // Accept unaligned stack operations
  64. m_nIsWindowsNT = 0 ; // When checking for dangers, assume NT
  65. }
  66. CMgDisasmBase::~CMgDisasmBase()
  67. {
  68. }
  69. //----------------------------------------------------------------------------
  70. // Disassemble name of 1, 2 or 4-byte general-purpose integer register and, if
  71. // requested and available, dump its contents. Parameter type changes decoding
  72. // of contents for some operand types.
  73. //----------------------------------------------------------------------------
  74. void CMgDisasmBase::DecodeRG(int index, int datasize, int type)
  75. {
  76. int sizeindex;
  77. char name[9];
  78. if ( m_nMode < DISASM_DATA)
  79. {
  80. return; // No need to decode
  81. }
  82. index &= 0x07;
  83. if( m_ulDataSize == 1 )
  84. {
  85. sizeindex = 0;
  86. }
  87. else if(m_ulDataSize == 2)
  88. {
  89. sizeindex = 1;
  90. }
  91. else if(m_ulDataSize == 4)
  92. {
  93. sizeindex = 2;
  94. }
  95. else
  96. {
  97. m_pDisasm->error = DAE_INTERN;
  98. return;
  99. }
  100. if( m_nMode >= DISASM_FILE )
  101. {
  102. strcpy(name, g_szRegName[sizeindex][index]);
  103. if(m_nLowercase)
  104. {
  105. strlwr(name);
  106. }
  107. if(type < PSEUDOOP) // Not a pseudooperand
  108. {
  109. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s"), name);
  110. }
  111. }
  112. }
  113. //----------------------------------------------------------------------------
  114. // Disassemble name of 80-bit floating-point register and, if available, dump
  115. // its contents.
  116. //----------------------------------------------------------------------------
  117. void CMgDisasmBase::DecodeST(int index, int pseudoop)
  118. {
  119. int i;
  120. char s[32];
  121. if (m_nMode < DISASM_FILE)
  122. {
  123. return; // No need to decode
  124. }
  125. index &= 0x07;
  126. i = sprintf(s, ("%s(%i)"), (m_nLowercase ? ("st") : ("ST")), index);
  127. if (pseudoop == 0)
  128. {
  129. strcpy(m_pDisasm->result + m_nResult, s);
  130. m_nResult += i;
  131. };
  132. }
  133. //----------------------------------------------------------------------------
  134. // Disassemble name of 64-bit MMX register.
  135. //----------------------------------------------------------------------------
  136. void CMgDisasmBase::DecodeMX(int index)
  137. {
  138. char *pr;
  139. if(m_nMode < DISASM_FILE)
  140. {
  141. return; // No need to decode
  142. }
  143. index &= 0x07;
  144. pr = m_pDisasm->result + m_nResult;
  145. m_nResult += sprintf(pr, ("%s%i"), (m_nLowercase ? ("mm") : ("MM")), index);
  146. }
  147. //----------------------------------------------------------------------------
  148. // Disassemble name of 64-bit 3DNow! register and, if available, dump its
  149. // contents.
  150. //----------------------------------------------------------------------------
  151. void CMgDisasmBase::DecodeNR(int index)
  152. {
  153. char *pr;
  154. if(m_nMode < DISASM_FILE)
  155. {
  156. return; // No need to decode
  157. }
  158. index &= 0x07;
  159. pr = m_pDisasm->result + m_nResult;
  160. m_nResult += sprintf(pr, ("%s%i"), (m_nLowercase ? ("mm") : ("MM")), index);
  161. }
  162. //----------------------------------------------------------------------------
  163. // Disassemble memory/register from the ModRM/SIB bytes and, if available, dump
  164. // address and contents of memory.
  165. //----------------------------------------------------------------------------
  166. void CMgDisasmBase::DecodeMR(int type)
  167. {
  168. int j, memonly, inmemory, seg;
  169. int c, sib;
  170. ulong dsize, regsize, addr;
  171. char s[TEXTLEN];
  172. //
  173. if( m_ulSize < 2 )
  174. {
  175. m_pDisasm->error = DAE_CROSS; // ModR/M byte outside the memory block
  176. return;
  177. }
  178. m_nHasRM = 1;
  179. dsize = regsize = m_ulDataSize; // Default size of addressed reg/memory
  180. memonly = 0; // Register in ModM field is allowed
  181. // Size and kind of addressed memory or register in ModM has no influence on
  182. // the command size, and exact calculations are omitted if only command size
  183. // is requested. If register is used, optype will be incorrect and we need
  184. // to correct it later.
  185. c = m_pCmd[1] & 0xC7; // Leave only Mod and M fields
  186. if( m_nMode >= DISASM_DATA )
  187. {
  188. // Register operand
  189. if ((c & 0xC0) == 0xC0)
  190. {
  191. inmemory = 0;
  192. }
  193. else
  194. {
  195. inmemory = 1; // Memory operand
  196. }
  197. switch(type)
  198. {
  199. case MRG: // Memory/register in ModRM byte
  200. {
  201. if (inmemory)
  202. {
  203. if(m_ulDataSize == 1)
  204. {
  205. m_pDisasm->memtype = DEC_BYTE;
  206. }
  207. else if(m_ulDataSize == 2)
  208. {
  209. m_pDisasm->memtype = DEC_WORD;
  210. }
  211. else
  212. {
  213. m_pDisasm->memtype = DEC_DWORD;
  214. }
  215. }
  216. break;
  217. }
  218. case MRJ: // Memory/reg in ModRM as JUMP target
  219. {
  220. if( m_ulDataSize != 2 && inmemory )
  221. {
  222. m_pDisasm->memtype = DEC_DWORD;
  223. }
  224. if( m_nMode >= DISASM_FILE && m_nShowNEAR != 0 )
  225. {
  226. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s "), ( m_nLowercase ? ("near") : ("NEAR") ) );
  227. }
  228. break;
  229. }
  230. case MR1: // 1-byte memory/register in ModRM byte
  231. {
  232. dsize = regsize = 1;
  233. if(inmemory)
  234. {
  235. m_pDisasm->memtype = DEC_BYTE;
  236. }
  237. break;
  238. }
  239. case MR2: // 2-byte memory/register in ModRM byte
  240. {
  241. dsize = regsize = 2;
  242. if(inmemory)
  243. {
  244. m_pDisasm->memtype = DEC_WORD;
  245. }
  246. break;
  247. }
  248. case MR4: // 4-byte memory/register in ModRM byte
  249. case RR4: // 4-byte memory/register (register only)
  250. {
  251. dsize = regsize = 4;
  252. if(inmemory)
  253. {
  254. m_pDisasm->memtype = DEC_DWORD;
  255. }
  256. break;
  257. }
  258. case MR8: // 8-byte memory/MMX register in ModRM
  259. case RR8: // 8-byte MMX register only in ModRM
  260. {
  261. dsize = 8;
  262. if(inmemory)
  263. {
  264. m_pDisasm->memtype = DEC_QWORD;
  265. }
  266. break;
  267. }
  268. case MRD: // 8-byte memory/3DNow! register in ModRM
  269. case RRD: // 8-byte memory/3DNow! (register only)
  270. {
  271. dsize = 8;
  272. if(inmemory)
  273. {
  274. m_pDisasm->memtype = DEC_3DNOW;
  275. }
  276. break;
  277. }
  278. case MMA: // Memory address in ModRM byte for LEA
  279. {
  280. memonly = 1;
  281. break;
  282. }
  283. case MML: // Memory in ModRM byte (for LES)
  284. {
  285. dsize = m_ulDataSize + 2;
  286. memonly = 1;
  287. if(m_ulDataSize == 4 && inmemory)
  288. {
  289. m_pDisasm->memtype = DEC_FWORD;
  290. }
  291. m_pDisasm->warnings |= DAW_SEGMENT;
  292. break;
  293. }
  294. case MMS: // Memory in ModRM byte (as SEG:OFFS)
  295. {
  296. dsize = m_ulDataSize + 2;
  297. memonly = 1;
  298. if(m_ulDataSize == 4 && inmemory)
  299. {
  300. m_pDisasm->memtype = DEC_FWORD;
  301. }
  302. if(m_nMode >= DISASM_FILE)
  303. {
  304. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s "), (m_nLowercase ? ("far") : ("FAR")) );
  305. }
  306. break;
  307. }
  308. case MM6: // Memory in ModRM (6-byte descriptor)
  309. {
  310. dsize = 6;
  311. memonly = 1;
  312. if(inmemory)
  313. {
  314. m_pDisasm->memtype = DEC_FWORD;
  315. }
  316. break;
  317. }
  318. case MMB: // Two adjacent memory locations (BOUND)
  319. {
  320. dsize = (m_nIDEAL ? m_ulDataSize : m_ulDataSize * 2);
  321. memonly = 1;
  322. break;
  323. }
  324. case MD2: // Memory in ModRM byte (16-bit integer)
  325. case MB2: // Memory in ModRM byte (16-bit binary)
  326. {
  327. dsize = 2;
  328. memonly = 1;
  329. if(inmemory)
  330. {
  331. m_pDisasm->memtype = DEC_WORD;
  332. }
  333. break;
  334. }
  335. case MD4: // Memory in ModRM byte (32-bit integer)
  336. {
  337. dsize = 4;
  338. memonly = 1;
  339. if(inmemory)
  340. {
  341. m_pDisasm->memtype = DEC_DWORD;
  342. }
  343. break;
  344. }
  345. case MD8: // Memory in ModRM byte (64-bit integer)
  346. {
  347. dsize = 8;
  348. memonly = 1;
  349. if(inmemory)
  350. {
  351. m_pDisasm->memtype = DEC_QWORD;
  352. }
  353. break;
  354. }
  355. case MDA: // Memory in ModRM byte (80-bit BCD)
  356. {
  357. dsize = 10;
  358. memonly = 1;
  359. if(inmemory)
  360. {
  361. m_pDisasm->memtype = DEC_TBYTE;
  362. }
  363. break;
  364. }
  365. case MF4: // Memory in ModRM byte (32-bit float)
  366. {
  367. dsize = 4;
  368. memonly = 1;
  369. if(inmemory)
  370. {
  371. m_pDisasm->memtype = DEC_FLOAT4;
  372. }
  373. break;
  374. }
  375. case MF8: // Memory in ModRM byte (64-bit float)
  376. {
  377. dsize = 8;
  378. memonly = 1;
  379. if(inmemory)
  380. {
  381. m_pDisasm->memtype = DEC_FLOAT8;
  382. }
  383. break;
  384. }
  385. case MFA: // Memory in ModRM byte (80-bit float)
  386. {
  387. dsize = 10;
  388. memonly = 1;
  389. if(inmemory)
  390. {
  391. m_pDisasm->memtype = DEC_FLOAT10;
  392. }
  393. break;
  394. }
  395. case MFE: // Memory in ModRM byte (FPU environment)
  396. {
  397. dsize = 28;
  398. memonly = 1;
  399. break;
  400. }
  401. case MFS: // Memory in ModRM byte (FPU state)
  402. {
  403. dsize = 108;
  404. memonly = 1;
  405. break;
  406. }
  407. case MFX: // Memory in ModRM byte (ext. FPU state)
  408. {
  409. dsize = 512;
  410. memonly = 1;
  411. break;
  412. }
  413. default: // Operand is not in ModM!
  414. {
  415. m_pDisasm->error = DAE_INTERN;
  416. break;
  417. }
  418. }
  419. }
  420. addr = 0;
  421. // There are many possibilities to decode ModM/SIB address. The first
  422. // possibility is register in ModM - general-purpose, MMX or 3DNow!
  423. if((c & 0xC0) == 0xC0) // Decode register operand
  424. {
  425. if(type == MR8 || type == RR8)
  426. {
  427. DecodeMX(c); // MMX register
  428. }
  429. else if(type == MRD || type == RRD)
  430. {
  431. DecodeNR(c); // 3DNow! register
  432. }
  433. else
  434. {
  435. DecodeRG(c, regsize, type); // General-purpose register
  436. }
  437. //
  438. if(memonly != 0)
  439. {
  440. m_nSoftError = DAE_MEMORY; // Register where only memory allowed
  441. }
  442. return;
  443. }
  444. // Next possibility: 16-bit addressing mode, very seldom in 32-bit flat model
  445. // but still supported by processor. SIB byte is never used here.
  446. if(m_ulAddrSize == 2)
  447. {
  448. if(c == 0x06) // Special case of immediate address
  449. {
  450. m_nDispSize = 2;
  451. if(m_ulSize < 4)
  452. {
  453. m_pDisasm->error = DAE_CROSS; // Disp16 outside the memory block
  454. }
  455. else if(m_nMode >= DISASM_DATA)
  456. {
  457. m_pDisasm->adrconst = addr = *(ushort *)(m_pCmd + 2);
  458. if (addr == 0)
  459. {
  460. m_pDisasm->zeroconst = 1;
  461. }
  462. seg = SEG_DS;
  463. Memadr(seg, (""), addr, dsize);
  464. }
  465. }
  466. else
  467. {
  468. m_pDisasm->indexed = 1;
  469. if((c & 0xC0) == 0x40) // 8-bit signed displacement
  470. {
  471. if(m_ulSize < 3)
  472. {
  473. m_pDisasm->error = DAE_CROSS;
  474. }
  475. else
  476. {
  477. addr = (signed char)m_pCmd[2] & 0xFFFF;
  478. }
  479. m_nDispSize = 1;
  480. }
  481. else if((c & 0xC0) == 0x80) // 16-bit unsigned displacement
  482. {
  483. if(m_ulSize < 4)
  484. {
  485. m_pDisasm->error = DAE_CROSS;
  486. }
  487. else
  488. {
  489. addr = *(ushort *)(m_pCmd + 2);
  490. }
  491. m_nDispSize = 2;
  492. }
  493. if(m_nMode >= DISASM_DATA && m_pDisasm->error == DAE_NOERR)
  494. {
  495. m_pDisasm->adrconst = addr;
  496. if(addr == 0)
  497. {
  498. m_pDisasm->zeroconst = 1;
  499. }
  500. seg = g_addr16[c & 0x07].defseg;
  501. Memadr(seg, g_addr16[c & 0x07].descr, addr, dsize);
  502. }
  503. }
  504. }
  505. // Next possibility: immediate 32-bit address.
  506. else if(c == 0x05) // Special case of immediate address
  507. {
  508. m_nDispSize = 4;
  509. if(m_ulSize < 6)
  510. {
  511. m_pDisasm->error = DAE_CROSS; // Disp32 outside the memory block
  512. }
  513. else if(m_nMode >= DISASM_DATA)
  514. {
  515. m_pDisasm->adrconst = addr = *(ulong *)(m_pCmd + 2);
  516. if(m_pPFixup == NULL)
  517. {
  518. m_pPFixup = m_pCmd + 2;
  519. }
  520. m_pDisasm->fixupsize += 4;
  521. if(addr == 0)
  522. {
  523. m_pDisasm->zeroconst = 1;
  524. }
  525. seg = SEG_DS;
  526. Memadr(seg, (""), addr, dsize);
  527. }
  528. }
  529. // Next possibility: 32-bit address with SIB byte.
  530. else if ((c & 0x07) == 0x04) // SIB addresation
  531. {
  532. sib = m_pCmd[2];
  533. m_nHasSIB = 1;
  534. *s = '\0';
  535. if(c == 0x04 && (sib & 0x07) == 0x05)
  536. {
  537. m_nDispSize = 4; // Immediate address without base
  538. if(m_ulSize < 7)
  539. {
  540. m_pDisasm->error = DAE_CROSS; // Disp32 outside the memory block
  541. }
  542. else
  543. {
  544. m_pDisasm->adrconst = addr = *(ulong *)(m_pCmd + 3);
  545. if(m_pPFixup == NULL)
  546. {
  547. m_pPFixup = m_pCmd + 3;
  548. }
  549. m_pDisasm->fixupsize += 4;
  550. if(addr == 0)
  551. {
  552. m_pDisasm->zeroconst = 1;
  553. }
  554. if ((sib & 0x38) != 0x20) // Index register present
  555. {
  556. m_pDisasm->indexed = 1;
  557. if (type == MRJ)
  558. {
  559. m_pDisasm->jmptable = addr;
  560. }
  561. }
  562. seg = SEG_DS;
  563. }
  564. }
  565. else // Base and, eventually, displacement
  566. {
  567. if ((c & 0xC0) == 0x40) // 8-bit displacement
  568. {
  569. m_nDispSize = 1;
  570. if(m_ulSize < 4)
  571. {
  572. m_pDisasm->error = DAE_CROSS;
  573. }
  574. else
  575. {
  576. m_pDisasm->adrconst = addr = (signed char)m_pCmd[3];
  577. if(addr == 0)
  578. {
  579. m_pDisasm->zeroconst = 1;
  580. }
  581. }
  582. }
  583. else if ((c & 0xC0) == 0x80) // 32-bit displacement
  584. {
  585. m_nDispSize = 4;
  586. if (m_ulSize < 7)
  587. {
  588. m_pDisasm->error = DAE_CROSS; // Disp32 outside the memory block
  589. }
  590. else
  591. {
  592. m_pDisasm->adrconst = addr = *(ulong *)(m_pCmd + 3);
  593. if (m_pPFixup == NULL)
  594. {
  595. m_pPFixup = m_pCmd + 3;
  596. }
  597. m_pDisasm->fixupsize += 4;
  598. if (addr == 0)
  599. {
  600. m_pDisasm->zeroconst = 1;
  601. }
  602. // Most compilers use address of type [index*4+displacement] to
  603. // address jump table (switch). But, for completeness, I allow all
  604. // cases which include index with scale 1 or 4, base or both.
  605. if (type == MRJ)
  606. {
  607. m_pDisasm->jmptable = addr;
  608. }
  609. }
  610. }
  611. m_pDisasm->indexed = 1;
  612. j = sib & 0x07;
  613. if (m_nMode >= DISASM_FILE)
  614. {
  615. strcpy(s, g_szRegName[2][j]);
  616. seg = g_addr32[j].defseg;
  617. }
  618. }
  619. if ((sib & 0x38) != 0x20) // Scaled index present
  620. {
  621. if ((sib & 0xC0) == 0x40)
  622. {
  623. m_pDisasm->indexed = 2;
  624. }
  625. else if ((sib & 0xC0) == 0x80)
  626. {
  627. m_pDisasm->indexed = 4;
  628. }
  629. else if ((sib & 0xC0) == 0xC0)
  630. {
  631. m_pDisasm->indexed = 8;
  632. }
  633. else
  634. {
  635. m_pDisasm->indexed = 1;
  636. }
  637. }
  638. if(m_nMode >= DISASM_FILE && m_pDisasm->error == DAE_NOERR)
  639. {
  640. if ((sib & 0x38) != 0x20) // Scaled index present
  641. {
  642. if (*s != '\0')
  643. {
  644. strcat(s, ("+"));
  645. }
  646. strcat(s, g_addr32[(sib >> 3) & 0x07].descr);
  647. if ((sib & 0xC0) == 0x40)
  648. {
  649. m_pDisasm->jmptable = 0; // Hardly a switch!
  650. strcat(s, ("*2"));
  651. }
  652. else if ((sib & 0xC0) == 0x80)
  653. {
  654. strcat(s, ("*4"));
  655. }
  656. else if ((sib & 0xC0) == 0xC0)
  657. {
  658. m_pDisasm->jmptable = 0; // Hardly a switch!
  659. strcat(s, ("*8"));
  660. }
  661. }
  662. Memadr(seg, s, addr, dsize);
  663. }
  664. }
  665. // Last possibility: 32-bit address without SIB byte.
  666. else // No SIB
  667. {
  668. if ((c & 0xC0) == 0x40)
  669. {
  670. m_nDispSize = 1;
  671. if (m_ulSize < 3)
  672. {
  673. m_pDisasm->error = DAE_CROSS; // Disp8 outside the memory block
  674. }
  675. else
  676. {
  677. m_pDisasm->adrconst = addr = (signed char)m_pCmd[2];
  678. if (addr == 0)
  679. {
  680. m_pDisasm->zeroconst = 1;
  681. }
  682. }
  683. }
  684. else if ((c & 0xC0) == 0x80)
  685. {
  686. m_nDispSize = 4;
  687. if (m_ulSize < 6)
  688. {
  689. m_pDisasm->error = DAE_CROSS; // Disp32 outside the memory block
  690. }
  691. else
  692. {
  693. m_pDisasm->adrconst = addr = *(ulong *)(m_pCmd + 2);
  694. if (m_pPFixup == NULL)
  695. {
  696. m_pPFixup = m_pCmd + 2;
  697. }
  698. m_pDisasm->fixupsize += 4;
  699. if (addr == 0)
  700. {
  701. m_pDisasm->zeroconst = 1;
  702. }
  703. if (type == MRJ)
  704. {
  705. m_pDisasm->jmptable = addr;
  706. }
  707. }
  708. }
  709. m_pDisasm->indexed = 1;
  710. if (m_nMode >= DISASM_FILE && m_pDisasm->error == DAE_NOERR)
  711. {
  712. seg = g_addr32[c & 0x07].defseg;
  713. Memadr(seg, g_addr32[c & 0x07].descr, addr, dsize);
  714. }
  715. }
  716. }
  717. //----------------------------------------------------------------------------
  718. // Disassemble implicit source of string operations and, if available, dump
  719. // address and contents.
  720. //----------------------------------------------------------------------------
  721. void CMgDisasmBase::DecodeSO(void)
  722. {
  723. if (m_nMode < DISASM_FILE)
  724. {
  725. return; // No need to decode
  726. }
  727. if (m_ulDataSize == 1)
  728. {
  729. m_pDisasm->memtype = DEC_BYTE;
  730. }
  731. else if (m_ulDataSize == 2)
  732. {
  733. m_pDisasm->memtype = DEC_WORD;
  734. }
  735. else if (m_ulDataSize == 4)
  736. {
  737. m_pDisasm->memtype = DEC_DWORD;
  738. }
  739. m_pDisasm->indexed = 1;
  740. Memadr(SEG_DS, g_szRegName[m_ulAddrSize == 2 ? 1 : 2][REG_ESI], 0L, m_ulDataSize);
  741. }
  742. //----------------------------------------------------------------------------
  743. // Disassemble implicit destination of string operations and, if available,
  744. // dump address and contents. Destination always uses segment ES, and this
  745. // setting cannot be overridden.
  746. //----------------------------------------------------------------------------
  747. void CMgDisasmBase::DecodeDE(void)
  748. {
  749. int seg;
  750. if (m_nMode < DISASM_FILE)
  751. {
  752. return; // No need to decode
  753. }
  754. if (m_ulDataSize == 1)
  755. {
  756. m_pDisasm->memtype = DEC_BYTE;
  757. }
  758. else if (m_ulDataSize == 2)
  759. {
  760. m_pDisasm->memtype = DEC_WORD;
  761. }
  762. else if (m_ulDataSize == 4)
  763. {
  764. m_pDisasm->memtype = DEC_DWORD;
  765. }
  766. m_pDisasm->indexed = 1;
  767. seg = m_nSegPrefix;
  768. m_nSegPrefix = SEG_ES; // Fake Memadr by changing segment prefix
  769. Memadr(SEG_DS, g_szRegName[m_ulAddrSize == 2 ? 1 : 2][REG_EDI], 0L, m_ulDataSize);
  770. m_nSegPrefix = seg; // Restore segment prefix
  771. }
  772. //----------------------------------------------------------------------------
  773. // Decode XLAT operand and, if available, dump address and contents.
  774. //----------------------------------------------------------------------------
  775. void CMgDisasmBase::DecodeXL(void)
  776. {
  777. if (m_nMode < DISASM_FILE)
  778. {
  779. return; // No need to decode
  780. }
  781. m_pDisasm->memtype = DEC_BYTE;
  782. m_pDisasm->indexed = 1;
  783. Memadr(SEG_DS, (m_ulAddrSize == 2 ? ("BX+AL") : ("EBX+AL")), 0L, 1);
  784. }
  785. //----------------------------------------------------------------------------
  786. // Decode immediate operand of size constsize. If sxt is non-zero, byte operand
  787. // should be sign-extended to sxt bytes. If type of immediate constant assumes
  788. // this, small negative operands may be displayed as signed negative numbers.
  789. // Note that in most cases immediate operands are not shown in comment window.
  790. //----------------------------------------------------------------------------
  791. void CMgDisasmBase::DecodeIM(int constsize, int sxt, int type)
  792. {
  793. int i;
  794. signed long data;
  795. ulong l;
  796. char name[TEXTLEN] = {0}, comment[TEXTLEN] = {0};
  797. //
  798. m_nImmSize += constsize; // Allows several immediate operands
  799. if (m_nMode < DISASM_DATA)
  800. {
  801. return;
  802. }
  803. l = 1 + m_nHasRM + m_nHasSIB + m_nDispSize + (m_nImmSize - constsize);
  804. data = 0;
  805. //
  806. if (m_ulSize < l + constsize)
  807. {
  808. m_pDisasm->error = DAE_CROSS;
  809. }
  810. else if (constsize == 1)
  811. {
  812. if (sxt == 0)
  813. {
  814. data = (uchar)m_pCmd[l];
  815. }
  816. else
  817. {
  818. data = (signed char)m_pCmd[l];
  819. }
  820. if (type == IMS && ((data & 0xE0) != 0 || data == 0))
  821. {
  822. m_pDisasm->warnings |= DAW_SHIFT;
  823. m_pDisasm->cmdtype |= C_RARE;
  824. }
  825. }
  826. else if (constsize == 2)
  827. {
  828. if (sxt == 0)
  829. {
  830. data = *(ushort *)(m_pCmd + l);
  831. }
  832. else
  833. {
  834. data = *(short *)(m_pCmd + l);
  835. }
  836. }
  837. else
  838. {
  839. data = *(long *)(m_pCmd + l);
  840. if (m_pPFixup == NULL)
  841. {
  842. m_pPFixup = m_pCmd + l;
  843. }
  844. m_pDisasm->fixupsize += 4;
  845. }
  846. if (sxt == 2)
  847. {
  848. data &= 0x0000FFFF;
  849. }
  850. if (data == 0 && m_pDisasm->error == 0)
  851. {
  852. m_pDisasm->zeroconst = 1;
  853. }
  854. // Command ENTER, as an exception from Intel's rules, has two immediate
  855. // constants. As the second constant is rarely used, I exclude it from
  856. // search if the first constant is non-zero (which is usually the case).
  857. if (m_pDisasm->immconst == 0)
  858. {
  859. m_pDisasm->immconst = data;
  860. }
  861. if (m_nMode >= DISASM_FILE && m_pDisasm->error == DAE_NOERR)
  862. {
  863. if (m_nMode >= DISASM_CODE && type != IMU)
  864. {
  865. i = Decodeaddress(data, name, TEXTLEN - m_nResult - 24, comment);
  866. }
  867. else
  868. {
  869. i = 0;
  870. comment[0] = '\0';
  871. }
  872. if (i != 0 && m_nSymbolic != 0)
  873. {
  874. strcpy(m_pDisasm->result + m_nResult, name);
  875. m_nResult += i;
  876. }
  877. else if (type == IMU || type == IMS || type == IM2 || data >= 0 || data < NEGLIMIT)
  878. {
  879. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%lX"), data);
  880. }
  881. else
  882. {
  883. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("-%lX"), -data);
  884. }
  885. //
  886. if (m_nAddComment && comment[0] != '\0')
  887. {
  888. strcpy(m_pDisasm->comment, comment);
  889. }
  890. }
  891. }
  892. //----------------------------------------------------------------------------
  893. // Decode VxD service name (always 4-byte).
  894. //----------------------------------------------------------------------------
  895. void CMgDisasmBase::DecodeVX(void)
  896. {
  897. ulong l, data;
  898. //
  899. m_nImmSize += 4; // Allows several immediate operands
  900. if (m_nMode < DISASM_DATA)
  901. {
  902. return;
  903. }
  904. l = 1 + m_nHasRM + m_nHasSIB + m_nDispSize + (m_nImmSize - 4);
  905. if (m_ulSize < l + 4)
  906. {
  907. m_pDisasm->error = DAE_CROSS;
  908. return;
  909. }
  910. data = *(long *)(m_pCmd + l);
  911. if (data == 0 && m_pDisasm->error == 0)
  912. {
  913. m_pDisasm->zeroconst = 1;
  914. }
  915. if (m_pDisasm->immconst == 0)
  916. {
  917. m_pDisasm->immconst = data;
  918. }
  919. if (m_nMode >= DISASM_FILE && m_pDisasm->error == DAE_NOERR)
  920. {
  921. if ((data & 0x00008000) != 0 && memicmp(("VxDCall"), m_pDisasm->result, 7) == 0)
  922. {
  923. memcpy(m_pDisasm->result, m_nLowercase ? ("vxdjump") : ("VxDJump"), 7);
  924. }
  925. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%lX"), data);
  926. }
  927. }
  928. //----------------------------------------------------------------------------
  929. // Decode implicit constant 1 (used in shift commands). This operand is so
  930. // insignificant that it is never shown in comment window.
  931. //----------------------------------------------------------------------------
  932. void CMgDisasmBase::DecodeC1(void)
  933. {
  934. if (m_nMode < DISASM_DATA)
  935. {
  936. return;
  937. }
  938. m_pDisasm->immconst = 1;
  939. if (m_nMode >= DISASM_FILE)
  940. {
  941. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("1") );
  942. }
  943. }
  944. //----------------------------------------------------------------------------
  945. // Decode immediate absolute data address. This operand is used in 8080-
  946. // compatible commands which allow to move data from memory to accumulator and
  947. // back. Note that bytes ModRM and SIB never appear in commands with IA operand.
  948. //----------------------------------------------------------------------------
  949. void CMgDisasmBase::DecodeIA(void)
  950. {
  951. ulong addr;
  952. if (m_ulSize < 1 + m_ulAddrSize)
  953. {
  954. m_pDisasm->error = DAE_CROSS;
  955. return;
  956. }
  957. m_nDispSize = m_ulAddrSize;
  958. if (m_nMode < DISASM_DATA)
  959. {
  960. return;
  961. }
  962. if (m_ulDataSize == 1)
  963. {
  964. m_pDisasm->memtype = DEC_BYTE;
  965. }
  966. else if (m_ulDataSize == 2)
  967. {
  968. m_pDisasm->memtype = DEC_WORD;
  969. }
  970. else if (m_ulDataSize == 4)
  971. {
  972. m_pDisasm->memtype = DEC_DWORD;
  973. }
  974. if (m_ulAddrSize == 2)
  975. {
  976. addr = *(ushort *)(m_pCmd + 1);
  977. }
  978. else
  979. {
  980. addr = *(ulong *)(m_pCmd + 1);
  981. if (m_pPFixup == NULL)
  982. {
  983. m_pPFixup = m_pCmd + 1;
  984. }
  985. m_pDisasm->fixupsize += 4;
  986. }
  987. m_pDisasm->adrconst = addr;
  988. if (addr == 0)
  989. {
  990. m_pDisasm->zeroconst = 1;
  991. }
  992. if (m_nMode >= DISASM_FILE)
  993. {
  994. Memadr(SEG_DS, (""), addr, m_ulDataSize);
  995. }
  996. }
  997. //----------------------------------------------------------------------------
  998. // Decodes jump relative to nextip of size offsize.
  999. //----------------------------------------------------------------------------
  1000. void CMgDisasmBase::DecodeRJ(ulong offsize, ulong nextip)
  1001. {
  1002. int i;
  1003. ulong addr;
  1004. char s[TEXTLEN];
  1005. //
  1006. if (m_ulSize < offsize + 1)
  1007. {
  1008. m_pDisasm->error = DAE_CROSS;
  1009. return;
  1010. }
  1011. m_nDispSize = offsize; // Interpret offset as displacement
  1012. if (m_nMode < DISASM_DATA)
  1013. {
  1014. return;
  1015. }
  1016. if (offsize == 1)
  1017. {
  1018. addr = (signed char)m_pCmd[1] + nextip;
  1019. }
  1020. else if (offsize == 2)
  1021. {
  1022. addr = *(signed short *)(m_pCmd + 1) + nextip;
  1023. }
  1024. else
  1025. {
  1026. addr = *(ulong *)(m_pCmd + 1) + nextip;
  1027. }
  1028. if (m_ulDataSize == 2)
  1029. {
  1030. addr &= 0xFFFF;
  1031. }
  1032. m_pDisasm->jmpconst = addr;
  1033. if (addr == 0)
  1034. {
  1035. m_pDisasm->zeroconst = 1;
  1036. }
  1037. if (m_nMode >= DISASM_FILE)
  1038. {
  1039. if (offsize == 1)
  1040. {
  1041. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s "), (m_nLowercase == 0 ? ("SHORT") : ("short")));
  1042. }
  1043. if (m_nMode >= DISASM_CODE)
  1044. {
  1045. i = Decodeaddress(addr, s, TEXTLEN, m_pDisasm->comment);
  1046. }
  1047. else
  1048. {
  1049. i = 0;
  1050. }
  1051. if (m_nSymbolic == 0 || i == 0)
  1052. {
  1053. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%08lX"), addr);
  1054. }
  1055. else
  1056. {
  1057. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%.*s"), TEXTLEN - m_nResult - 25, s);
  1058. }
  1059. if (m_nSymbolic == 0 && i != 0 && m_pDisasm->comment[0] == '\0')
  1060. {
  1061. strcpy(m_pDisasm->comment, s);
  1062. }
  1063. }
  1064. }
  1065. //----------------------------------------------------------------------------
  1066. // Decode immediate absolute far jump address. In flat model, such addresses
  1067. // are not used (mostly because selector is specified directly in the command),
  1068. // so I neither decode as symbol nor comment it. To allow search for selector
  1069. // by value, I interprete it as an immediate constant.
  1070. //----------------------------------------------------------------------------
  1071. void CMgDisasmBase::DecodeJF(void)
  1072. {
  1073. ulong addr, seg;
  1074. //
  1075. if (m_ulSize < 1 + m_ulAddrSize + 2)
  1076. {
  1077. m_pDisasm->error = DAE_CROSS;
  1078. return;
  1079. }
  1080. m_nDispSize = m_ulAddrSize;
  1081. m_nImmSize = 2; // Non-trivial but allowed interpretation
  1082. if (m_nMode < DISASM_DATA)
  1083. {
  1084. return;
  1085. }
  1086. if (m_ulAddrSize == 2)
  1087. {
  1088. addr = *(ushort *)(m_pCmd + 1);
  1089. seg = *(ushort *)(m_pCmd + 3);
  1090. }
  1091. else
  1092. {
  1093. addr = *(ulong *)(m_pCmd + 1);
  1094. seg = *(ushort *)(m_pCmd + 5);
  1095. }
  1096. m_pDisasm->jmpconst = addr;
  1097. m_pDisasm->immconst = seg;
  1098. if (addr == 0 || seg == 0)
  1099. {
  1100. m_pDisasm->zeroconst = 1;
  1101. }
  1102. if (m_nMode >= DISASM_FILE)
  1103. {
  1104. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s %04X:%08X"), (m_nLowercase == 0 ? ("FAR") : ("far")), seg, addr);
  1105. }
  1106. }
  1107. //----------------------------------------------------------------------------
  1108. // Decode segment register. In flat model, operands of this type are seldom.
  1109. //----------------------------------------------------------------------------
  1110. void CMgDisasmBase::DecodeSG(int index)
  1111. {
  1112. int i;
  1113. if (m_nMode < DISASM_DATA)
  1114. {
  1115. return;
  1116. }
  1117. index &= 0x07;
  1118. if (index >= 6)
  1119. {
  1120. m_nSoftError = DAE_BADSEG; // Undefined segment register
  1121. }
  1122. if (m_nMode >= DISASM_FILE)
  1123. {
  1124. i = sprintf(m_pDisasm->result + m_nResult, ("%s"), g_szSegName[index]);
  1125. if (m_nLowercase)
  1126. {
  1127. strlwr(m_pDisasm->result + m_nResult);
  1128. }
  1129. m_nResult += i;
  1130. }
  1131. }
  1132. //----------------------------------------------------------------------------
  1133. // Decode control register addressed in R part of ModRM byte. Operands of
  1134. // this type are extremely rare. Contents of control registers are accessible
  1135. // only from privilege level 0, so I cannot dump them here.
  1136. //----------------------------------------------------------------------------
  1137. void CMgDisasmBase::DecodeCR(int index)
  1138. {
  1139. m_nHasRM = 1;
  1140. if (m_nMode >= DISASM_FILE)
  1141. {
  1142. index = (index >> 3) & 0x07;
  1143. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s"), g_szCRName[index]);
  1144. if (m_nLowercase)
  1145. {
  1146. strlwr(m_pDisasm->result + m_nResult);
  1147. }
  1148. }
  1149. }
  1150. //----------------------------------------------------------------------------
  1151. // Decode debug register addressed in R part of ModRM byte. Operands of
  1152. // this type are extremely rare. I can dump only those debug registers
  1153. // available in CONTEXT structure.
  1154. //----------------------------------------------------------------------------
  1155. void CMgDisasmBase::DecodeDR(int index)
  1156. {
  1157. int i;
  1158. m_nHasRM = 1;
  1159. if (m_nMode >= DISASM_FILE)
  1160. {
  1161. index = (index >> 3) & 0x07;
  1162. i = sprintf(m_pDisasm->result + m_nResult, ("%s"), g_szDRName[index]);
  1163. if (m_nLowercase)
  1164. {
  1165. strlwr(m_pDisasm->result + m_nResult);
  1166. }
  1167. m_nResult += i;
  1168. }
  1169. }
  1170. //----------------------------------------------------------------------------
  1171. // Service function, adds valid memory adress in MASM or Ideal format to
  1172. // disassembled string. Parameters: defseg - default segment for given
  1173. // register combination, descr - fully decoded register part of address,
  1174. // offset - constant part of address, dsize - data size in bytes. If global
  1175. // flag 'symbolic' is set, function also tries to decode offset as name of
  1176. // some label.
  1177. //----------------------------------------------------------------------------
  1178. void CMgDisasmBase::Memadr(int defseg, const char *descr, long offset, int dsize)
  1179. {
  1180. int i, n, seg;
  1181. char *pr;
  1182. char s[TEXTLEN];
  1183. if( m_nMode < DISASM_FILE || descr == NULL)
  1184. {
  1185. return; // No need or possibility to decode
  1186. }
  1187. //
  1188. pr = m_pDisasm->result + m_nResult;
  1189. n = 0;
  1190. //
  1191. if (m_nSegPrefix != SEG_UNDEF)
  1192. {
  1193. seg = m_nSegPrefix;
  1194. }
  1195. else
  1196. {
  1197. seg = defseg;
  1198. }
  1199. //
  1200. if (m_nIDEAL != 0)
  1201. {
  1202. pr[n++] = '[';
  1203. }
  1204. // In some cases Disassembler may omit size of memory operand. Namely, flag
  1205. // showmemsize must be 0, type bit C_EXPL must be 0 (this bit namely means
  1206. // that explicit operand size is necessary) and type of command must not be
  1207. // C_MMX or C_NOW (because bit C_EXPL has in these cases different meaning).
  1208. // Otherwise, exact size must be supplied.
  1209. if ( m_nShowMemSize != 0 || (m_pDisasm->cmdtype & C_TYPEMASK) == C_MMX ||
  1210. (m_pDisasm->cmdtype & C_TYPEMASK) == C_NOW || (m_pDisasm->cmdtype & C_EXPL) != 0
  1211. )
  1212. {
  1213. if (dsize < sizeof(g_szSizeName) / sizeof(g_szSizeName[0]))
  1214. {
  1215. n += sprintf(pr + n, ("%s %s"), g_szSizeName[dsize], (m_nIDEAL == 0 ? ("PTR ") : ("")));
  1216. }
  1217. else
  1218. {
  1219. n += sprintf(pr + n, ("(%i-BYTE) %s"), dsize, (m_nIDEAL == 0 ? ("PTR ") : ("")));
  1220. }
  1221. }
  1222. //
  1223. if ((m_nPutDefSeg != 0 || seg != defseg) && seg != SEG_UNDEF)
  1224. {
  1225. n += sprintf(pr + n, ("%s:"), g_szSegName[seg]);
  1226. }
  1227. //
  1228. if (m_nIDEAL == 0)
  1229. {
  1230. pr[n++] = '[';
  1231. }
  1232. //
  1233. n += sprintf(pr + n, ("%s"), descr);
  1234. if (m_nLowercase)
  1235. {
  1236. strlwr(pr);
  1237. }
  1238. //
  1239. if (offset == 0L)
  1240. {
  1241. if (*descr == '\0')
  1242. {
  1243. pr[n++] = '0';
  1244. }
  1245. }
  1246. else
  1247. {
  1248. if (m_nSymbolic && m_nMode >= DISASM_CODE)
  1249. {
  1250. i = Decodeaddress(offset, s, TEXTLEN - n - 24, NULL);
  1251. }
  1252. else
  1253. {
  1254. i = 0;
  1255. }
  1256. //
  1257. if (i > 0)
  1258. {
  1259. // Offset decoded in symbolic form
  1260. if (*descr != '\0')
  1261. {
  1262. pr[n++] = '+';
  1263. }
  1264. strcpy(pr + n, s);
  1265. n += i;
  1266. }
  1267. else if (offset < 0 && offset > -16384 && *descr != '\0')
  1268. {
  1269. n += sprintf(pr + n, ("-%lX"), -offset);
  1270. }
  1271. else
  1272. {
  1273. if (*descr != '\0') pr[n++] = '+';
  1274. n += sprintf(pr + n, ("%lX"), offset);
  1275. }
  1276. }
  1277. pr[n++] = ']';
  1278. pr[n] = '\0';
  1279. m_nResult += n;
  1280. }
  1281. //----------------------------------------------------------------------------
  1282. // Decodes address into symb (nsymb bytes long, including the terminating zero
  1283. // character) and comments its possible meaning. Returns number of bytes in
  1284. // symb not including terminating zero.
  1285. //----------------------------------------------------------------------------
  1286. int CMgDisasmBase::Decodeaddress(ulong addr, char *symb, int nsymb, char *comment)
  1287. {
  1288. // Environment-specific routine! Do it yourself!
  1289. return 0;
  1290. }
  1291. //----------------------------------------------------------------------------
  1292. // Skips 3DNow! operands and extracts command suffix. Returns suffix or -1 if
  1293. // suffix lies outside the memory block. This subroutine assumes that cmd still
  1294. // points to the beginning of 3DNow! command (i.e. to the sequence of two bytes
  1295. // 0F, 0F).
  1296. //----------------------------------------------------------------------------
  1297. int CMgDisasmBase::Get3dnowsuffix(void)
  1298. {
  1299. int c, sib;
  1300. ulong offset = 3;
  1301. if( m_ulSize < 3 )
  1302. {
  1303. return -1; // Suffix outside the memory block
  1304. }
  1305. //
  1306. c = m_pCmd[2] & 0xC7; // Leave only Mod and M fields
  1307. // Register in ModM - general-purpose, MMX or 3DNow!
  1308. if ( (c & 0xC0) == 0xC0 ) {}
  1309. // 16-bit addressing mode, SIB byte is never used here.
  1310. else if( m_ulAddrSize == 2 )
  1311. {
  1312. if ( c == 0x06 ) // Special case of immediate address
  1313. {
  1314. offset += 2;
  1315. }
  1316. else if ( (c & 0xC0) == 0x40 ) // 8-bit signed displacement
  1317. {
  1318. offset ++;
  1319. }
  1320. else if ( (c & 0xC0) == 0x80 ) // 16-bit unsigned displacement
  1321. {
  1322. offset += 2;
  1323. }
  1324. }
  1325. // Immediate 32-bit address.
  1326. else if( c == 0x05 ) // Special case of immediate address
  1327. {
  1328. offset += 4 ;
  1329. }
  1330. // 32-bit address with SIB byte.
  1331. else if( (c & 0x07) == 0x04) // SIB addresation
  1332. {
  1333. if( m_ulSize < 4 )
  1334. {
  1335. return -1; // Suffix outside the memory block
  1336. }
  1337. //
  1338. sib = m_pCmd[3];
  1339. offset++;
  1340. //
  1341. if( c == 0x04 && (sib & 0x07) == 0x05 )
  1342. {
  1343. offset += 4; // Immediate address without base
  1344. }
  1345. else if( (c & 0xC0) == 0x40 ) // 8-bit displacement
  1346. {
  1347. offset += 1;
  1348. }
  1349. else if( (c & 0xC0) == 0x80 ) // 32-bit dislacement
  1350. {
  1351. offset += 4;
  1352. }
  1353. }
  1354. // 32-bit address without SIB byte
  1355. else if( (c & 0xC0) == 0x40 )
  1356. {
  1357. offset += 1;
  1358. }
  1359. else if( (c & 0xC0) == 0x80 )
  1360. {
  1361. offset += 4;
  1362. }
  1363. //
  1364. if( offset >= m_ulSize )
  1365. {
  1366. return -1; // Suffix outside the memory block
  1367. }
  1368. return m_pCmd[offset];
  1369. }
  1370. //--------------------------------------------------------------------------------
  1371. // Function attempts to calculate address of assembler instruction which is n
  1372. // lines back in the listing. Maximal stepback is limited to 127. In general,
  1373. // this is rather non-trivial task. Proposed solution may cause problems which
  1374. // however are not critical here.
  1375. //--------------------------------------------------------------------------------
  1376. ulong CMgDisasmBase::Disassembleback(char *block, ulong base, ulong size, ulong ip, int n)
  1377. {
  1378. int i;
  1379. ulong abuf[131], addr, back, cmdsize;
  1380. char *pdata;
  1381. t_disasm da;
  1382. //
  1383. if (block == NULL)
  1384. {
  1385. return 0; // Error, no code!
  1386. }
  1387. if (n < 0)
  1388. {
  1389. n = 0; // Try to correct obvious errors
  1390. }
  1391. else if (n > 127)
  1392. {
  1393. n = 127;
  1394. }
  1395. if (ip > base + size)
  1396. {
  1397. ip = base + size;
  1398. }
  1399. if (n == 0)
  1400. {
  1401. return ip; // Obvious answers
  1402. }
  1403. if (ip <= base + n)
  1404. {
  1405. return base;
  1406. }
  1407. back = MAXCMDSIZE * (n + 3); // Command length limited to MAXCMDSIZE
  1408. if (ip < base + back)
  1409. {
  1410. back = ip - base;
  1411. }
  1412. addr = ip - back;
  1413. pdata = block + (addr - base);
  1414. for (i = 0; addr < ip; i++)
  1415. {
  1416. abuf[i % 128] = addr;
  1417. cmdsize = Disasm(pdata, back, addr, &da, DISASM_SIZE);
  1418. pdata += cmdsize;
  1419. addr += cmdsize;
  1420. back -= cmdsize;
  1421. }
  1422. if (i < n)
  1423. {
  1424. return abuf[0];
  1425. }
  1426. else
  1427. {
  1428. return abuf[(i - n + 128) % 128];
  1429. }
  1430. }
  1431. //--------------------------------------------------------------------------------
  1432. // Function attempts to calculate address of assembler instruction which is n
  1433. // lines forward in the listing.
  1434. //--------------------------------------------------------------------------------
  1435. ulong CMgDisasmBase::Disassembleforward(char *block, ulong base, ulong size, ulong ip, int n)
  1436. {
  1437. int i;
  1438. ulong cmdsize;
  1439. char *pdata;
  1440. t_disasm da;
  1441. if (block == NULL)
  1442. {
  1443. return 0; // Error, no code!
  1444. }
  1445. if (ip < base)
  1446. {
  1447. ip = base; // Try to correct obvious errors
  1448. }
  1449. if (ip > base + size)
  1450. {
  1451. ip = base + size;
  1452. }
  1453. if (n <= 0)
  1454. {
  1455. return ip;
  1456. }
  1457. //
  1458. pdata = block + (ip - base);
  1459. size -= (ip - base);
  1460. //
  1461. for (i = 0; i < n && size > 0; i++)
  1462. {
  1463. cmdsize = Disasm(pdata, size, ip, &da, DISASM_SIZE);
  1464. pdata += cmdsize;
  1465. ip += cmdsize;
  1466. size -= cmdsize;
  1467. }
  1468. return ip;
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. //
  1472. //
  1473. //----------------------------------------------------------------------------
  1474. ulong CMgDisasmBase::Disasm( char *src, ulong srcsize, ulong srcip, t_disasm *disasm, int disasmmode )
  1475. {
  1476. //
  1477. int nIsPrefix = 0;
  1478. int nRepeated = 0;
  1479. int nLockPrefix = 0; // Non-zero if lock prefix present
  1480. int nRepPrefix = 0; // REPxxx prefix or 0
  1481. //
  1482. ulong u = 0 , code = 0 ;
  1483. int i = 0, j = 0 , is3dnow = 0 ;
  1484. int cxsize = 0;
  1485. int mnemosize = 0;
  1486. int operand = 0;
  1487. int arg = 0;
  1488. //
  1489. t_cmddata *pd, *pdan;
  1490. //
  1491. char name[TEXTLEN], *pname;
  1492. // 32-bit code and data segments only!
  1493. m_ulDataSize = 4;
  1494. m_ulAddrSize = 4;
  1495. // Prepare disassembler variables and initialize structure disasm.
  1496. m_nSegPrefix = SEG_UNDEF;
  1497. m_nHasRM = 0;
  1498. m_nHasSIB = 0;
  1499. m_nDispSize = 0;
  1500. m_nImmSize = 0;
  1501. m_nDump = 0;
  1502. m_nResult = 0;
  1503. m_nSoftError = 0;
  1504. //
  1505. m_pCmd = src;
  1506. m_ulSize = srcsize;
  1507. m_pPFixup = NULL;
  1508. //结构初始化:
  1509. m_pDisasm = disasm;
  1510. m_pDisasm->ip = srcip;
  1511. m_pDisasm->comment[0] = '\0';
  1512. m_pDisasm->cmdtype = C_BAD;
  1513. m_pDisasm->nprefix = 0;
  1514. m_pDisasm->memtype = DEC_UNKNOWN;
  1515. m_pDisasm->indexed = 0;
  1516. m_pDisasm->jmpconst = 0;
  1517. m_pDisasm->jmptable = 0;
  1518. m_pDisasm->adrconst = 0;
  1519. m_pDisasm->immconst = 0;
  1520. m_pDisasm->zeroconst = 0;
  1521. m_pDisasm->fixupoffset = 0;
  1522. m_pDisasm->fixupsize = 0;
  1523. m_pDisasm->warnings = 0;
  1524. m_pDisasm->error = DAE_NOERR;
  1525. // No need to use register contents
  1526. m_nMode = disasmmode;
  1527. // Correct 80x86 command may theoretically contain up to 4 prefixes belonging
  1528. // to different prefix groups. This limits maximal possible size of the
  1529. // command to MAXCMDSIZE=16 bytes. In order to maintain this limit, if
  1530. // Disasm() detects second prefix from the same group, it flushes first
  1531. // prefix in the sequence as a pseudocommand.
  1532. //
  1533. while( m_ulSize > 0 )
  1534. {
  1535. nIsPrefix = 1;
  1536. switch( *m_pCmd )
  1537. {
  1538. case 0x26:
  1539. {
  1540. if(m_nSegPrefix == SEG_UNDEF)
  1541. {
  1542. m_nSegPrefix = SEG_ES;
  1543. }
  1544. else
  1545. {
  1546. nRepeated = 1;
  1547. }
  1548. break;
  1549. }
  1550. case 0x2E:
  1551. {
  1552. if(m_nSegPrefix == SEG_UNDEF)
  1553. {
  1554. m_nSegPrefix = SEG_CS;
  1555. }
  1556. else
  1557. {
  1558. nRepeated = 1;
  1559. }
  1560. break;
  1561. }
  1562. case 0x36:
  1563. {
  1564. if(m_nSegPrefix == SEG_UNDEF)
  1565. {
  1566. m_nSegPrefix = SEG_SS;
  1567. }
  1568. else
  1569. {
  1570. nRepeated = 1;
  1571. }
  1572. break;
  1573. }
  1574. case 0x3E:
  1575. {
  1576. if(m_nSegPrefix == SEG_UNDEF)
  1577. {
  1578. m_nSegPrefix = SEG_DS;
  1579. }
  1580. else
  1581. {
  1582. nRepeated = 1;
  1583. }
  1584. break;
  1585. }
  1586. case 0x64:
  1587. {
  1588. if(m_nSegPrefix == SEG_UNDEF)
  1589. {
  1590. m_nSegPrefix = SEG_FS;
  1591. }
  1592. else
  1593. {
  1594. nRepeated = 1;
  1595. }
  1596. break;
  1597. }
  1598. case 0x65:
  1599. {
  1600. if(m_nSegPrefix == SEG_UNDEF)
  1601. {
  1602. m_nSegPrefix = SEG_GS;
  1603. }
  1604. else
  1605. {
  1606. nRepeated = 1;
  1607. }
  1608. break;
  1609. }
  1610. case 0x66:
  1611. {
  1612. if(m_ulDataSize == 4)
  1613. {
  1614. m_ulDataSize = 2;
  1615. }
  1616. else
  1617. {
  1618. nRepeated = 1;
  1619. }
  1620. break;
  1621. }
  1622. case 0x67:
  1623. {
  1624. if(m_ulAddrSize == 4)
  1625. {
  1626. m_ulAddrSize = 2;
  1627. }
  1628. else
  1629. {
  1630. nRepeated = 1;
  1631. }
  1632. break;
  1633. }
  1634. case 0xF0:
  1635. {
  1636. if (nLockPrefix == 0)
  1637. {
  1638. nLockPrefix = 0xF0;
  1639. }
  1640. else
  1641. {
  1642. nRepeated = 1;
  1643. }
  1644. break;
  1645. }
  1646. case 0xF2:
  1647. {
  1648. if (nRepPrefix == 0)
  1649. {
  1650. nRepPrefix = 0xF2;
  1651. }
  1652. else
  1653. {
  1654. nRepeated = 1;
  1655. }
  1656. break;
  1657. }
  1658. case 0xF3:
  1659. {
  1660. if (nRepPrefix == 0)
  1661. {
  1662. nRepPrefix = 0xF3;
  1663. }
  1664. else
  1665. {
  1666. nRepeated = 1;
  1667. }
  1668. break;
  1669. }
  1670. default:
  1671. {
  1672. nIsPrefix = 0;
  1673. break;
  1674. }
  1675. }
  1676. //
  1677. if( nRepPrefix == 0 || nRepeated != 0 )
  1678. {
  1679. break;
  1680. }
  1681. //
  1682. if( m_nMode >= DISASM_FILE )
  1683. {
  1684. m_nDump += sprintf( m_pDisasm->dump + m_nDump , ("%02X:"), *m_pCmd );
  1685. }
  1686. //
  1687. m_pDisasm->nprefix ++;
  1688. m_pCmd ++;
  1689. srcip ++;
  1690. m_ulSize --;
  1691. u ++;
  1692. }
  1693. //
  1694. if( nRepeated ) //nRepeated == 1
  1695. {
  1696. //
  1697. if( m_nMode >= DISASM_FILE )
  1698. {
  1699. m_pDisasm->dump[3] = '\0'; //end 00 // Leave only first dumped prefix
  1700. m_pDisasm->nprefix = 1;
  1701. switch( m_pCmd[-(long)u])
  1702. {
  1703. case 0x26:
  1704. {
  1705. pname = (char *)(g_szSegName[SEG_ES]);
  1706. break;
  1707. }
  1708. case 0x2E:
  1709. {
  1710. pname = (char *)(g_szSegName[SEG_CS]);
  1711. break;
  1712. }
  1713. case 0x36:
  1714. {
  1715. pname = (char *)(g_szSegName[SEG_SS]);
  1716. break;
  1717. }
  1718. case 0x3E:
  1719. {
  1720. pname = (char *)(g_szSegName[SEG_DS]);
  1721. break;
  1722. }
  1723. case 0x64:
  1724. {
  1725. pname = (char *)(g_szSegName[SEG_FS]);
  1726. break;
  1727. }
  1728. case 0x65:
  1729. {
  1730. pname = (char *)(g_szSegName[SEG_GS]);
  1731. break;
  1732. }
  1733. case 0x66:
  1734. {
  1735. pname = ("DATASIZE");
  1736. break;
  1737. }
  1738. case 0x67:
  1739. {
  1740. pname = ("ADDRSIZE");
  1741. break;
  1742. }
  1743. case 0xF0:
  1744. {
  1745. pname = ("LOCK");
  1746. break;
  1747. }
  1748. case 0xF2:
  1749. {
  1750. pname = ("REPNE");
  1751. break;
  1752. }
  1753. case 0xF3:
  1754. {
  1755. pname = ("REPE");
  1756. break;
  1757. }
  1758. default:
  1759. {
  1760. pname = "?";
  1761. break;
  1762. }
  1763. }
  1764. m_nResult += sprintf( m_pDisasm->result + m_nResult, ("PREFIX %s:"), pname );
  1765. if( m_nLowercase )
  1766. {
  1767. strlwr( m_pDisasm->result );
  1768. }
  1769. if( m_nExtraPrefix == 0 ) strcpy( m_pDisasm->comment, ("Superfluous prefix") );
  1770. }
  1771. m_pDisasm->warnings |= DAW_PREFIX;
  1772. if (nLockPrefix)
  1773. {
  1774. m_pDisasm->warnings |= DAW_LOCK;
  1775. }
  1776. m_pDisasm->cmdtype = C_RARE;
  1777. return 1;
  1778. }
  1779. // If lock prefix available, display it and forget, because it has no
  1780. // influence on decoding of rest of the command.
  1781. if (nLockPrefix != 0)
  1782. {
  1783. if(m_nMode >= DISASM_FILE)
  1784. {
  1785. m_nResult += sprintf( m_pDisasm->result + m_nResult, ("LOCK ") );
  1786. }
  1787. m_pDisasm->warnings |= DAW_LOCK;
  1788. }
  1789. // Fetch (if available) first 3 bytes of the command, add repeat prefix and
  1790. // find command in the command table.
  1791. code = 0;
  1792. if (m_ulSize > 0)
  1793. {
  1794. *( ((char *)&code) + 0 ) = m_pCmd[0];
  1795. }
  1796. if (m_ulSize > 1)
  1797. {
  1798. *( ((char *)&code) + 1 ) = m_pCmd[1];
  1799. }
  1800. if (m_ulSize > 2)
  1801. {
  1802. *( ((char *)&code) + 2 ) = m_pCmd[2];
  1803. }
  1804. if (nRepPrefix != 0) // RER/REPE/REPNE is considered to be
  1805. {
  1806. code = (code << 8) | nRepPrefix; // part of command.
  1807. }
  1808. if (m_nDecodeVxd && (code & 0xFFFF) == 0x20CD)
  1809. {
  1810. pd = &g_VxdCmd; // Decode VxD call (Win95/98)
  1811. }
  1812. else
  1813. {
  1814. for( pd = g_CmdData; pd->mask != 0; pd++ )
  1815. {
  1816. if (((code ^ pd->code) & pd->mask) != 0)
  1817. {
  1818. continue;
  1819. }
  1820. if ( m_nMode >= DISASM_FILE && m_nShortStringCmds &&
  1821. (pd->arg1 == MSO || pd->arg1 == MDE || pd->arg2 == MSO || pd->arg2 == MDE) )
  1822. {
  1823. continue; // Search short form of string command
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. //
  1829. if ((pd->type & C_TYPEMASK) == C_NOW)
  1830. {
  1831. // 3DNow! commands require additional search.
  1832. is3dnow = 1;
  1833. j = Get3dnowsuffix();
  1834. if (j < 0)
  1835. {
  1836. m_pDisasm->error = DAE_CROSS;
  1837. }
  1838. else
  1839. {
  1840. for ( ; pd->mask != 0; pd++ )
  1841. {
  1842. if (((code ^ pd->code) & pd->mask) != 0)
  1843. {
  1844. continue;
  1845. }
  1846. if (((uchar *) & (pd->code))[2] == j)
  1847. {
  1848. break;
  1849. }
  1850. }
  1851. }
  1852. }
  1853. // Command not found
  1854. if ( pd->mask == 0)
  1855. {
  1856. m_pDisasm->cmdtype = C_BAD;
  1857. if( m_ulSize < 2 )
  1858. {
  1859. m_pDisasm->error = DAE_CROSS;
  1860. }
  1861. else
  1862. {
  1863. m_pDisasm->error = DAE_BADCMD;
  1864. }
  1865. }
  1866. else
  1867. {
  1868. m_pDisasm->cmdtype = pd->type;
  1869. cxsize = m_ulDataSize;
  1870. //
  1871. if( m_nSegPrefix == SEG_FS || m_nSegPrefix == SEG_GS || nLockPrefix != 0 )
  1872. {
  1873. m_pDisasm->cmdtype |= C_RARE; // These prefixes are rare
  1874. }
  1875. //
  1876. if(pd->bits == PR)
  1877. {
  1878. m_pDisasm->warnings |= DAW_PRIV; // Privileged command (ring 0)
  1879. }
  1880. else if(pd->bits == WP)
  1881. {
  1882. m_pDisasm->warnings |= DAW_IO; // I/O command
  1883. }
  1884. // Win32 programs usually try to keep stack dword-aligned, so INC ESP
  1885. // (44) and DEC ESP (4C) usually don't appear in real code. Also check for
  1886. // ADD ESP,imm and SUB ESP,imm (81,C4,imm32; 83,C4,imm8; 81,EC,imm32;
  1887. // 83,EC,imm8).
  1888. if(m_pCmd[0] == 0x44 || m_pCmd[0] == 0x4C ||
  1889. (m_ulSize >= 3 && (m_pCmd[0] == 0x81 || m_pCmd[0] == 0x83) &&
  1890. (m_pCmd[1] == 0xC4 || m_pCmd[1] == 0xEC) && (m_pCmd[2] & 0x03) != 0)
  1891. )
  1892. {
  1893. m_pDisasm->warnings |= DAW_STACK;
  1894. m_pDisasm->cmdtype |= C_RARE;
  1895. };
  1896. // Warn also on MOV SEG,... (8E...). Win32 works in flat mode.
  1897. if(m_pCmd[0] == 0x8E)
  1898. {
  1899. m_pDisasm->warnings |= DAW_SEGMENT;
  1900. }
  1901. // If opcode is 2-byte, adjust command.
  1902. if(pd->len == 2)
  1903. {
  1904. if(m_ulSize == 0)
  1905. {
  1906. m_pDisasm->error = DAE_CROSS;
  1907. }
  1908. else
  1909. {
  1910. if(m_nMode >= DISASM_FILE)
  1911. {
  1912. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd);
  1913. }
  1914. m_pCmd[0]++;
  1915. srcip++;
  1916. m_ulSize--;
  1917. }
  1918. }
  1919. if(m_ulSize == 0)
  1920. {
  1921. m_pDisasm->error = DAE_CROSS;
  1922. }
  1923. // Some commands either feature non-standard data size or have bit which
  1924. // allowes to select data size.
  1925. if ((pd->bits & WW) != 0 && (*m_pCmd & WW) == 0)
  1926. {
  1927. m_ulDataSize = 1; // Bit W in command set to 0
  1928. }
  1929. else if ((pd->bits & W3) != 0 && (*m_pCmd & W3) == 0)
  1930. {
  1931. m_ulDataSize = 1; // Another position of bit W
  1932. }
  1933. else if ((pd->bits & FF) != 0)
  1934. {
  1935. m_ulDataSize = 2;
  1936. }
  1937. //
  1938. // Some commands either have mnemonics which depend on data size (8/16 bits
  1939. // or 32 bits, like CWD/CDQ), or have several different mnemonics (like
  1940. // JNZ/JNE). First case is marked by either '&' (mnemonic depends on
  1941. // operand size) or '$' (depends on address size). In the second case,
  1942. // there is no special marker and disassembler selects main mnemonic.
  1943. if (m_nMode >= DISASM_FILE)
  1944. {
  1945. //
  1946. if (pd->name[0] == '&')
  1947. {
  1948. mnemosize = m_ulDataSize;
  1949. }
  1950. else if (pd->name[0] == '$')
  1951. {
  1952. mnemosize = m_ulDataSize;
  1953. }
  1954. else
  1955. {
  1956. mnemosize = 0;
  1957. }
  1958. //
  1959. if(mnemosize != 0)
  1960. {
  1961. for(i = 0, j = 1; pd->name[j] != '\0'; j++)
  1962. {
  1963. if (pd->name[j] == ':') // Separator between 16/32 mnemonics
  1964. {
  1965. if(mnemosize == 4)
  1966. {
  1967. i = 0;
  1968. }
  1969. else
  1970. {
  1971. break;
  1972. }
  1973. }
  1974. else if (pd->name[j] == '*') // Substitute by 'W', 'D' or none
  1975. {
  1976. if (mnemosize == 4 && m_nSizeSens != 2)
  1977. {
  1978. name[i++] = 'D';
  1979. }
  1980. else if (mnemosize != 4 && m_nSizeSens != 0)
  1981. {
  1982. name[i++] = 'W';
  1983. }
  1984. }
  1985. else
  1986. {
  1987. name[i++] = pd->name[j];
  1988. }
  1989. }
  1990. name[i] = '\0';
  1991. }
  1992. else
  1993. {
  1994. strcpy(name, pd->name);
  1995. for (i = 0; name[i] != '\0'; i++)
  1996. {
  1997. // Use main mnemonic
  1998. if (name[i] == ',')
  1999. {
  2000. name[i] = '\0';
  2001. break;
  2002. }
  2003. }
  2004. }
  2005. //
  2006. if( nRepPrefix != 0 && m_nTabArguments )
  2007. {
  2008. for (i = 0; name[i] != '\0' && name[i] != ' '; i++)
  2009. {
  2010. m_pDisasm->result[m_nResult++] = name[i];
  2011. }
  2012. if (name[i] == ' ')
  2013. {
  2014. m_pDisasm->result[m_nResult++] = ' ';
  2015. i++;
  2016. }
  2017. while(m_nResult < 8)
  2018. {
  2019. m_pDisasm->result[m_nResult++] = ' ';
  2020. }
  2021. for ( ; name[i] != '\0'; i++)
  2022. {
  2023. m_pDisasm->result[m_nResult++] = name[i];
  2024. }
  2025. }
  2026. else
  2027. {
  2028. m_nResult += sprintf(m_pDisasm->result + m_nResult, ("%s"), name);
  2029. }
  2030. if(m_nLowercase)
  2031. {
  2032. strlwr(m_pDisasm->result);
  2033. }
  2034. }
  2035. //
  2036. // Decode operands (explicit - encoded in command, implicit - present in
  2037. // mmemonic or assumed - used or modified by command). Assumed operands
  2038. // must stay after all explicit and implicit operands. Up to 3 operands
  2039. // are allowed.
  2040. for(operand = 0; operand < 3; operand++)
  2041. {
  2042. if (m_pDisasm->error)
  2043. {
  2044. break; // Error - no sense to continue
  2045. }
  2046. // If command contains both source and destination, one usually must not
  2047. // decode destination to comment because it will be overwritten on the
  2048. // next step. Global addcomment takes care of this. Decoding routines,
  2049. // however, may ignore this flag.
  2050. if (operand == 0 && pd->arg2 != NNN && pd->arg2 < PSEUDOOP)
  2051. {
  2052. m_nAddComment = 0;
  2053. }
  2054. else
  2055. {
  2056. m_nAddComment = 1;
  2057. }
  2058. // Get type of next argument.
  2059. if (operand == 0)
  2060. {
  2061. arg = pd->arg1;
  2062. }
  2063. else if(operand == 1)
  2064. {
  2065. arg = pd->arg2;
  2066. }
  2067. else
  2068. {
  2069. arg = pd->arg3;
  2070. }
  2071. //
  2072. if (arg == NNN)
  2073. {
  2074. break; // No more operands
  2075. }
  2076. // Arguments with arg>=PSEUDOOP are assumed operands and are not
  2077. // displayed in disassembled result, so they require no delimiter.
  2078. if ((m_nMode >= DISASM_FILE) && arg < PSEUDOOP)
  2079. {
  2080. if(operand == 0)
  2081. {
  2082. m_pDisasm->result[m_nResult++] = ' ';
  2083. if (m_nTabArguments)
  2084. {
  2085. while(m_nResult < 8)
  2086. {
  2087. m_pDisasm->result[m_nResult++] = ' ';
  2088. }
  2089. }
  2090. }
  2091. else
  2092. {
  2093. m_pDisasm->result[m_nResult++] = ',';
  2094. if (m_nExtraSpace)
  2095. {
  2096. m_pDisasm->result[m_nResult++] = ' ';
  2097. }
  2098. }
  2099. }
  2100. //
  2101. // Decode, analyse and comment next operand of the command.
  2102. switch(arg)
  2103. {
  2104. case REG: // Integer register in Reg field
  2105. {
  2106. if( m_ulSize < 2 )
  2107. {
  2108. m_pDisasm->error = DAE_CROSS;
  2109. }
  2110. else
  2111. {
  2112. DecodeRG(m_pCmd[1] >> 3, m_ulDataSize, REG);
  2113. }
  2114. m_nHasRM = 1;
  2115. break;
  2116. }
  2117. case RCM: // Integer register in command byte
  2118. {
  2119. DecodeRG(m_pCmd[0], m_ulDataSize, RCM);
  2120. break;
  2121. }
  2122. case RG4: // Integer 4-byte register in Reg field
  2123. {
  2124. if(m_ulSize < 2)
  2125. {
  2126. m_pDisasm->error = DAE_CROSS;
  2127. }
  2128. else
  2129. {
  2130. DecodeRG(m_pCmd[1] >> 3, 4, RG4);
  2131. }
  2132. m_nHasRM = 1;
  2133. break;
  2134. }
  2135. case RAC: // Accumulator (AL/AX/EAX, implicit)
  2136. {
  2137. DecodeRG(REG_EAX, m_ulDataSize, RAC);
  2138. break;
  2139. }
  2140. case RAX: // AX (2-byte, implicit)
  2141. {
  2142. DecodeRG(REG_EAX, 2, RAX);
  2143. break;
  2144. }
  2145. case RDX: // DX (16-bit implicit port address)
  2146. {
  2147. DecodeRG(REG_EDX, 2, RDX);
  2148. break;
  2149. }
  2150. case RCL: // Implicit CL register (for shifts)
  2151. {
  2152. DecodeRG(REG_ECX, 1, RCL);
  2153. break;
  2154. }
  2155. case RS0: // Top of FPU stack (ST(0))
  2156. {
  2157. DecodeST(0, 0);
  2158. break;
  2159. }
  2160. case RST: // FPU register (ST(i)) in command byte
  2161. {
  2162. DecodeST(m_pCmd[0], 0);
  2163. break;
  2164. }
  2165. case RMX: // MMX register MMx
  2166. {
  2167. if (m_ulSize < 2)
  2168. {
  2169. m_pDisasm->error = DAE_CROSS;
  2170. }
  2171. else
  2172. {
  2173. DecodeMX(m_pCmd[1] >> 3);
  2174. }
  2175. m_nHasRM = 1;
  2176. break;
  2177. }
  2178. case R3D: // 3DNow! register MMx
  2179. {
  2180. if(m_ulSize < 2)
  2181. {
  2182. m_pDisasm->error = DAE_CROSS;
  2183. }
  2184. else
  2185. {
  2186. DecodeNR(m_pCmd[1] >> 3);
  2187. }
  2188. m_nHasRM = 1;
  2189. break;
  2190. }
  2191. case MRG: // Memory/register in ModRM byte
  2192. case MRJ: // Memory/reg in ModRM as JUMP target
  2193. case MR1: // 1-byte memory/register in ModRM byte
  2194. case MR2: // 2-byte memory/register in ModRM byte
  2195. case MR4: // 4-byte memory/register in ModRM byte
  2196. case MR8: // 8-byte memory/MMX register in ModRM
  2197. case MRD: // 8-byte memory/3DNow! register in ModRM
  2198. case MMA: // Memory address in ModRM byte for LEA
  2199. case MML: // Memory in ModRM byte (for LES)
  2200. case MM6: // Memory in ModRm (6-byte descriptor)
  2201. case MMB: // Two adjacent memory locations (BOUND)
  2202. case MD2: // Memory in ModRM byte (16-bit integer)
  2203. case MB2: // Memory in ModRM byte (16-bit binary)
  2204. case MD4: // Memory in ModRM byte (32-bit integer)
  2205. case MD8: // Memory in ModRM byte (64-bit integer)
  2206. case MDA: // Memory in ModRM byte (80-bit BCD)
  2207. case MF4: // Memory in ModRM byte (32-bit float)
  2208. case MF8: // Memory in ModRM byte (64-bit float)
  2209. case MFA: // Memory in ModRM byte (80-bit float)
  2210. case MFE: // Memory in ModRM byte (FPU environment)
  2211. case MFS: // Memory in ModRM byte (FPU state)
  2212. case MFX: // Memory in ModRM byte (ext. FPU state)
  2213. {
  2214. DecodeMR(arg);
  2215. break;
  2216. }
  2217. case MMS: // Memory in ModRM byte (as SEG:OFFS)
  2218. {
  2219. DecodeMR(arg);
  2220. m_pDisasm->warnings |= DAW_FARADDR;
  2221. break;
  2222. }
  2223. case RR4: // 4-byte memory/register (register only)
  2224. case RR8: // 8-byte MMX register only in ModRM
  2225. case RRD: // 8-byte memory/3DNow! (register only)
  2226. {
  2227. if ((m_pCmd[1] & 0xC0) != 0xC0)
  2228. {
  2229. m_nSoftError = DAE_REGISTER;
  2230. }
  2231. DecodeMR(arg);
  2232. break;
  2233. }
  2234. case MSO: // Source in string op's ([ESI])
  2235. {
  2236. DecodeSO();
  2237. break;
  2238. }
  2239. case MDE: // Destination in string op's ([EDI])
  2240. {
  2241. DecodeDE();
  2242. break;
  2243. }
  2244. case MXL: // XLAT operand ([EBX+AL])
  2245. {
  2246. DecodeXL();
  2247. break;
  2248. }
  2249. case IMM: // Immediate data (8 or 16/32)
  2250. case IMU: // Immediate unsigned data (8 or 16/32)
  2251. {
  2252. if ((pd->bits & SS) != 0 && (*m_pCmd & 0x02) != 0)
  2253. {
  2254. DecodeIM(1, m_ulDataSize, arg);
  2255. }
  2256. else
  2257. {
  2258. DecodeIM(m_ulDataSize, 0, arg);
  2259. }
  2260. break;
  2261. }
  2262. case VXD: // VxD service (32-bit only)
  2263. {
  2264. DecodeVX();
  2265. break;
  2266. }
  2267. case IMX: // Immediate sign-extendable byte
  2268. {
  2269. DecodeIM(1, m_ulDataSize, arg);
  2270. break;
  2271. }
  2272. case C01: // Implicit constant 1 (for shifts)
  2273. {
  2274. DecodeC1();
  2275. break;
  2276. }
  2277. case IMS: // Immediate byte (for shifts)
  2278. case IM1: // Immediate byte
  2279. {
  2280. DecodeIM(1, 0, arg);
  2281. break;
  2282. }
  2283. case IM2: // Immediate word (ENTER/RET)
  2284. {
  2285. DecodeIM(2, 0, arg);
  2286. if ((m_pDisasm->immconst & 0x03) != 0)
  2287. {
  2288. m_pDisasm->warnings |= DAW_STACK;
  2289. }
  2290. break;
  2291. }
  2292. case IMA: // Immediate absolute near data address
  2293. {
  2294. DecodeIA();
  2295. break;
  2296. }
  2297. case JOB: // Immediate byte offset (for jumps)
  2298. {
  2299. DecodeRJ(1, srcip + 2);
  2300. break;
  2301. }
  2302. case JOW: // Immediate full offset (for jumps)
  2303. {
  2304. DecodeRJ(m_ulDataSize, srcip + m_ulDataSize + 1);
  2305. break;
  2306. }
  2307. case JMF: // Immediate absolute far jump/call addr
  2308. {
  2309. DecodeJF();
  2310. m_pDisasm->warnings |= DAW_FARADDR;
  2311. break;
  2312. }
  2313. case SGM: // Segment register in ModRM byte
  2314. {
  2315. if(m_ulSize < 2)
  2316. {
  2317. m_pDisasm->error = DAE_CROSS;
  2318. }
  2319. DecodeSG(m_pCmd[1] >> 3);
  2320. m_nHasRM = 1;
  2321. break;
  2322. }
  2323. case SCM: // Segment register in command byte
  2324. {
  2325. DecodeSG(m_pCmd[0] >> 3);
  2326. if((m_pDisasm->cmdtype & C_TYPEMASK) == C_POP)
  2327. {
  2328. m_pDisasm->warnings |= DAW_SEGMENT;
  2329. }
  2330. break;
  2331. }
  2332. case CRX: // Control register CRx
  2333. {
  2334. if ((m_pCmd[1] & 0xC0) != 0xC0)
  2335. {
  2336. m_pDisasm->error = DAE_REGISTER;
  2337. }
  2338. DecodeCR(m_pCmd[1]);
  2339. break;
  2340. }
  2341. case DRX: // Debug register DRx
  2342. {
  2343. if ((m_pCmd[1] & 0xC0) != 0xC0)
  2344. {
  2345. m_pDisasm->error = DAE_REGISTER;
  2346. }
  2347. DecodeDR(m_pCmd[1]);
  2348. break;
  2349. }
  2350. case PRN: // Near return address (pseudooperand)
  2351. {
  2352. break;
  2353. }
  2354. case PRF: // Far return address (pseudooperand)
  2355. {
  2356. m_pDisasm->warnings |= DAW_FARADDR;
  2357. break;
  2358. }
  2359. case PAC: // Accumulator (AL/AX/EAX, pseudooperand)
  2360. {
  2361. DecodeRG(REG_EAX, m_ulDataSize, PAC);
  2362. break;
  2363. }
  2364. case PAH: // AH (in LAHF/SAHF, pseudooperand)
  2365. case PFL: // Lower byte of flags (pseudooperand)
  2366. {
  2367. break;
  2368. }
  2369. case PS0: // Top of FPU stack (pseudooperand)
  2370. {
  2371. DecodeST(0, 1);
  2372. break;
  2373. }
  2374. case PS1: // ST(1) (pseudooperand)
  2375. {
  2376. DecodeST(1, 1);
  2377. break;
  2378. }
  2379. case PCX: // CX/ECX (pseudooperand)
  2380. {
  2381. DecodeRG(REG_ECX, cxsize, PCX);
  2382. break;
  2383. }
  2384. case PDI: // EDI (pseudooperand in MMX extentions)
  2385. {
  2386. DecodeRG(REG_EDI, 4, PDI);
  2387. break;
  2388. }
  2389. default:
  2390. {
  2391. m_pDisasm->error = DAE_INTERN; // Unknown argument type
  2392. break;
  2393. }
  2394. }
  2395. }
  2396. // Check whether command may possibly contain fixups.
  2397. if( m_pPFixup != NULL && m_pDisasm->fixupsize > 0)
  2398. {
  2399. m_pDisasm->fixupoffset = m_pPFixup - src;
  2400. }
  2401. // Segment prefix and address size prefix are superfluous for command which
  2402. // does not access memory. If this the case, mark command as rare to help
  2403. // in analysis.
  2404. if ( m_pDisasm->memtype == DEC_UNKNOWN && ( m_nSegPrefix != SEG_UNDEF || (m_ulAddrSize != 4 && pd->name[0] != '$') ) )
  2405. {
  2406. m_pDisasm->warnings |= DAW_PREFIX;
  2407. m_pDisasm->cmdtype |= C_RARE;
  2408. }
  2409. // 16-bit addressing is rare in 32-bit programs. If this is the case,
  2410. // mark command as rare to help in analysis.
  2411. if(m_ulAddrSize != 4)
  2412. {
  2413. m_pDisasm->cmdtype |= C_RARE;
  2414. }
  2415. }
  2416. //
  2417. // Suffix of 3DNow! command is accounted best by assuming it immediate byte
  2418. // constant.
  2419. if (is3dnow)
  2420. {
  2421. if (m_nImmSize != 0)
  2422. {
  2423. m_pDisasm->error = DAE_BADCMD;
  2424. }
  2425. else m_nImmSize = 1;
  2426. }
  2427. // Right or wrong, command decoded. Now dump it.
  2428. if (m_pDisasm->error != 0)
  2429. {
  2430. // Hard error in command detected
  2431. if (m_nMode >= DISASM_FILE)
  2432. {
  2433. m_nResult = sprintf(m_pDisasm->result, ("???"));
  2434. }
  2435. if (m_pDisasm->error == DAE_BADCMD && (*m_pCmd == 0x0F || *m_pCmd == 0xFF) && m_ulSize > 0)
  2436. {
  2437. if (m_nMode >= DISASM_FILE)
  2438. {
  2439. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd);
  2440. }
  2441. m_pCmd++;
  2442. m_ulSize--;
  2443. }
  2444. if (m_ulSize > 0)
  2445. {
  2446. if (m_nMode >= DISASM_FILE)
  2447. {
  2448. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd);
  2449. }
  2450. m_pCmd++;
  2451. m_ulSize--;
  2452. }
  2453. }
  2454. else // No hard error, dump command
  2455. {
  2456. if (m_nMode >= DISASM_FILE)
  2457. {
  2458. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd++);
  2459. if (m_nHasRM)
  2460. {
  2461. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd++);
  2462. }
  2463. if (m_nHasSIB)
  2464. {
  2465. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd++);
  2466. }
  2467. if (m_nDispSize != 0)
  2468. {
  2469. m_pDisasm->dump[m_nDump++] = ' ';
  2470. for (i = 0; i < m_nDispSize; i++)
  2471. {
  2472. m_nDump += sprintf(m_pDisasm->dump + m_nDump, ("%02X"), *m_pCmd++);
  2473. }
  2474. }
  2475. if (m_nImmSize != 0)
  2476. {
  2477. m_pDisasm->dump[m_nDump++] = ' ';
  2478. for (i = 0; i < m_nImmSize; i++)
  2479. {
  2480. m_nDump += sprintf(m_pDisasm->dump + m_nDump, "%02X", *m_pCmd++);
  2481. }
  2482. }
  2483. }
  2484. else
  2485. {
  2486. m_pCmd += 1 + m_nHasRM + m_nHasSIB + m_nDispSize + m_nImmSize;
  2487. }
  2488. //
  2489. m_ulSize -= 1 + m_nHasRM + m_nHasSIB + m_nDispSize + m_nImmSize;
  2490. }
  2491. // Check that command is not a dangerous one.
  2492. if (m_nMode >= DISASM_DATA)
  2493. {
  2494. for(pdan = g_Dangerous; pdan->mask != 0; pdan++)
  2495. {
  2496. if (((code ^ pdan->code) & pdan->mask) != 0)
  2497. {
  2498. continue;
  2499. }
  2500. if (pdan->type == C_DANGERLOCK && nLockPrefix == 0)
  2501. {
  2502. break; // Command harmless without LOCK prefix
  2503. }
  2504. if ( m_nIsWindowsNT && pdan->type == C_DANGER95)
  2505. {
  2506. break; // Command harmless under Windows NT
  2507. }
  2508. // Dangerous command!
  2509. if (pdan->type == C_DANGER95)
  2510. {
  2511. m_pDisasm->warnings |= DAW_DANGER95;
  2512. }
  2513. else
  2514. {
  2515. m_pDisasm->warnings |= DAW_DANGEROUS;
  2516. }
  2517. break;
  2518. }
  2519. }
  2520. if (m_pDisasm->error == 0 && m_nSoftError != 0)
  2521. {
  2522. m_pDisasm->error = m_nSoftError; // Error, but still display command
  2523. }
  2524. //
  2525. if (m_nMode >= DISASM_FILE)
  2526. {
  2527. if (m_pDisasm->error != DAE_NOERR)
  2528. {
  2529. switch (m_pDisasm->error)
  2530. {
  2531. case DAE_CROSS:
  2532. {
  2533. strcpy(m_pDisasm->comment, ("Command crosses end of memory block"));
  2534. break;
  2535. }
  2536. case DAE_BADCMD:
  2537. {
  2538. strcpy(m_pDisasm->comment, ("Unknown command"));
  2539. break;
  2540. }
  2541. case DAE_BADSEG:
  2542. {
  2543. strcpy(m_pDisasm->comment, ("Undefined segment register"));
  2544. break;
  2545. }
  2546. case DAE_MEMORY:
  2547. {
  2548. strcpy(m_pDisasm->comment, ("Illegal use of register"));
  2549. break;
  2550. }
  2551. case DAE_REGISTER:
  2552. {
  2553. strcpy(m_pDisasm->comment, ("Memory address not allowed"));
  2554. break;
  2555. }
  2556. case DAE_INTERN:
  2557. {
  2558. strcpy(m_pDisasm->comment, ("Internal OLLYDBG error"));
  2559. break;
  2560. }
  2561. default:
  2562. {
  2563. strcpy(m_pDisasm->comment, ("Unknown error"));
  2564. break;
  2565. }
  2566. }
  2567. }
  2568. else if ((m_pDisasm->warnings & DAW_PRIV) != 0 && m_nPrivileged == 0)
  2569. {
  2570. strcpy(m_pDisasm->comment, ("Privileged command"));
  2571. }
  2572. else if ((m_pDisasm->warnings & DAW_IO) != 0 && m_nIOCommand == 0)
  2573. {
  2574. strcpy(m_pDisasm->comment, ("I/O command"));
  2575. }
  2576. else if ((m_pDisasm->warnings & DAW_FARADDR) != 0 && m_nFarCalls == 0)
  2577. {
  2578. if ((m_pDisasm->cmdtype & C_TYPEMASK) == C_JMP)
  2579. {
  2580. strcpy(m_pDisasm->comment, ("Far jump"));
  2581. }
  2582. else if ((m_pDisasm->cmdtype & C_TYPEMASK) == C_CAL)
  2583. {
  2584. strcpy(m_pDisasm->comment, ("Far call"));
  2585. }
  2586. else if ((m_pDisasm->cmdtype & C_TYPEMASK) == C_RET)
  2587. {
  2588. strcpy(m_pDisasm->comment, ("Far return"));
  2589. }
  2590. }
  2591. else if ((m_pDisasm->warnings & DAW_SEGMENT) != 0 && m_nFarCalls == 0)
  2592. {
  2593. strcpy(m_pDisasm->comment, ("Modification of segment register"));
  2594. }
  2595. else if ((m_pDisasm->warnings & DAW_SHIFT) != 0 && m_nBadShift == 0)
  2596. {
  2597. strcpy(m_pDisasm->comment, ("Shift constant out of range 1..31"));
  2598. }
  2599. else if ((m_pDisasm->warnings & DAW_PREFIX) != 0 && m_nExtraPrefix == 0)
  2600. {
  2601. strcpy(m_pDisasm->comment, ("Superfluous prefix"));
  2602. }
  2603. else if ((m_pDisasm->warnings & DAW_LOCK) != 0 && m_nLockedbus == 0)
  2604. {
  2605. strcpy(m_pDisasm->comment, ("LOCK prefix"));
  2606. }
  2607. else if ((m_pDisasm->warnings & DAW_STACK) != 0 && m_nStackAlign == 0)
  2608. {
  2609. strcpy(m_pDisasm->comment, ("Unaligned stack operation"));
  2610. }
  2611. }
  2612. return (srcsize - m_ulSize); // Returns number of recognized bytes
  2613. }