disassem.cpp 50 KB

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