Guest User

Untitled

a guest
Feb 12th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : Assembler.c
  4. Author : RB
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello World in C, Ansi-style
  8. ============================================================================
  9. */
  10.  
  11.  
  12. //IS-Imperative,DL-Declarative,AD-Assembler Directive
  13.  
  14. #include<iostream>
  15. #include<string>
  16. #include<fstream>
  17. #include<stdlib.h>
  18.  
  19. using namespace std;
  20.  
  21. typedef struct abcd
  22. {
  23. string operationname;
  24. string operator_class;
  25. int opcode;
  26. int length;
  27. }opcodetable;
  28.  
  29. typedef struct aghcd
  30. {
  31. string symbolname;
  32. int address;
  33. int length;
  34. }symtab;
  35.  
  36. typedef struct abdwcd
  37. {
  38. string literalname;
  39. int address;
  40. }littab;
  41.  
  42. typedef struct dsdsdds
  43. {
  44. int first;
  45. int number_of_literals;
  46. }pooltab;
  47.  
  48. typedef struct aascd
  49. {
  50. string regname;
  51. int address;
  52. int length;
  53. }regtab;
  54.  
  55. int main(void)
  56. {
  57. opcodetable optable[20];
  58. regtab registers_table[16];
  59. symtab symbols_table[30];
  60. littab literals[20];
  61. pooltab literal_pools[20];
  62.  
  63. int temp,operatorcount=0,symbolcount=0,instruction_addresses[40],num_addresses=0,literalcount=0,curpool=0,lc=0;
  64.  
  65. string opnametemp,opclassname;
  66.  
  67. /*Configuring system registers*/
  68. for(int i=0;i<10;i++)
  69. {
  70. char ch[2];
  71. ch[0]=65+i;
  72. ch[1]='\0';
  73. string abc=ch;
  74. registers_table[i].regname=abc+"REG";
  75. }
  76. registers_table[10].regname="LT";
  77. registers_table[11].regname="LE";
  78. registers_table[12].regname="EQ";
  79. registers_table[13].regname="GT";
  80. registers_table[14].regname="GE";
  81. registers_table[15].regname="ANY";
  82. /*
  83. cout<<"System registers are : "<<endl;
  84. for(int i=0;i<16;i++)
  85. {
  86. cout<<registers_table[i].regname<<endl;
  87. }
  88. */
  89.  
  90. //Reading the operators from file.
  91. ifstream inputfile;
  92. inputfile.open("opinput.txt");
  93. while(1)
  94. {
  95. if(inputfile>>opnametemp)
  96. optable[operatorcount].operationname=opnametemp;
  97. else
  98. break;
  99.  
  100. if(inputfile>>temp)
  101. optable[operatorcount].opcode=temp;
  102. else
  103. break;
  104.  
  105. if(inputfile>>opclassname)
  106. optable[operatorcount].operator_class=opclassname;
  107. else
  108. break;
  109.  
  110. if(inputfile>>temp)
  111. optable[operatorcount].length=temp;
  112. else
  113. break;
  114.  
  115. operatorcount++;
  116. }
  117. inputfile.close();
  118.  
  119. /*
  120. cout<<endl<<"Operations are : "<<endl;
  121. for(int i=0;i<operatorcount;i++)
  122. {
  123. cout<<optable[i].operationname<<"\t"<<optable[i].opcode<<"\t"<<optable[i].operator_class<<"\t"<<optable[i].length<<endl;
  124. }
  125. */
  126.  
  127.  
  128. /*PASS 1*/
  129. int isnewline=0;
  130. char peeking;
  131. string symtemp;
  132.  
  133. literal_pools[0].first=0;
  134. literal_pools[0].number_of_literals=0;
  135.  
  136. string filetobeused;
  137. cout<<"\nEnter the name of the file : ";
  138. cin>>filetobeused;
  139. inputfile.open(filetobeused);
  140. inputfile>>symtemp;
  141. inputfile>>temp;
  142. isnewline=1;
  143. string pooltable[50];
  144. instruction_addresses[num_addresses]=temp;
  145. num_addresses++;
  146. int isnotsymbol=1;
  147. int inslength=1; //account for this later
  148. int poofilled=0;
  149. int getliterals=0;
  150. int nomemtostatement=0;
  151.  
  152. temp=1;
  153.  
  154. cout<<"\nThe intermediate code after pass 1 is : \n";
  155. cout<<"\t(AD,1)"<<"(C,"<<temp<<")"<<endl;
  156. int icopindex=-1,icpoolindex=-1;
  157. int iclitindex=-1;
  158. string icsysreg=" ",icsymbol=" ";
  159. while(inputfile>>symtemp)
  160. {
  161. isnotsymbol=0;
  162. if(symtemp!="END")
  163. {
  164. if(getliterals==1)
  165. {
  166. if(symtemp.at(0)=='=')
  167. {
  168. lc=instruction_addresses[num_addresses-1]+1;
  169. literals[literalcount].literalname=symtemp;
  170. literals[literalcount].address=lc;
  171. iclitindex=literalcount;
  172. literalcount++;
  173. }
  174. else
  175. {
  176. getliterals=0;
  177. curpool++;
  178. literal_pools[curpool].first=literal_pools[curpool-1].number_of_literals+literal_pools[curpool-1].first;
  179. literal_pools[curpool].number_of_literals=0;
  180. }
  181. }
  182. if(getliterals==0)
  183. {
  184. for(int i=0;i<operatorcount;i++)
  185. {
  186. if(symtemp==optable[i].operationname)
  187. {
  188. icopindex=i;
  189. isnotsymbol=1;
  190. isnewline=0;
  191. temp=optable[i].length;
  192. if(num_addresses==1)
  193. {
  194. temp=0;
  195. }
  196. lc=instruction_addresses[num_addresses-1]+temp;
  197. if(optable[i].opcode==-60)
  198. {
  199. icopindex=-1;
  200. getliterals=1;
  201. nomemtostatement=1;
  202. }
  203. break;
  204. }
  205. }
  206. if(isnotsymbol==0)
  207. {
  208. for(int i=0;i<16;i++)
  209. {
  210. if(symtemp==registers_table[i].regname)
  211. {
  212. icsysreg=symtemp;
  213. isnotsymbol=1;
  214. break;
  215. }
  216. }
  217. }
  218. if(isnotsymbol==0)
  219. {
  220. if(symtemp.at(0)=='=')
  221. {
  222. int present=0;
  223. int pool_size=literal_pools[curpool].number_of_literals;
  224. for(int i=0;i<pool_size;i++)
  225. {
  226. if(symtemp==pooltable[i])
  227. {
  228. present=1;
  229. icpoolindex=i;
  230. break;
  231. }
  232. }
  233. if(present==0)
  234. {
  235. pooltable[pool_size]=symtemp;
  236. icpoolindex=pool_size;
  237. literal_pools[curpool].number_of_literals++;
  238. }
  239. }
  240. else
  241. {
  242. if(isnewline==1)
  243. {
  244. symbols_table[symbolcount].symbolname=symtemp;
  245. inputfile>>symtemp;
  246. lc=instruction_addresses[num_addresses-1]+temp;
  247. for(int i=0;i<operatorcount;i++)
  248. {
  249. if(symtemp==optable[i].operationname)
  250. {
  251. isnewline=0;
  252. if(optable[i].opcode==-50)
  253. {
  254. inputfile>>temp;
  255. }
  256. else if(optable[i].opcode==-70)
  257. {
  258. nomemtostatement=1;
  259. inputfile>>symtemp;
  260. for(int h=0;h<symbolcount;h++)
  261. {
  262. if(symtemp==symbols_table[h].symbolname)
  263. {
  264. lc=symbols_table[h].address;
  265. break;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. temp=optable[i].length;
  272. }
  273. icopindex=i;
  274. }
  275. }
  276. symbols_table[symbolcount].address=lc;
  277. symbolcount++;
  278. }
  279. else
  280. {
  281. icsymbol=symtemp;
  282. }
  283. }
  284. }
  285. }
  286.  
  287. peeking=inputfile.peek();
  288. if(peeking=='\n')
  289. {
  290. isnewline=1;
  291. // cout<<instruction_addresses[num_addresses-1]<<endl;
  292. if(nomemtostatement==0)
  293. {
  294. //This is our pass1 output
  295. if(icpoolindex!=-1)
  296. {
  297. if(icopindex!=-1)
  298. {
  299. int printicopcode=optable[icopindex].opcode;
  300. if(printicopcode==-50)
  301. {
  302. printicopcode=1;
  303. string newbuffer;
  304. char buffer[50];
  305. itoa(temp,buffer,10);
  306. newbuffer=buffer;
  307. icsymbol="(C,"+newbuffer+")";
  308. }
  309. cout<<lc<<"("<<optable[icopindex].operator_class<<","<<printicopcode<<") "<<icsysreg<<" "<<"(L,"<<literal_pools[curpool].first+icpoolindex<<")"<<endl;
  310. }
  311. }
  312. else
  313. {
  314. if(icopindex!=-1)
  315. {
  316.  
  317. int printicopcode=optable[icopindex].opcode;
  318. if(printicopcode==-50)
  319. {
  320. printicopcode=1;
  321. string newbuffer;
  322. char buffer[50];
  323. itoa(temp,buffer,10);
  324. newbuffer=buffer;
  325. icsymbol="(C,"+newbuffer+")";
  326. }
  327. cout<<lc<<"("<<optable[icopindex].operator_class<<","<<printicopcode<<") "<<icsysreg<<" "<<icsymbol<<endl;
  328.  
  329. }
  330. else if(iclitindex!=-1)
  331. {
  332. cout<<lc<<" "<<literals[iclitindex].literalname<<endl;
  333. }
  334.  
  335. }
  336. icsysreg=" ";
  337. icsymbol=" ";
  338. icopindex=-1;
  339. icpoolindex=-1;
  340. iclitindex=-1;
  341. //
  342.  
  343. instruction_addresses[num_addresses]=lc;
  344. num_addresses++;
  345. }
  346. else
  347. {
  348. nomemtostatement=0;
  349. }
  350. }
  351. else
  352. isnewline=0;
  353. }
  354. }
  355. cout<<"\t(AD,0)"<<endl;
  356. inputfile.close();
  357.  
  358.  
  359. /*
  360.  
  361. cout<<"\nThe addresses are : ";
  362. cout<<num_addresses<<endl;
  363. for(int i=1;i<num_addresses;i++)
  364. {
  365. cout<<instruction_addresses[i]<<endl;
  366. }
  367.  
  368. cout<<endl<<"The symbols are : "<<endl;
  369. for(int i=0;i<symbolcount;i++)
  370. {
  371. cout<<symbols_table[i].symbolname<<"\t"<<symbols_table[i].address<<endl;
  372. }
  373. cout<<endl;
  374.  
  375. cout<<endl<<"The literals are : "<<endl;
  376. for(int i=0;i<literalcount;i++)
  377. {
  378. cout<<literals[i].literalname<<"\t"<<literals[i].address<<endl;
  379. }
  380. cout<<endl;
  381.  
  382. cout<<endl<<"The literal pool table is : "<<endl;
  383. for(int i=0;i<=curpool;i++)
  384. {
  385. cout<<literal_pools[i].first<<"\t"<<literal_pools[i].number_of_literals<<endl;
  386. }
  387. cout<<endl;
  388.  
  389. */
  390.  
  391.  
  392. /*PASS 2*/
  393. int tempsysaddress=0,tempopcode=0,tempsymaddress=0;
  394.  
  395. inputfile.open(filetobeused);
  396. inputfile>>symtemp;
  397. inputfile>>temp;
  398. int insno=1;
  399. int isfound=0;
  400. string leftover;
  401. char nextchar;
  402. curpool=0;
  403. int ltorgon=0;
  404.  
  405. cout<<"\nMachine code produced after pass 2 is as follows : ";
  406. while(inputfile>>symtemp)
  407. {
  408. if(ltorgon==1)
  409. {
  410. if(symtemp.at(0)=='=')
  411. {
  412. tempopcode=0;
  413. tempsysaddress=0;
  414. leftover=symtemp.substr(1);
  415. }
  416. else
  417. {
  418. ltorgon=0;
  419. curpool++;
  420. }
  421. }
  422. if(ltorgon==0)
  423. {
  424.  
  425. leftover="00";
  426. if(symtemp=="END")
  427. {
  428. break;
  429. }
  430. isfound=0;
  431. for(int i=0;i<operatorcount;i++)
  432. {
  433. if(symtemp==optable[i].operationname)
  434. {
  435. tempopcode=optable[i].opcode;
  436. if(tempopcode==-60)
  437. {
  438. ltorgon=1;
  439. }
  440. isfound=1;
  441. break;
  442. }
  443. }
  444. if(isfound==0)
  445. {
  446. for(int i=0;i<16;i++)
  447. {
  448. if(symtemp==registers_table[i].regname)
  449. {
  450. tempsysaddress=i+1;
  451. isfound=1;
  452. break;
  453. }
  454. }
  455. }
  456. if(isfound==0)
  457. {
  458. for(int i=0;i<symbolcount;i++)
  459. {
  460. if(symtemp==symbols_table[i].symbolname)
  461. {
  462. tempsymaddress=symbols_table[i].address;
  463. isfound=1;
  464. break;
  465. }
  466. }
  467. }
  468. if(isfound==0)
  469. {
  470. int starting=literal_pools[curpool].first;
  471. int ending=literal_pools[curpool].first+literal_pools[curpool].number_of_literals;
  472. for(int i=starting;i<ending;i++)
  473. {
  474. if(symtemp==literals[i].literalname)
  475. {
  476. tempsymaddress=literals[i].address;
  477. isfound=1;
  478. break;
  479. }
  480. }
  481. }
  482. if(isfound==0)
  483. {
  484. leftover=symtemp;
  485. }
  486. }
  487.  
  488. peeking=inputfile.peek();
  489. if(peeking=='\n')
  490. {
  491. if(tempopcode!=-60 && tempopcode!=-70)
  492. {
  493. cout<<endl<<instruction_addresses[insno]<<") ";
  494. if(tempopcode>0)
  495. {
  496. cout<<tempopcode<<" "<<tempsysaddress%10<<" "<<tempsymaddress;
  497. }
  498. if(tempopcode==0)
  499. {
  500. cout<<tempopcode<<" "<<tempsysaddress%10<<" "<<leftover;
  501. }
  502. tempsysaddress=0;
  503. tempopcode=0;
  504. tempsymaddress=0;
  505. insno++;
  506. }
  507. }
  508.  
  509. }
  510. inputfile.close();
  511. cout<<endl;
  512. return 0;
  513. }
Advertisement
Add Comment
Please, Sign In to add comment