disassem.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /****************************************************************************
  2. * dcc project disassembler
  3. * (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
  4. ****************************************************************************/
  5. #include <stdint.h>
  6. #include "dcc.h"
  7. #include "symtab.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <malloc.h> /* For free() */
  11. #include <vector>
  12. #ifdef _CONSOLE
  13. #include <windows.h> /* For console mode routines */
  14. #endif
  15. #include "disassem.h"
  16. // Note: for the time being, there is no interactive disassembler
  17. // for unix
  18. #ifndef __UNIX__
  19. #include <conio.h> // getch() etc
  20. #endif
  21. using namespace std;
  22. #define POS_LAB 15 /* Position of label */
  23. #define POS_OPC 20 /* Position of opcode */
  24. #define POS_OPR 25 /* Position of operand */
  25. #define WID_PTR 10 /* Width of the "xword ptr" lingo */
  26. #define POS_OPR2 POS_OPR+WID_PTR /* Position of operand after "xword ptr" */
  27. #define POS_CMT 54 /* Position of comment */
  28. #define DELTA_ICODE 16 /* Number of icodes to realloc by each time */
  29. static const char *szOps[] =
  30. {
  31. "CBW", "AAA", "AAD", "AAM", "AAS", "ADC", "ADD", "AND",
  32. "BOUND","CALL", "CALL", "CLC", "CLD", "CLI", "CMC", "CMP",
  33. "CMPS", "REPNE CMPS","REPE CMPS","DAA", "DAS", "DEC", "DIV", "ENTER",
  34. "ESC", "HLT", "IDIV", "IMUL", "IN", "INC", "INS", "REP INS",
  35. "INT", "IRET", "JB", "JBE", "JAE", "JA", "JE", "JNE",
  36. "JL", "JGE", "JLE", "JG", "JS", "JNS", "JO", "JNO",
  37. "JP", "JNP", "JCXZ", "JMP", "JMP", "LAHF", "LDS", "LEA",
  38. "LEAVE","LES", "LOCK", "LODS", "REP LODS", "LOOP", "LOOPE","LOOPNE",
  39. "MOV", "MOVS", "REP MOVS", "MUL", "NEG", "NOT", "OR", "OUT",
  40. "OUTS", "REP OUTS", "POP", "POPA", "POPF", "PUSH", "PUSHA","PUSHF",
  41. "RCL", "RCR", "ROL", "ROR", "RET", "RETF", "SAHF", "SAR",
  42. "SHL", "SHR", "SBB", "SCAS", "REPNE SCAS","REPE SCAS", "CWD", "STC",
  43. "STD", "STI", "STOS", "REP STOS", "SUB", "TEST", "WAIT", "XCHG",
  44. "XLAT", "XOR", "INTO", "NOP", "REPNE", "REPE", "MOD"
  45. };
  46. /* The following opcodes are for mod != 3 */
  47. static const char *szFlops1[] =
  48. {
  49. /* 0 1 2 3 4 5 6 7 */
  50. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  51. "FLD", "???", "FST", "???", "FLDENV","FLDCW", "FSTENV","FSTSW", /* 08 */
  52. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 10 */
  53. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  54. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  55. "FLD", "FLD", "FST", "FSTP", "FRESTOR","???", "FSAVE", "FSTSW", /* 28 */
  56. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 30 */
  57. "FILD", "???", "FIST", "FISTP", "FBLD", "???", "FBSTP", "FISTP" /* 38 */
  58. };
  59. /* The following opcodes are for mod == 3 */
  60. static const char *szFlops2[] =
  61. {
  62. /* 0 1 2 3 4 5 6 7 */
  63. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  64. "FLD", "FXCH", "FNOP", "???", "", "", "", "", /* 08 */
  65. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "", "FIDIV", "FIDIVR", /* 10 */
  66. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  67. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  68. "FFREE", "FSTP", "FST", "???", "FUCOM", "FUCOMP","???", "???", /* 28 */
  69. "FADDP", "FMULP", "FICOM","", "FSUBRP","FISUBR","FDIVRP","FDIVP", /* 30 */
  70. "FILD", "???", "FIST", "FISTP", "", "???", "FBSTP", "FISTP" /* 38 */
  71. };
  72. static const char *szFlops0C[] =
  73. {
  74. "FCHS", "FABS", "???", "???", "FTST", "FXAM", "???", "???"
  75. };
  76. static const char *szFlops0D[] =
  77. {
  78. "FLD1", "FLDL2T","FLDL2E","FLDP1", "FLDLG2","FLDLN2","FLDZ", "???"
  79. };
  80. static const char *szFlops0E[] =
  81. {
  82. "F2XM1", "FYL2X", "FPTAN", "FPATAN","FXTRACT","FPREM1","FDECSTP","FINCSTP"
  83. };
  84. static const char *szFlops0F[] =
  85. {
  86. "FPREM", "FYLXP1","FSQRT", "FSINCOS","FRNDINT","FSCALE","FSIN","FCOS"
  87. };
  88. static const char *szFlops15[] =
  89. {
  90. "???", "FUCOMPP", "???", "???", "???", "???", "???", "???"
  91. };
  92. static const char *szFlops1C[] =
  93. {
  94. "???", "???", "FCLEX", "FINIT", "FTST", "FXAM", "???", "???"
  95. };
  96. static const char *szFlops33[] =
  97. {
  98. "???", "FCOMPP", "???", "???", "???", "???", "???", "???"
  99. };
  100. static const char *szFlops3C[] =
  101. {
  102. "FSTSWAX","???", "???", "???", "???", "???", "???", "???"
  103. };
  104. static const char *szIndex[8] = {"bx+si", "bx+di", "bp+si", "bp+di", "si", "di","bp","bx" };
  105. static const char *szBreg[8] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
  106. static const char *szWreg[12] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  107. "es", "cs", "ss", "ds" };
  108. static const char *szPtr[2] = { " word ptr ", " byte ptr " };
  109. static void dis1Line (Int i, boolT fWin, char attr, Int pass);
  110. void dis1LineOp(Int i, boolT fWin, char attr, word *len, Function * pProc);
  111. static void formatRM(char *p, flags32 flg, ICODEMEM* pm);
  112. static char *strDst(flags32 flg, ICODEMEM *pm);
  113. static char *strSrc(ICODE * pc);
  114. static char *strHex(dword d);
  115. static Int checkScanned(dword pcCur);
  116. static void setProc(Function * proc);
  117. static void dispData(word dataSeg);
  118. static void flops(ICODE * pi);
  119. boolT callArg(word off, char *temp); /* Check for procedure name */
  120. static FILE *fp;
  121. static ICODE * pc;
  122. static char buf[200], *p;
  123. static Int cb, j, numIcode, allocIcode, eop;
  124. static vector<int> pl;
  125. static dword nextInst;
  126. static boolT fImpure;
  127. static Int lab, prevPass;
  128. static Function * pProc; /* Points to current proc struct */
  129. struct POSSTACK_ENTRY
  130. {
  131. Int ic; /* An icode offset */
  132. Function * pProc; /* A pointer to a PROCEDURE structure */
  133. } ;
  134. vector<POSSTACK_ENTRY> posStack; /* position stack */
  135. byte iPS; /* Index into the stack */
  136. static char cbuf[256]; /* Has to be 256 for wgetstr() to work */
  137. // These are "curses equivalent" functions. (Used to use curses for all this,
  138. // but it was too much of a distribution hassle
  139. #if _CONSOLE
  140. HANDLE hConsole; /* All 32 bit console style routines need this handle */
  141. #endif
  142. void attrSet(char attrib)
  143. {
  144. #ifdef _CONSOLE
  145. switch (attrib)
  146. {
  147. case A_NORMAL:
  148. SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
  149. break;
  150. case A_REVERSE:
  151. SetConsoleTextAttribute(hConsole,BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
  152. break;
  153. case A_BOLD:
  154. SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED |FOREGROUND_INTENSITY);
  155. break;
  156. }
  157. #else
  158. /* Set the attribute, using VT100 codes */
  159. switch (attrib)
  160. {
  161. case A_NORMAL:
  162. printf("\033[0m");
  163. break;
  164. case A_REVERSE:
  165. printf("\033[7m");
  166. break;
  167. case A_BOLD:
  168. printf("\033[1m");
  169. break;
  170. }
  171. #endif
  172. }
  173. #ifdef _CONSOLE
  174. void initConsole()
  175. {
  176. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  177. }
  178. #endif
  179. void erase(void)
  180. {
  181. #ifdef _CONSOLE
  182. COORD coordScreen = { 0, 0 }; /* here's where we'll home the
  183. cursor */
  184. DWORD cCharsWritten;
  185. CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  186. DWORD dwConSize; /* number of character cells in
  187. the current buffer */
  188. /* get the number of character cells in the current buffer */
  189. GetConsoleScreenBufferInfo( hConsole, &csbi );
  190. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  191. /* fill the entire screen with blanks */
  192. FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',dwConSize, coordScreen, &cCharsWritten );
  193. /* get the current text attribute */
  194. // GetConsoleScreenBufferInfo( hConsole, &csbi );
  195. /* now set the buffer's attributes accordingly */
  196. FillConsoleOutputAttribute( hConsole, csbi.wAttributes,dwConSize, coordScreen, &cCharsWritten );
  197. /* put the cursor at (0, 0) */
  198. SetConsoleCursorPosition( hConsole, coordScreen );
  199. #else
  200. // Assume that ANSI is supported
  201. printf("\033[2J");
  202. #endif
  203. }
  204. void move(int r, int c)
  205. {
  206. #ifdef _CONSOLE
  207. COORD pos;
  208. pos.X = c;
  209. pos.Y = r;
  210. SetConsoleCursorPosition( hConsole, pos );
  211. #else
  212. printf("\033[%d;%dH", r+1, c+1);
  213. #endif
  214. }
  215. #define printfd(x) printf(x)
  216. #define dis_newline() printf("\n")
  217. #define dis_show() // Nothing to do unless using Curses
  218. /*****************************************************************************
  219. * disassem - Prints a disassembled listing of a procedure.
  220. * pass == 1 generates output on file .a1
  221. * pass == 2 generates output on file .a2
  222. * pass == 3 generates output on file .b
  223. ****************************************************************************/
  224. void disassem(Int pass, Function * ppProc)
  225. {
  226. Int i;
  227. pProc = ppProc; /* Save the passes pProc */
  228. if (pass != prevPass)
  229. {
  230. prevPass = pass;
  231. lab = 0; /* Restart label numbers */
  232. }
  233. createSymTables();
  234. allocIcode = numIcode = pProc->Icode.GetNumIcodes();
  235. if ((cb = allocIcode * sizeof(ICODE)) == 0)
  236. {
  237. return; /* No Icode */
  238. }
  239. /* Open the output file (.a1 or .a2 only) */
  240. if (pass != 3)
  241. {
  242. p = (pass == 1)? asm1_name: asm2_name;
  243. fp = fopen(p, "a+");
  244. if (!fp)
  245. {
  246. fatalError(CANNOT_OPEN, p);
  247. }
  248. }
  249. /* Create temporary code array */
  250. // Mike: needs objectising!
  251. pc = (ICODE *)memcpy(allocMem(cb), pProc->Icode.GetFirstIcode(), (size_t)cb);
  252. if (pass == 1)
  253. {
  254. /* Bind jump offsets to labels */
  255. for (i = 0; i < numIcode; i++)
  256. {
  257. if ((pc[i].ic.ll.flg & I) && !(pc[i].ic.ll.flg & JMP_ICODE) &&
  258. JmpInst(pc[i].ic.ll.opcode))
  259. {
  260. /* Replace the immediate operand with an icode index */
  261. if (labelSrch(pc,numIcode, pc[i].ic.ll.immed.op,(Int *)&pc[i].ic.ll.immed.op))
  262. {
  263. /* This icode is the target of a jump */
  264. pc[pc[i].ic.ll.immed.op].ic.ll.flg |= TARGET;
  265. pc[i].ic.ll.flg |= JMP_ICODE; /* So its not done twice */
  266. }
  267. else
  268. {
  269. /* This jump cannot be linked to a label */
  270. pc[i].ic.ll.flg |= NO_LABEL;
  271. }
  272. }
  273. }
  274. }
  275. /* Create label array to keep track of location => label name */
  276. pl.clear();
  277. pl.resize(numIcode,0);
  278. /* Write procedure header */
  279. if (pass != 3)
  280. fprintf(fp, "\t\t%s PROC %s\n", pProc->name, (pProc->flg & PROC_FAR)? "FAR": "NEAR");
  281. /* Loop over array printing each record */
  282. for (i = nextInst = 0; i < numIcode; i++)
  283. {
  284. dis1Line(i, FALSE, 0, pass);
  285. }
  286. /* Write procedure epilogue */
  287. if (pass != 3)
  288. {
  289. fprintf(fp, "\n\t\t%s ENDP\n\n", pProc->name);
  290. fclose(fp);
  291. }
  292. free(pc);
  293. destroySymTables();
  294. }
  295. /****************************************************************************
  296. * dis1Line() - disassemble one line to stream fp * *
  297. * i is index into Icode for this proc *
  298. * It is assumed that icode i is already scanned *
  299. ****************************************************************************/
  300. static void
  301. dis1Line(Int i, boolT fWindow, char attr, Int pass)
  302. {
  303. ICODE * pIcode = &pc[i];
  304. /* Disassembly stage 1 --
  305. * Do not try to display NO_CODE entries or synthetic instructions,
  306. * other than JMPs, that have been introduced for def/use analysis. */
  307. if ((option.asm1) &&
  308. ((pIcode->ic.ll.flg & NO_CODE) ||
  309. ((pIcode->ic.ll.flg & SYNTHETIC) && (pIcode->ic.ll.opcode != iJMP))))
  310. {
  311. return;
  312. }
  313. else if (pIcode->ic.ll.flg & NO_CODE)
  314. {
  315. return;
  316. }
  317. /* p points to the current position in buf[] */
  318. p = (char*)memset(buf, ' ', sizeof(buf));
  319. if (pIcode->ic.ll.flg & (TARGET | CASE))
  320. {
  321. if (fWindow) /* Printing to disassem window? */
  322. dis_newline(); /* Yes */
  323. else if (pass == 3)
  324. cCode.appendCode("\n"); /* No, print to c code buffer */
  325. else
  326. fprintf(fp, "\n"); /* No, print to the stream */
  327. }
  328. /* Find next instruction label and print hex bytes */
  329. if (pIcode->ic.ll.flg & SYNTHETIC)
  330. nextInst = pIcode->ic.ll.label;
  331. else
  332. {
  333. cb = (dword) pIcode->ic.ll.numBytes;
  334. nextInst = pIcode->ic.ll.label + cb;
  335. /* Output hexa code in program image */
  336. if (pass != 3)
  337. {
  338. for (j = 0; j < cb; j++, p += 2)
  339. sprintf(p, "%02X", prog.Image[pIcode->ic.ll.label + j]);
  340. *p = ' ';
  341. }
  342. }
  343. /* Check if there is a symbol here */
  344. selectTable(Label);
  345. if (readVal(&buf[POS_LAB], pIcode->ic.ll.label, 0))
  346. {
  347. buf[strlen(buf)] = ':'; /* Also removes the null */
  348. }
  349. else if (pIcode->ic.ll.flg & TARGET) /* Symbols override Lnn labels */
  350. { /* Print label */
  351. if (! pl[i])
  352. {
  353. pl[i] = ++lab;
  354. }
  355. if (pass == 3)
  356. sprintf(buf, "L%ld", pl[i]);
  357. else
  358. sprintf(&buf[15], "L%ld", pl[i]);
  359. buf[strlen(buf)] = ':'; /* Also removes the null */
  360. }
  361. if (pIcode->ic.ll.opcode == iSIGNEX && (pIcode->ic.ll.flg & B))
  362. {
  363. pIcode->ic.ll.opcode = iCBW;
  364. }
  365. if (pass == 3)
  366. {
  367. strcpy (&buf[8], szOps[pIcode->ic.ll.opcode]);
  368. buf[eop = strlen(buf)] = ' ';
  369. p = buf + 8 + (POS_OPR - POS_OPC);
  370. }
  371. else
  372. {
  373. strcpy(&buf[POS_OPC], szOps[pIcode->ic.ll.opcode]);
  374. buf[eop = strlen(buf)] = ' ';
  375. p = buf + POS_OPR;
  376. }
  377. switch (pIcode->ic.ll.opcode)
  378. {
  379. case iADD: case iADC: case iSUB: case iSBB: case iAND: case iOR:
  380. case iXOR: case iTEST: case iCMP: case iMOV: case iLEA: case iXCHG:
  381. strcpy(p, strDst(pIcode->ic.ll.flg, &pIcode->ic.ll.dst));
  382. strcat(p, strSrc(pIcode));
  383. break;
  384. case iESC:
  385. flops(pIcode);
  386. break;
  387. case iSAR: case iSHL: case iSHR: case iRCL: case iRCR: case iROL:
  388. case iROR:
  389. strcpy(p, strDst(pIcode->ic.ll.flg | I, &pIcode->ic.ll.dst));
  390. strcat(p, (pIcode->ic.ll.flg & I)? strSrc(pIcode): ", cl");
  391. break;
  392. case iINC: case iDEC: case iNEG: case iNOT: case iPOP:
  393. strcpy(p, strDst(pIcode->ic.ll.flg | I, &pIcode->ic.ll.dst));
  394. break;
  395. case iPUSH:
  396. if (pIcode->ic.ll.flg & I)
  397. {
  398. strcpy(p + WID_PTR, strHex(pIcode->ic.ll.immed.op));
  399. }
  400. else
  401. {
  402. strcpy(p, strDst(pIcode->ic.ll.flg | I, &pIcode->ic.ll.dst));
  403. }
  404. break;
  405. case iDIV: case iIDIV: case iMUL: case iIMUL: case iMOD:
  406. if (pIcode->ic.ll.flg & I)
  407. {
  408. strcat(strcpy(p, strDst(pIcode->ic.ll.flg, &pIcode->ic.ll.dst)),", ");
  409. formatRM(p + strlen(p), pIcode->ic.ll.flg, &pIcode->ic.ll.src);
  410. strcat(p, strSrc(pIcode));
  411. }
  412. else
  413. strcpy(p, strDst(pIcode->ic.ll.flg | I, &pIcode->ic.ll.src));
  414. break;
  415. case iLDS: case iLES: case iBOUND:
  416. strcpy(p, strDst(pIcode->ic.ll.flg, &pIcode->ic.ll.dst));
  417. strcat(strcat(p, ", dword ptr"), strSrc(pIcode)+1);
  418. break;
  419. case iJB: case iJBE: case iJAE: case iJA:
  420. case iJL: case iJLE: case iJGE: case iJG:
  421. case iJE: case iJNE: case iJS: case iJNS:
  422. case iJO: case iJNO: case iJP: case iJNP:
  423. case iJCXZ:case iLOOP: case iLOOPE:case iLOOPNE:
  424. case iJMP: case iJMPF:
  425. /* Check if there is a symbol here */
  426. selectTable(Label);
  427. if ((pIcode->ic.ll.immed.op < (dword)numIcode) && /* Ensure in range */
  428. readVal(p+WID_PTR, pc[pIcode->ic.ll.immed.op].ic.ll.label, 0))
  429. {
  430. break; /* Symbolic label. Done */
  431. }
  432. if (pIcode->ic.ll.flg & NO_LABEL)
  433. {
  434. strcpy(p + WID_PTR, strHex(pIcode->ic.ll.immed.op));
  435. }
  436. else if (pIcode->ic.ll.flg & I)
  437. {
  438. j = pIcode->ic.ll.immed.op;
  439. if (! pl[j]) /* Forward jump */
  440. {
  441. pl[j] = ++lab;
  442. }
  443. if (pIcode->ic.ll.opcode == iJMPF)
  444. {
  445. sprintf(p, " far ptr L%ld", pl[j]);
  446. }
  447. else
  448. {
  449. sprintf(p + WID_PTR, "L%ld", pl[j]);
  450. }
  451. }
  452. else if (pIcode->ic.ll.opcode == iJMPF)
  453. {
  454. strcat(strcpy(p-1, "dword ptr"), strSrc(pIcode)+1);
  455. }
  456. else
  457. {
  458. strcpy(p, strDst(I, &pIcode->ic.ll.src));
  459. }
  460. break;
  461. case iCALL: case iCALLF:
  462. if (pIcode->ic.ll.flg & I)
  463. {
  464. sprintf(p, "%s ptr %s",(pIcode->ic.ll.opcode == iCALL) ?" near":" far",(pIcode->ic.ll.immed.proc.proc)->name);
  465. }
  466. else if (pIcode->ic.ll.opcode == iCALLF)
  467. {
  468. strcat(strcpy(p, "dword ptr"),strSrc(pIcode)+1);
  469. }
  470. else
  471. {
  472. strcpy(p, strDst(I, &pIcode->ic.ll.src));
  473. }
  474. break;
  475. case iENTER:
  476. strcat(strcpy(p + WID_PTR, strHex(pIcode->ic.ll.dst.off)), ", ");
  477. strcat(p, strHex(pIcode->ic.ll.immed.op));
  478. break;
  479. case iRET: case iRETF: case iINT:
  480. if (pIcode->ic.ll.flg & I)
  481. {
  482. strcpy(p + WID_PTR, strHex(pIcode->ic.ll.immed.op));
  483. }
  484. else
  485. {
  486. buf[eop] = '\0';
  487. }
  488. break;
  489. case iCMPS: case iREPNE_CMPS: case iREPE_CMPS:
  490. case iSCAS: case iREPNE_SCAS: case iREPE_SCAS:
  491. case iSTOS: case iREP_STOS:
  492. case iLODS: case iREP_LODS:
  493. case iMOVS: case iREP_MOVS:
  494. case iINS: case iREP_INS:
  495. case iOUTS: case iREP_OUTS:
  496. if (pIcode->ic.ll.src.segOver)
  497. {
  498. (pIcode->ic.ll.opcode == iOUTS || pIcode->ic.ll.opcode == iREP_OUTS)
  499. ? strcat(strcpy(p+WID_PTR,"dx, "), szPtr[pIcode->ic.ll.flg & B])
  500. : strcpy(&buf[eop+1], szPtr[pIcode->ic.ll.flg & B]);
  501. if (pIcode->ic.ll.opcode == iLODS ||
  502. pIcode->ic.ll.opcode == iREP_LODS ||
  503. pIcode->ic.ll.opcode == iOUTS ||
  504. pIcode->ic.ll.opcode == iREP_OUTS)
  505. {
  506. strcat(p, szWreg[pIcode->ic.ll.src.segOver-rAX]);
  507. }
  508. else
  509. {
  510. strcat(strcat(p, "es:[di], "),szWreg[pIcode->ic.ll.src.segOver - rAX]);
  511. }
  512. strcat(p, ":[si]");
  513. }
  514. else strcpy(&buf[eop], (pIcode->ic.ll.flg & B)? "B": "W");
  515. break;
  516. case iXLAT:
  517. if (pIcode->ic.ll.src.segOver)
  518. {
  519. strcpy(&buf[eop+1], szPtr[1]);
  520. strcat(strcat(p, szWreg[pIcode->ic.ll.src.segOver-rAX]), ":[bx]");
  521. }
  522. else buf[eop] = '\0';
  523. break;
  524. case iIN:
  525. strcpy(p+WID_PTR, (pIcode->ic.ll.flg & B)?"al, ": "ax, ");
  526. strcat(p+WID_PTR, (pIcode->ic.ll.flg & I)? strHex(pIcode->ic.ll.immed.op): "dx");
  527. break;
  528. case iOUT:
  529. strcpy(p+WID_PTR, (pIcode->ic.ll.flg & I)? strHex(pIcode->ic.ll.immed.op): "dx");
  530. strcat(p+WID_PTR, (pIcode->ic.ll.flg & B)?", al": ", ax");
  531. break;
  532. default:
  533. buf[eop] = '\0';
  534. break;
  535. }
  536. /* Comments */
  537. if (pIcode->ic.ll.flg & SYNTHETIC)
  538. {
  539. fImpure = FALSE;
  540. }
  541. else
  542. {
  543. for (j = pIcode->ic.ll.label, fImpure = 0; j > 0 && j < (Int)nextInst;
  544. j++)
  545. {
  546. fImpure |= BITMAP(j, BM_DATA);
  547. }
  548. }
  549. /* Check for user supplied comment */
  550. selectTable(Comment);
  551. if (readVal(cbuf, pIcode->ic.ll.label, 0))
  552. {
  553. buf[strlen(buf)] = ' '; /* Removes the null */
  554. buf[POS_CMT] = ';';
  555. strcpy(buf+POS_CMT+1, cbuf);
  556. }
  557. else if (fImpure || (pIcode->ic.ll.flg & (SWITCH | CASE | SEG_IMMED |
  558. IMPURE | SYNTHETIC | TERMINATES)))
  559. {
  560. buf[strlen(buf)] = ' ';
  561. buf[POS_CMT] = '\0';
  562. if (pIcode->ic.ll.flg & CASE)
  563. {
  564. sprintf(buf+POS_CMT, ";Case l%ld", pIcode->ic.ll.caseTbl.numEntries);
  565. }
  566. if (pIcode->ic.ll.flg & SWITCH)
  567. {
  568. strcat(buf, ";Switch ");
  569. }
  570. if (fImpure)
  571. {
  572. strcat(buf, ";Accessed as data ");
  573. }
  574. if (pIcode->ic.ll.flg & IMPURE)
  575. {
  576. strcat(buf, ";Impure operand ");
  577. }
  578. if (pIcode->ic.ll.flg & SEG_IMMED)
  579. {
  580. strcat(buf, ";Segment constant");
  581. }
  582. if (pIcode->ic.ll.flg & TERMINATES)
  583. {
  584. strcat(buf, ";Exit to DOS");
  585. }
  586. }
  587. /* Comment on iINT icodes */
  588. if (pIcode->ic.ll.opcode == iINT)
  589. pIcode->writeIntComment (buf);
  590. /* Display output line */
  591. if (! (pIcode->ic.ll.flg & SYNTHETIC))
  592. {
  593. if (fWindow)
  594. {
  595. word off;
  596. char szOffset[6];
  597. off = (word)(pIcode->ic.ll.label - ((dword)pProc->state.r[rCS] << 4));
  598. attrSet(attr);
  599. sprintf(szOffset, "%04X ", off);
  600. printfd(szOffset);
  601. printfd(buf);
  602. dis_newline();
  603. attrSet(A_NORMAL);
  604. }
  605. else if (pass == 3) /* output to .b code buffer */
  606. cCode.appendCode("%s\n", buf);
  607. else /* output to .a1 or .a2 file */
  608. fprintf (fp, "%03ld %06lX %s\n", i, pIcode->ic.ll.label, buf);
  609. }
  610. else /* SYNTHETIC instruction */
  611. {
  612. strcat (buf, ";Synthetic inst");
  613. if (fWindow)
  614. {
  615. printfd(" ");
  616. printfd(buf);
  617. dis_newline();
  618. }
  619. else if (pass == 3) /* output to .b code buffer */
  620. {
  621. cCode.appendCode("%s\n", buf);
  622. }
  623. else /* output to .a1 or .a2 file */
  624. {
  625. fprintf (fp, "%03ld %s\n", i, buf);
  626. }
  627. }
  628. }
  629. /****************************************************************************
  630. * formatRM
  631. ***************************************************************************/
  632. static void formatRM(char *p, flags32 flg, ICODEMEM *pm)
  633. {
  634. char seg[4];
  635. if (pm->segOver)
  636. {
  637. strcat(strcpy(seg, szWreg[pm->segOver - rAX]), ":");
  638. }
  639. else *seg = '\0';
  640. if (pm->regi == 0)
  641. {
  642. sprintf(p,"%s[%s]", seg, strHex((dword)pm->off));
  643. }
  644. else if (pm->regi == (INDEXBASE - 1))
  645. {
  646. strcpy (p, "tmp");
  647. }
  648. else if (pm->regi < INDEXBASE)
  649. {
  650. strcpy(p, (flg & B)? szBreg[pm->regi - rAL]: szWreg[pm->regi - rAX]);
  651. }
  652. else if (pm->off)
  653. {
  654. if (pm->off < 0)
  655. {
  656. sprintf(p,"%s[%s-%s]", seg, szIndex[pm->regi - INDEXBASE],strHex((dword)(- pm->off)));
  657. }
  658. else
  659. {
  660. sprintf(p,"%s[%s+%s]", seg, szIndex[pm->regi - INDEXBASE],strHex((dword)pm->off));
  661. }
  662. }
  663. else sprintf(p,"%s[%s]", seg, szIndex[pm->regi - INDEXBASE]);
  664. }
  665. /*****************************************************************************
  666. * strDst
  667. ****************************************************************************/
  668. static char *strDst(flags32 flg, ICODEMEM *pm)
  669. {
  670. static char buf[30];
  671. /* Immediates to memory require size descriptor */
  672. if ((flg & I) && (pm->regi == 0 || pm->regi >= INDEXBASE))
  673. {
  674. memcpy(buf, szPtr[flg & B], WID_PTR);
  675. }
  676. else
  677. {
  678. memset(buf, ' ', WID_PTR);
  679. }
  680. formatRM(buf + WID_PTR, flg, pm);
  681. return buf;
  682. }
  683. /****************************************************************************
  684. * strSrc *
  685. ****************************************************************************/
  686. static char *strSrc(ICODE *pc)
  687. {
  688. static char buf[30] = {", "};
  689. if (pc->ic.ll.flg & I)
  690. strcpy(buf + 2, strHex(pc->ic.ll.immed.op));
  691. else if (pc->ic.ll.flg & IM_SRC) /* level 2 */
  692. strcpy (buf + 2, "dx:ax");
  693. else
  694. formatRM(buf + 2, pc->ic.ll.flg, &pc->ic.ll.src);
  695. return buf;
  696. }
  697. /****************************************************************************
  698. * strHex *
  699. ****************************************************************************/
  700. static char *strHex(dword d)
  701. {
  702. static char buf[10];
  703. d &= 0xFFFF;
  704. sprintf(buf, "0%lX%s", d, (d > 9)? "h": "");
  705. return (buf + (buf[1] <= '9'));
  706. }
  707. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  708. | Interactive Disassembler and Associated Routines |
  709. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  710. dword pcTop; /* Image offset of top line */
  711. Int icTop; /* Icode index of top line */
  712. dword pcCur; /* Image offset of cursor */
  713. static dword oldPcCur; /* As above, before latest command */
  714. Int icCur; /* Icode index of cursor */
  715. dword pcBot; /* Image offset of bottom line */
  716. Int icBot; /* Icode index of bottom line */
  717. dword pcLast; /* Image offset of last instr in proc */
  718. int NSCROLL; /* Number of limes to scroll. Pseudo constant */
  719. /* Paint the title line */
  720. void dispTitle(void)
  721. {
  722. char buf[80];
  723. move(0, 0); /* Must move before setting attributes */
  724. attrSet(A_BOLD);
  725. sprintf(buf, "Proc %s at %06lX (%04X:%04X): %d bytes of parameters ",pProc->name, pProc->Icode.GetFirstIcode()->ic.ll.label,
  726. pProc->state.r[rCS],(word)(pProc->Icode.GetFirstIcode()->ic.ll.label - ((dword)(pProc->state.r[rCS]) << 4)),
  727. pProc->cbParam);
  728. printfd(buf);
  729. if (pProc->flg & PROC_ISLIB) printfd(" LIBRARY");
  730. attrSet(A_NORMAL);
  731. }
  732. /****************************************************************************
  733. * updateScr - update the screen *
  734. ****************************************************************************/
  735. /* bNew is true if must recalculate the top line */
  736. void updateScr(boolT bNew)
  737. {
  738. int y, x;
  739. Int i, ic;
  740. bNew |= (pcCur > pcBot) || (pcCur < pcTop);
  741. if (bNew)
  742. {
  743. /* We need to redo the screen completely */
  744. erase();
  745. dispTitle();
  746. icTop = icCur;
  747. for (x=0; x < NSCROLL; x++)
  748. {
  749. if (icTop && pc[icTop-1].ic.ll.label +
  750. (dword)pc[icTop-1].ic.ll.numBytes == pc[icTop].ic.ll.label)
  751. {
  752. /* Then this instruction is contiguous with the current */
  753. icTop--;
  754. }
  755. else break;
  756. }
  757. pcTop = pc[icTop].ic.ll.label;
  758. }
  759. else
  760. {
  761. dispTitle();
  762. }
  763. move(1, 0);
  764. nextInst = pcTop;
  765. for (y=1, ic=icTop; y < LINES-1; ic++, y++)
  766. {
  767. if ((ic >= numIcode) || (nextInst != pc[ic].ic.ll.label))
  768. {
  769. if (labelSrch(pc,numIcode, nextInst, &i))
  770. {
  771. ic = i;
  772. }
  773. else
  774. {
  775. pcLast = pc[ic-1].ic.ll.label; /* Remember end of proc */
  776. break; /* Must be past last */
  777. }
  778. }
  779. /* Save pc of current line. Last assignment will be pc of bott line */
  780. pcBot = nextInst;
  781. icBot = ic;
  782. // Only have to repaint if screen is new, or repainting formerly highlighted
  783. // line, or newly highlighted line
  784. if (bNew || (pcCur == nextInst) || (oldPcCur == nextInst))
  785. dis1Line(ic, TRUE, (char)((pcCur == nextInst) ? A_REVERSE : A_NORMAL), 0);
  786. if (ic == numIcode-1)
  787. {
  788. switch (pc[ic].ic.ll.opcode)
  789. {
  790. case iJMP: case iJMPF:
  791. case iRET: case iRETF:
  792. case iIRET:
  793. break;
  794. default:
  795. /* We have other than a break of control flow instruction
  796. at the end of the proc. Parse more instructions to
  797. complete the basic block
  798. */
  799. if ((ic = checkScanned(nextInst)) == -1)
  800. {
  801. /* Some error. */
  802. pcLast = pcCur; /* Remember end of proc */
  803. break; /* Must be past last */
  804. }
  805. }
  806. }
  807. }
  808. dis_show(); /* Make it happen */
  809. }
  810. #if 0
  811. /* An opcode based version of updateScr() */
  812. /****************************************************************************
  813. * updateScrOp - update the screen *
  814. ****************************************************************************/
  815. /* bNew is true if must recalculate the top line */
  816. void
  817. updateScrOp(boolT bNew)
  818. {
  819. int y, x;
  820. dword pc;
  821. word len;
  822. dispTitle();
  823. if (bNew || (pcCur > pcBot) || (pcCur < pcTop))
  824. {
  825. /* We need to redo the screen completely */
  826. pcTop = pcCur;
  827. }
  828. move(1, 0);
  829. for (y=1, pc = pcTop; y < LINES-1;)
  830. {
  831. /* Save pc of current line. Last assignment will be pc of bott line */
  832. pcBot = pc;
  833. dis1LineOp(pc, TRUE, (pcCur == pc) ? A_REVERSE : A_NORMAL, &len,pProc);
  834. pc += len;
  835. getyx(stdscr, y, x);
  836. }
  837. refresh();
  838. }
  839. #endif
  840. void pushPosStack(void)
  841. {
  842. /* Push the current position on the position stack */
  843. posStack[iPS].ic = icCur;
  844. posStack[iPS++].pProc = pProc;
  845. }
  846. static void popPosStack(void)
  847. {
  848. /* Push the current position on the position stack */
  849. /* Note: relies on the byte wraparound. Beware! */
  850. // if ((Int)(posStack[--iPS].pProc) != -1)
  851. if ((intptr_t)(posStack[--iPS].pProc) != intptr_t(-1))
  852. {
  853. if (posStack[iPS].pProc != pProc)
  854. {
  855. setProc(posStack[iPS].pProc);
  856. }
  857. icCur = posStack[iPS].ic;
  858. pcCur = pc[icCur].ic.ll.label;
  859. }
  860. else iPS++; /* Stack empty.. don't pop */
  861. }
  862. /* Check to see if there is an icode for given image offset.
  863. Scan it if necessary, adjusting the allocation of pc[] and pl[]
  864. if necessary. Returns -1 if an error, otherwise the icode offset
  865. */
  866. static Int checkScanned(dword pcCur)
  867. {
  868. Int i;
  869. /* First we check if the current icode is in range */
  870. /* A sanity check first */
  871. if (pcCur >= (dword)prog.cbImage)
  872. {
  873. /* Couldn't be! */
  874. return -1;
  875. }
  876. if (!labelSrch(pc,numIcode, pcCur, &i))
  877. {
  878. /* This icode does not exist yet. Tack it on the end of the existing */
  879. if (numIcode >= allocIcode)
  880. {
  881. allocIcode = numIcode + DELTA_ICODE; /* Make space for this one, and a few more */
  882. pc = (ICODE *)reallocVar(pc, allocIcode * sizeof(ICODE));
  883. /* It is important to clear the new icodes, to ensure that the type
  884. is set to NOT_SCANNED */
  885. memset(&pc[numIcode], 0, (size_t)(allocIcode-numIcode)*sizeof(ICODE));
  886. pl.resize(allocIcode);
  887. memset(&pl[numIcode], 0, (size_t)(allocIcode-numIcode)*sizeof(Int));
  888. }
  889. i = numIcode++;
  890. }
  891. if (pc[i].type == NOT_SCANNED)
  892. {
  893. /* This is a new icode not even scanned yet. Scan it now */
  894. /* Ignore most errors... at this stage */
  895. if (scan(pcCur, &pc[i]) == IP_OUT_OF_RANGE)
  896. {
  897. /* Something went wrong... just forget it */
  898. return -1;
  899. }
  900. }
  901. return i;
  902. }
  903. /* Set up to use the procedure proc */
  904. /* This includes some important initialisations, allocations, etc that are
  905. normally done in disassem() */
  906. static void setProc(Function * proc)
  907. {
  908. Int i;
  909. pProc = proc; /* Keep in a static */
  910. /* Free old arrays, if any */
  911. if (pc) free(pc);
  912. pl.clear();
  913. /* Create temporary code array */
  914. numIcode = pProc->Icode.GetNumIcodes();
  915. cb = numIcode * sizeof(ICODE);
  916. // Mike: needs objectising
  917. pc = (ICODE *)memcpy(allocMem(cb), pProc->Icode.GetFirstIcode(), (size_t)cb);
  918. /* Create label array to keep track of location => label name */
  919. pl.clear();
  920. pl.resize(numIcode,0);
  921. /* Bind jump offsets to labels */
  922. for (i = 0; i < numIcode; i++)
  923. {
  924. if ((pc[i].ic.ll.flg & I) && !(pc[i].ic.ll.flg & JMP_ICODE) &&
  925. JmpInst(pc[i].ic.ll.opcode))
  926. {
  927. /* Immediate jump instructions. Make dest an icode index */
  928. if (labelSrch(pc,numIcode, pc[i].ic.ll.immed.op, (Int *)&pc[i].ic.ll.immed.op))
  929. {
  930. /* This icode is the target of a jump */
  931. pc[pc[i].ic.ll.immed.op].ic.ll.flg |= TARGET;
  932. pc[i].ic.ll.flg |= JMP_ICODE; /* So its not done twice */
  933. }
  934. else
  935. {
  936. /* This jump cannot be linked to a label */
  937. pc[i].ic.ll.flg |= NO_LABEL;
  938. }
  939. }
  940. }
  941. /* Window initially scrolled with entry point on top */
  942. pcCur = pcTop = pProc->procEntry;
  943. labelSrch(pc,numIcode, pcCur, &icCur);
  944. /* pcLast is set properly in updateScr(), at least for now */
  945. pcLast = (dword)-1;
  946. }
  947. /****************************************************************************
  948. * interactDis - interactive disassembler *
  949. ****************************************************************************/
  950. void interactDis(Function * initProc, Int initIC)
  951. {
  952. #ifdef __UNIX__
  953. printf("Sorry - interactive disasassembler option not available for Unix\n");
  954. return;
  955. #else
  956. boolT fInteract;
  957. int nEsc = 0; /* This cycles 0 1 2 for Esc [ X under Unix */
  958. /* and 0 1 for NULL X under Dos */
  959. int ch;
  960. Int i;
  961. pProc = initProc; /* Keep copy of init proc */
  962. NSCROLL = max(3, LINES >> 3); /* Number of lines to scroll */
  963. /* Allocate the position stack */
  964. posStack = (POSSTACK_ENTRY*)allocMem(256 * sizeof(POSSTACK_ENTRY));
  965. iPS = 0;
  966. memset(posStack, -1, 256 * sizeof(POSSTACK_ENTRY));
  967. /* Initialise the console interface, if required */
  968. initConsole();
  969. /* Initially, work on the given proc */
  970. setProc(initProc);
  971. if (initIC)
  972. {
  973. icCur = initIC;
  974. pcCur = pc[icCur].ic.ll.label;
  975. }
  976. /* Initialise the symbol table */
  977. createSymTables();
  978. strcpy(cbuf, "label"); /* Provide a default label string */
  979. updateScr(TRUE);
  980. fInteract = TRUE;
  981. while (fInteract)
  982. {
  983. ch = ::_getch(); // Mike: need a Unix equivalent of getch()!
  984. #ifdef __MSDOS__
  985. if (nEsc)
  986. {
  987. ch += EXT; /* Got the NULL before, so this is extended */
  988. nEsc = 0;
  989. }
  990. else if (ch == 0)
  991. {
  992. nEsc = 1; /* Got one escape (actually, NULL) char */
  993. break;
  994. }
  995. #endif
  996. #ifdef __UNIX__
  997. switch (nEsc)
  998. {
  999. case 1: /* Already got one escape */
  1000. if (ch == '[')
  1001. {
  1002. nEsc++; /* Got 2 chars in the escape sequence */
  1003. break;
  1004. }
  1005. else
  1006. {
  1007. /* Escape something else. Ignore */
  1008. nEsc = 0;
  1009. }
  1010. break;
  1011. case 2:
  1012. /* Already got Esc [ ... */
  1013. ch += EXT; /* Make it an extended key */
  1014. nEsc = 0; /* Reset the escape state */
  1015. break;
  1016. case 0:
  1017. /* No escapes... yet */
  1018. if (ch == 0x1B)
  1019. {
  1020. nEsc++; /* That's one escape... */
  1021. break;
  1022. }
  1023. }
  1024. #endif
  1025. // For consoles, we get a 0xE0 then KEY_DOWN for the normal down arrow character.
  1026. // We simply ignore the 0xE0; this has the effect that the numeric keypad keys
  1027. // work as well (regardless of numlock state).
  1028. oldPcCur = pcCur;
  1029. switch (ch)
  1030. {
  1031. case KEY_DOWN:
  1032. if (pcCur >= pcLast) continue; /* Ignore it */
  1033. pcCur += pc[icCur].ic.ll.numBytes;
  1034. labelSrch(pc,numIcode, pcCur, &icCur);
  1035. if (pcCur >= pcBot)
  1036. {
  1037. int j;
  1038. /* We have gone past the bottom line. Scroll a few lines */
  1039. for (j=0; j < NSCROLL; j++)
  1040. {
  1041. if (pcTop >= pcLast)
  1042. {
  1043. break;
  1044. }
  1045. pcTop += pc[icTop].ic.ll.numBytes;
  1046. if (labelSrch(pc,numIcode, pcTop, &i))
  1047. icTop = i;
  1048. else break; /* Some problem... no more scroll */
  1049. }
  1050. }
  1051. updateScr(FALSE);
  1052. break;
  1053. case KEY_UP:
  1054. /* First simply try the prev icode */
  1055. if ((icCur == 0) ||
  1056. pc[--icCur].ic.ll.label + (dword)pc[icCur].ic.ll.numBytes != pcCur)
  1057. {
  1058. for (i = 0; i < numIcode; i++)
  1059. {
  1060. if (pc[i].ic.ll.label + (dword)pc[i].ic.ll.numBytes == pcCur)
  1061. {
  1062. break; /* This is the one! */
  1063. }
  1064. }
  1065. if (pc[i].ic.ll.label + pc[i].ic.ll.numBytes != pcCur)
  1066. break; /* Not found. Sorry! */
  1067. icCur = i;
  1068. }
  1069. pcCur = pc[icCur].ic.ll.label;
  1070. updateScr(FALSE);
  1071. break;
  1072. case '2': /* Think up a better key... */
  1073. /* As for right arrow, but considers source operand first */
  1074. if (pc[icCur].ic.ll.src.off != 0)
  1075. {
  1076. pushPosStack();
  1077. pcCur = pc[icCur].ic.ll.src.off;
  1078. if (!labelSrch(pc,numIcode, pcCur, &icCur))
  1079. break;
  1080. updateScr(FALSE);
  1081. }
  1082. /* Fall through to KEY_RIGHT processing */
  1083. case KEY_RIGHT:
  1084. if (pc[icCur].ic.ll.flg & I)
  1085. {
  1086. if ((pc[icCur].ic.ll.opcode >= iJB) &&
  1087. (pc[icCur].ic.ll.opcode <= iJMPF))
  1088. {
  1089. /* An immediate jump op. Jump to it */
  1090. pushPosStack();
  1091. if (pc[icCur].ic.ll.flg & JMP_ICODE)
  1092. {
  1093. /* immed.op is an icode offset */
  1094. icCur = pc[icCur].ic.ll.immed.op;
  1095. pcCur = pc[icCur].ic.ll.label;
  1096. }
  1097. else
  1098. {
  1099. /* immed.op is still an image offset.
  1100. Quite likely we need to scan */
  1101. pcCur = pc[icCur].ic.ll.immed.op;
  1102. if ((icCur = checkScanned(pcCur)) == -1)
  1103. break;
  1104. }
  1105. }
  1106. else if ((pc[icCur].ic.ll.opcode == iCALL) ||
  1107. (pc[icCur].ic.ll.opcode == iCALLF))
  1108. {
  1109. /* The dest is a pointer to a proc struct */
  1110. // First check that the procedure has icodes (e.g. may be
  1111. // a library function, or just not disassembled yet)
  1112. Function * pp = (Function *)pc[icCur].ic.ll.immed.op;
  1113. if (pp->Icode.GetFirstIcode() != NULL)
  1114. {
  1115. pushPosStack();
  1116. setProc(pp);
  1117. }
  1118. }
  1119. else
  1120. {
  1121. /* Other immediate */
  1122. pushPosStack();
  1123. pcCur = pc[icCur].ic.ll.immed.op;
  1124. dispData(pProc->state.r[rDS]);
  1125. break;
  1126. }
  1127. }
  1128. else if (pc[icCur].ic.ll.dst.off != 0)
  1129. {
  1130. pushPosStack();
  1131. pcCur = pc[icCur].ic.ll.dst.off;
  1132. if (!labelSrch(pc,numIcode, pcCur, &icCur))
  1133. {
  1134. dispData(pProc->state.r[rDS]);
  1135. break;
  1136. }
  1137. }
  1138. else if (pc[icCur].ic.ll.src.off != 0)
  1139. {
  1140. pushPosStack();
  1141. pcCur = pc[icCur].ic.ll.src.off;
  1142. if (!labelSrch(pc,numIcode, pcCur, &icCur))
  1143. {
  1144. dispData(pProc->state.r[rDS]);
  1145. break;
  1146. }
  1147. }
  1148. updateScr(TRUE);
  1149. break;
  1150. case KEY_LEFT:
  1151. popPosStack();
  1152. pcCur = pc[icCur].ic.ll.label;
  1153. updateScr(TRUE);
  1154. break;
  1155. case KEY_NPAGE:
  1156. pcCur = pcTop = pcBot; /* Put bottom line at top now */
  1157. icCur = icTop = icBot;
  1158. updateScr(FALSE);
  1159. break;
  1160. case KEY_PPAGE:
  1161. pcTop -= (LINES-2) * 2; /* Average of 2 bytes per inst */
  1162. for (i = 0; i < numIcode; i++)
  1163. {
  1164. if ((pc[i].ic.ll.label <= pcTop) &&
  1165. (pc[i].ic.ll.label + (dword)pc[i].ic.ll.numBytes >= pcTop))
  1166. {
  1167. break; /* This is the spot! */
  1168. }
  1169. }
  1170. if (i >= numIcode)
  1171. {
  1172. /* Something went wrong. Goto to first icode */
  1173. i = 0;
  1174. }
  1175. icCur = icTop = i;
  1176. pcCur = pcTop = pc[i].ic.ll.label;
  1177. updateScr(FALSE);
  1178. break;
  1179. case 'l': /* Add a symbolic label here */
  1180. {
  1181. char *pStr;
  1182. move(LINES, 0);
  1183. printf("Enter symbol: ");
  1184. gets(cbuf); /* Get a string to buf */
  1185. move (LINES, 0);
  1186. printf("%50c", ' ');
  1187. if (strlen(cbuf) >= SYMLEN)
  1188. {
  1189. /* Name too ling. Truncate */
  1190. cbuf[SYMLEN-1] = '\0';
  1191. }
  1192. pStr = addStrTbl(cbuf); /* Add to the string table */
  1193. selectTable(Label); /* Select the label table */
  1194. /* Add the symbol to both value- and symbol- hashed tables */
  1195. enterSym(pStr, pcCur, pProc, TRUE);
  1196. if (icCur == 0)
  1197. {
  1198. /* We are at the first icode of a function.
  1199. Assume it is the entry point, and rename the function */
  1200. strcpy(pProc->name, cbuf);
  1201. }
  1202. updateScr(FALSE);
  1203. break;
  1204. }
  1205. case ';':
  1206. {
  1207. char *pStr;
  1208. word w;
  1209. if (findVal(pcCur, 0, &w))
  1210. {
  1211. readVal(cbuf, pcCur, 0);/* Make it the default string */
  1212. deleteVal(pcCur, 0, FALSE);
  1213. }
  1214. else
  1215. {
  1216. cbuf[0] = '\0'; /* Remove prev string */
  1217. }
  1218. /* Enter a comment here, from a window */
  1219. move(LINES, 0);
  1220. printf("Enter comment: ");
  1221. gets(cbuf); /* Get a string to buf */
  1222. move(LINES, 0);
  1223. printf("%50c", ' ');
  1224. pStr = addStrTbl(cbuf); /* Add to the string table */
  1225. selectTable(Comment);
  1226. enterSym(pStr, pcCur, pProc, FALSE);/* Add the symbol */
  1227. updateScr(FALSE);
  1228. break;
  1229. }
  1230. case 'X' & 0x1F: /* Control X; can't use Alt with Unix */
  1231. fInteract = FALSE; /* Exit interactive mode */
  1232. attrSet(A_NORMAL); /* Normal attributes */
  1233. break;
  1234. }
  1235. }
  1236. free(posStack);
  1237. destroySymTables();
  1238. #endif // #ifdef unix
  1239. }
  1240. /****************************************************************************
  1241. * Display the current image position as data *
  1242. ****************************************************************************/
  1243. static void
  1244. dispData(word dataSeg)
  1245. {
  1246. int y, c, i;
  1247. Int pc, pcStart;
  1248. Int off = (Int)dataSeg << 4;
  1249. char szOffset[6], szByte[4];
  1250. if (pcCur >= (dword)prog.cbImage)
  1251. {
  1252. /* We're at an invalid address. Use 0x100 instead */
  1253. pcCur = 0;
  1254. }
  1255. erase();
  1256. dispTitle();
  1257. pcStart = pc = pcCur; /* pc at start of line */
  1258. for (y=1; y < LINES-1; y++)
  1259. {
  1260. move (y, 1);
  1261. sprintf(szOffset, "%04lX ", pc);
  1262. printfd(szOffset);
  1263. for (i=0; i < 16; i++)
  1264. {
  1265. sprintf(szByte, "%02X ", prog.Image[pc++ + off]);
  1266. printfd(szByte);
  1267. if ((pc + off) > prog.cbImage) break;
  1268. }
  1269. pc = pcStart;
  1270. for (i=0; i < 16; i++)
  1271. {
  1272. c = prog.Image[pc++ + off];
  1273. if ((c < 0x20) || (c > 0x7E))
  1274. {
  1275. c = '.';
  1276. }
  1277. szByte[0] = (char)c;
  1278. szByte[1] = '\0';
  1279. printfd(szByte);
  1280. if ((pc + off) > prog.cbImage) break;
  1281. }
  1282. dis_newline();
  1283. pcStart = pc;
  1284. if ((pc + off) > prog.cbImage) break;
  1285. /* getyx(stdscr, y, x); */
  1286. }
  1287. }
  1288. boolT callArg(word off, char *sym)
  1289. {
  1290. dword imageOff;
  1291. imageOff = off + ((dword)pProc->state.r[rCS] << 4);
  1292. /* Search procedure list for one with appropriate entry point */
  1293. FunctionListType::iterator iter= std::find_if(pProcList.begin(),pProcList.end(),
  1294. [imageOff](const Function &f) -> bool { return f.procEntry==imageOff; });
  1295. if(iter==pProcList.end())
  1296. {
  1297. /* No existing proc entry */
  1298. //ERROR: dereferencing NULL !?!
  1299. //LibCheck(*iter);
  1300. Function *x=Function::Create();
  1301. x->procEntry=imageOff;
  1302. LibCheck(*x);
  1303. if (x->flg & PROC_ISLIB)
  1304. {
  1305. /* No entry for this proc, but it is a library function.
  1306. Create an entry for it */
  1307. pProcList.push_back(x);
  1308. iter = (++pProcList.rbegin()).base();
  1309. }
  1310. }
  1311. if(iter==pProcList.end())
  1312. return false;
  1313. /* We have a proc entry for this procedure. Copy the name */
  1314. strcpy(sym, iter->name);
  1315. return true;
  1316. }
  1317. /* Handle the floating point opcodes (icode iESC) */
  1318. static void flops(ICODE *pIcode)
  1319. {
  1320. char bf[30];
  1321. byte op = (byte)pIcode->ic.ll.immed.op;
  1322. /* Note that op is set to the escape number, e.g.
  1323. esc 0x38 is FILD */
  1324. if ((pIcode->ic.ll.dst.regi == 0) || (pIcode->ic.ll.dst.regi >= INDEXBASE))
  1325. {
  1326. /* The mod/rm mod bits are not set to 11 (i.e. register).
  1327. This is the normal floating point opcode */
  1328. strcpy(&buf[POS_OPC], szFlops1[op]);
  1329. buf[strlen(buf)] = ' ';
  1330. if ((op == 0x29) || (op == 0x1F))
  1331. {
  1332. strcpy(bf, "tbyte ptr ");
  1333. }
  1334. else switch (op & 0x30)
  1335. {
  1336. case 0x00:
  1337. case 0x10:
  1338. strcpy(bf, "dword ptr ");
  1339. break;
  1340. case 0x20:
  1341. strcpy(bf, "qword ptr ");
  1342. break;
  1343. case 0x30:
  1344. switch (op)
  1345. {
  1346. case 0x3C: /* FBLD */
  1347. case 0x3E: /* FBSTP */
  1348. strcpy(bf, "tbyte ptr ");
  1349. break;
  1350. case 0x3D: /* FILD 64 bit */
  1351. case 0x3F: /* FISTP 64 bit */
  1352. strcpy(bf, "qword ptr ");
  1353. break;
  1354. default:
  1355. strcpy(bf, "word ptr ");
  1356. break;
  1357. }
  1358. }
  1359. formatRM(bf + 10, pIcode->ic.ll.flg, &pIcode->ic.ll.dst);
  1360. strcpy(p, bf);
  1361. }
  1362. else
  1363. {
  1364. /* The mod/rm mod bits are set to 11 (i.e. register).
  1365. Could be specials (0x0C-0x0F, etc), or the st(i) versions of
  1366. normal opcodes. Because the opcodes are slightly different for
  1367. this case (e.g. op=04 means FSUB if reg != 3, but FSUBR for
  1368. reg == 3), a separate table is used (szFlops2). */
  1369. switch (op)
  1370. {
  1371. case 0x0C:
  1372. strcpy(&buf[POS_OPC], szFlops0C[pIcode->ic.ll.dst.regi - rAX]);
  1373. break;
  1374. case 0x0D:
  1375. strcpy(&buf[POS_OPC], szFlops0D[pIcode->ic.ll.dst.regi - rAX]);
  1376. break;
  1377. case 0x0E:
  1378. strcpy(&buf[POS_OPC], szFlops0E[pIcode->ic.ll.dst.regi - rAX]);
  1379. break;
  1380. case 0x0F:
  1381. strcpy(&buf[POS_OPC], szFlops0F[pIcode->ic.ll.dst.regi - rAX]);
  1382. break;
  1383. case 0x15:
  1384. strcpy(&buf[POS_OPC], szFlops15[pIcode->ic.ll.dst.regi - rAX]);
  1385. break;
  1386. case 0x1C:
  1387. strcpy(&buf[POS_OPC], szFlops1C[pIcode->ic.ll.dst.regi - rAX]);
  1388. break;
  1389. case 0x33:
  1390. strcpy(&buf[POS_OPC], szFlops33[pIcode->ic.ll.dst.regi - rAX]);
  1391. break;
  1392. case 0x3C:
  1393. strcpy(&buf[POS_OPC], szFlops3C[pIcode->ic.ll.dst.regi - rAX]);
  1394. break;
  1395. default:
  1396. strcpy(&buf[POS_OPC], szFlops2[op]);
  1397. buf[strlen(buf)] = ' ';
  1398. if ((op >= 0x20) && (op <= 0x27))
  1399. {
  1400. /* This is the ST(i), ST form. */
  1401. sprintf(&buf[POS_OPR2], "ST(%d),ST", pIcode->ic.ll.dst.regi - rAX);
  1402. }
  1403. else
  1404. {
  1405. /* ST, ST(i) */
  1406. sprintf(&buf[POS_OPR2], "ST,ST(%d)", pIcode->ic.ll.dst.regi - rAX);
  1407. }
  1408. break;
  1409. }
  1410. }
  1411. }