Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string>
  3. #include <map>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #define HEX 1
  8. #define BIN 0
  9. using namespace std;
  10. class Instruction
  11. {
  12. private:
  13. char format;
  14. string name;
  15. string line;
  16. string operand1;
  17. string operand2;
  18. string destination;
  19. int opcode;
  20. int shamt,funct,address,jaddress,base;
  21. map<string,char> NameToFormat;
  22. map<string,int> NameToOpcode;
  23. map<string,int> RegisterToCode;
  24. map<string,string> BinToHex;
  25. map<string,int> NameToFunct;
  26. static map<std::string,int> JumpLabels;
  27. string code;
  28. int strToInt(string s)
  29. {
  30. int num=0;
  31. int i=0;
  32. if(s[i]=='-')i++;
  33. while(i<int(s.size()))
  34. {
  35. num*=10;num+=s[i++]-'0';
  36. }
  37. if(s[0]=='-')num=-num;
  38. return num;
  39. }
  40. string bits(int x,int b)
  41. {
  42. string ret="";
  43. if(x>=0)
  44. {
  45. while(b--)
  46. {
  47. char c='0';
  48. if(x%2)c='1';
  49. ret=c+ret;
  50. x/=2;
  51. }
  52. }
  53. else
  54. {
  55. x=-x;
  56. while(b--)
  57. {
  58. char c='0';
  59. if(x%2)c='1';
  60. ret=c+ret;
  61. x/=2;
  62. }
  63. for(int i=0;i<int(ret.size());i++)ret[i]='1'-ret[i]+'0';
  64. for(int i=ret.size()-1;i>=0;i--)
  65. {
  66. if(ret[i]=='1')ret[i]='0';
  67. else{ret[i]='1';break;}
  68. }
  69. }
  70. return ret;
  71. }
  72. string Hex(string s)
  73. {
  74. string ret="";
  75. for(int i=0;i<int(s.size());i+=4)
  76. {
  77. string cur="";
  78. for(int j=i;j<i+4;j++)cur+=s[j];
  79. ret+=BinToHex[cur];
  80. }
  81. return ret;
  82. }
  83. bool validChar(char c)
  84. {
  85. if(c==' ' || c=='\n' || c=='\r' || c=='\t' || c==',' || c=='(' || c==')')return false;
  86. return true;
  87. }
  88. void setBase(int code_base)
  89. {
  90. base=code_base;
  91. }
  92. void setData()
  93. {
  94. NameToOpcode["add"]=0x00;
  95. NameToOpcode["addi"]=0x08;
  96. NameToOpcode["and"]=0x00;
  97. NameToOpcode["andi"]=0x0C;
  98. NameToOpcode["beq"]=0x04;
  99. NameToOpcode["jal"]=0x03;
  100. NameToOpcode["jr"]=0x00;
  101. NameToOpcode["j"]=0x02;
  102. NameToOpcode["nor"]=0x00;
  103. NameToOpcode["or"]=0x00;
  104. NameToOpcode["ori"]=0x0D;
  105. NameToOpcode["slt"]=0x00;
  106. NameToOpcode["sll"]=0x00;
  107. NameToOpcode["lw"]=0x23;
  108. NameToOpcode["sw"]=0x23;
  109. //=======================//
  110. NameToFormat["add"]='R';
  111. NameToFormat["addi"]='I';
  112. NameToFormat["and"]='R';
  113. NameToFormat["andi"]='I';
  114. NameToFormat["beq"]='I';
  115. NameToFormat["jal"]='J';
  116. NameToFormat["jr"]='R';
  117. NameToFormat["j"]='J';
  118. NameToFormat["nor"]='R';
  119. NameToFormat["or"]='R';
  120. NameToFormat["ori"]='I';
  121. NameToFormat["slt"]='R';
  122. NameToFormat["sll"]='R';
  123. NameToFormat["lw"]='I';
  124. NameToFormat["sw"]='I';
  125. //======================//
  126. RegisterToCode["$zero"]=0;
  127. RegisterToCode["$at"]=1;
  128. RegisterToCode["$v0"]=2;
  129. RegisterToCode["$v1"]=3;
  130. RegisterToCode["$a0"]=4;
  131. RegisterToCode["$a1"]=5;
  132. RegisterToCode["$a2"]=6;
  133. RegisterToCode["$a3"]=7;
  134. RegisterToCode["$t0"]=8;
  135. RegisterToCode["$t1"]=9;
  136. RegisterToCode["$t2"]=10;
  137. RegisterToCode["$t3"]=11;
  138. RegisterToCode["$t4"]=12;
  139. RegisterToCode["$t5"]=13;
  140. RegisterToCode["$t6"]=14;
  141. RegisterToCode["$t7"]=15;
  142. RegisterToCode["$s0"]=16;
  143. RegisterToCode["$s1"]=17;
  144. RegisterToCode["$s2"]=18;
  145. RegisterToCode["$s3"]=19;
  146. RegisterToCode["$s4"]=20;
  147. RegisterToCode["$s5"]=21;
  148. RegisterToCode["$s6"]=22;
  149. RegisterToCode["$s7"]=23;
  150. RegisterToCode["$t8"]=24;
  151. RegisterToCode["$t9"]=25;
  152. RegisterToCode["$k0"]=26;
  153. RegisterToCode["$k1"]=27;
  154. RegisterToCode["$gp"]=28;
  155. RegisterToCode["$sp"]=29;
  156. RegisterToCode["$s8"]=30;
  157. RegisterToCode["$fp"]=30;
  158. RegisterToCode["$ra"]=31;
  159. //=======================//
  160. NameToFunct["add"]=0x20;
  161. NameToFunct["and"]=0x24;
  162. NameToFunct["jr"]=0x08;
  163. NameToFunct["nor"]=0x27;
  164. NameToFunct["or"]=0x25;
  165. NameToFunct["slt"]=0x2A;
  166. NameToFunct["sll"]=0x00;
  167. //======================//
  168. BinToHex["0000"]="0";
  169. BinToHex["0001"]="1";
  170. BinToHex["0010"]="2";
  171. BinToHex["0011"]="3";
  172. BinToHex["0100"]="4";
  173. BinToHex["0101"]="5";
  174. BinToHex["0110"]="6";
  175. BinToHex["0111"]="7";
  176. BinToHex["1000"]="8";
  177. BinToHex["1001"]="9";
  178. BinToHex["1010"]="A";
  179. BinToHex["1011"]="B";
  180. BinToHex["1100"]="C";
  181. BinToHex["1101"]="D";
  182. BinToHex["1110"]="E";
  183. BinToHex["1111"]="F";
  184. }
  185. void readLine(string s)
  186. {
  187. line = s;
  188. //getline(cin,line);
  189. }
  190. void setJumpLabels(map<string, int> map)
  191. {
  192. JumpLabels = map;
  193. }
  194. string readItem()
  195. {
  196. string item="";
  197. int i=0;
  198. while(i<int(line.size()) && !validChar(line[i]))i++;
  199. while(i<int(line.size()) && validChar(line[i]))item+=line[i++];
  200. while(i<int(line.size()) && !validChar(line[i]))i++;
  201. line=line.substr(i);
  202. return item;
  203. }
  204. void setName()
  205. {
  206. name=readItem();
  207. }
  208.  
  209. void setFormat()
  210. {
  211. format=NameToFormat[name];
  212. }
  213.  
  214. void setOpcode()
  215. {
  216. opcode=NameToOpcode[name];
  217. }
  218. void setOperands()
  219. {
  220.  
  221. if(name=="lw" || name=="sw")
  222. {
  223. operand2=readItem();
  224. address=strToInt(readItem());
  225. operand1=readItem();
  226. }
  227. else if(name=="sll")
  228. {
  229. destination=readItem();
  230. operand2=readItem();
  231. shamt=strToInt(readItem());
  232. }
  233. else if(name == "jr")
  234. {
  235. operand1=readItem();
  236. }
  237. else if(name == "beq")
  238. {
  239. operand1=readItem();
  240. operand2=readItem();
  241. address=JumpLabels[readItem()];
  242. }
  243. else if(format=='R')
  244. {
  245. destination=readItem();
  246. operand1=readItem();
  247. operand2=readItem();
  248. }
  249. else if(format=='I')
  250. {
  251. operand2=readItem();
  252. operand1=readItem();
  253. address=strToInt(readItem());
  254. }
  255. else if(format == 'J')
  256. {
  257. jaddress = JumpLabels[readItem()];
  258. }
  259.  
  260.  
  261. }
  262. void setShamt()
  263. {
  264. if(name=="sll")
  265. shamt=strToInt(operand2);
  266. else shamt=0;
  267. }
  268. void setEncoding()
  269. {
  270. code="";
  271. if(format=='I')
  272. {
  273. code+=bits(opcode,6);
  274. code+=bits(RegisterToCode[operand1],5);
  275. code+=bits(RegisterToCode[operand2],5);
  276. code+=bits(address,16);
  277. }
  278. else if(format=='R')
  279. {
  280. code+=bits(opcode,6);
  281. code+=bits(RegisterToCode[operand1],5);
  282. code+=bits(RegisterToCode[operand2],5);
  283. code+=bits(RegisterToCode[destination],5);
  284. code+=bits(shamt,5);
  285. code+=bits(NameToFunct[name],6);
  286. }
  287. else if (format == 'J')
  288. {
  289. code+=bits(opcode, 6);
  290. code+=bits(jaddress,26);
  291. }
  292.  
  293. }
  294. public:
  295. Instruction()
  296. {
  297. shamt=0;
  298. opcode=0;
  299. operand1 = "";
  300. operand2 = "";
  301. destination = "";
  302. name = "";
  303. format = 0;
  304. funct = 0;
  305. address = 0;
  306. jaddress = 0;
  307. }
  308. string getCode()
  309. {
  310. if(base==BIN)return code;
  311. else return Hex(code);
  312. }
  313.  
  314. void process(string readline, map<std::string, int> map, int code_base) //helper function to call of the private functions
  315. {
  316. setBase(code_base);
  317. setData();
  318. readLine(readline);
  319. setJumpLabels(map);
  320. setName();
  321. setFormat();
  322. setOpcode();
  323. setOperands();
  324. setShamt();
  325. setEncoding();
  326. }
  327.  
  328. };
  329.  
  330. std::map<std::string, int> Instruction::JumpLabels; //that line was necessary for the map to work
  331.  
  332. int main()
  333. {
  334. //freopen("Input.txt", "r", stdin);
  335. //freopen("Output.txt","w",stdout);
  336. map<std::string,int> JumpLabels;
  337. std::vector<string> v;
  338. string line;
  339. int n =0;
  340. while(!cin.eof()) //get the jump labels then populate the instruction vector
  341. {
  342. getline(cin, line);
  343. int i=0;
  344. i = line.find(":");
  345. if(i != -1)
  346. {
  347. JumpLabels[line.substr(0,i)] = n;
  348. line=line.substr(i+1);
  349. }
  350. if(line == "" || line == " ") continue;
  351. v.push_back(line);
  352. n++;
  353. }
  354.  
  355. int i = 0; //represents instruction number
  356. while(i < int(v.size()))
  357. {
  358. Instruction X;
  359. X.process(v[i], JumpLabels, BIN); //we only pass the instruction line and then jump labels map
  360. cout<<X.getCode()<<endl;
  361. i++;
  362. }
  363. return 0;
  364. }
  365. #include <stdio.h>
  366. #include <string>
  367. #include <map>
  368. #include <iostream>
  369. #include <fstream>
  370. #include <vector>
  371. #define HEX 1
  372. #define BIN 0
  373. using namespace std;
  374. class Instruction
  375. {
  376. private:
  377. char format;
  378. string name;
  379. string line;
  380. string operand1;
  381. string operand2;
  382. string destination;
  383. int opcode;
  384. int shamt,funct,address,jaddress,base;
  385. map<string,char> NameToFormat;
  386. map<string,int> NameToOpcode;
  387. map<string,int> RegisterToCode;
  388. map<string,string> BinToHex;
  389. map<string,int> NameToFunct;
  390. static map<std::string,int> JumpLabels;
  391. string code;
  392. int strToInt(string s)
  393. {
  394. int num=0;
  395. int i=0;
  396. if(s[i]=='-')i++;
  397. while(i<int(s.size()))
  398. {
  399. num*=10;num+=s[i++]-'0';
  400. }
  401. if(s[0]=='-')num=-num;
  402. return num;
  403. }
  404. string bits(int x,int b)
  405. {
  406. string ret="";
  407. if(x>=0)
  408. {
  409. while(b--)
  410. {
  411. char c='0';
  412. if(x%2)c='1';
  413. ret=c+ret;
  414. x/=2;
  415. }
  416. }
  417. else
  418. {
  419. x=-x;
  420. while(b--)
  421. {
  422. char c='0';
  423. if(x%2)c='1';
  424. ret=c+ret;
  425. x/=2;
  426. }
  427. for(int i=0;i<int(ret.size());i++)ret[i]='1'-ret[i]+'0';
  428. for(int i=ret.size()-1;i>=0;i--)
  429. {
  430. if(ret[i]=='1')ret[i]='0';
  431. else{ret[i]='1';break;}
  432. }
  433. }
  434. return ret;
  435. }
  436. string Hex(string s)
  437. {
  438. string ret="";
  439. for(int i=0;i<int(s.size());i+=4)
  440. {
  441. string cur="";
  442. for(int j=i;j<i+4;j++)cur+=s[j];
  443. ret+=BinToHex[cur];
  444. }
  445. return ret;
  446. }
  447. bool validChar(char c)
  448. {
  449. if(c==' ' || c=='\n' || c=='\r' || c=='\t' || c==',' || c=='(' || c==')')return false;
  450. return true;
  451. }
  452. void setBase(int code_base)
  453. {
  454. base=code_base;
  455. }
  456. void setData()
  457. {
  458. NameToOpcode["add"]=0x00;
  459. NameToOpcode["addi"]=0x08;
  460. NameToOpcode["and"]=0x00;
  461. NameToOpcode["andi"]=0x0C;
  462. NameToOpcode["beq"]=0x04;
  463. NameToOpcode["jal"]=0x03;
  464. NameToOpcode["jr"]=0x00;
  465. NameToOpcode["j"]=0x02;
  466. NameToOpcode["nor"]=0x00;
  467. NameToOpcode["or"]=0x00;
  468. NameToOpcode["ori"]=0x0D;
  469. NameToOpcode["slt"]=0x00;
  470. NameToOpcode["sll"]=0x00;
  471. NameToOpcode["lw"]=0x23;
  472. NameToOpcode["sw"]=0x23;
  473. //=======================//
  474. NameToFormat["add"]='R';
  475. NameToFormat["addi"]='I';
  476. NameToFormat["and"]='R';
  477. NameToFormat["andi"]='I';
  478. NameToFormat["beq"]='I';
  479. NameToFormat["jal"]='J';
  480. NameToFormat["jr"]='R';
  481. NameToFormat["j"]='J';
  482. NameToFormat["nor"]='R';
  483. NameToFormat["or"]='R';
  484. NameToFormat["ori"]='I';
  485. NameToFormat["slt"]='R';
  486. NameToFormat["sll"]='R';
  487. NameToFormat["lw"]='I';
  488. NameToFormat["sw"]='I';
  489. //======================//
  490. RegisterToCode["$zero"]=0;
  491. RegisterToCode["$at"]=1;
  492. RegisterToCode["$v0"]=2;
  493. RegisterToCode["$v1"]=3;
  494. RegisterToCode["$a0"]=4;
  495. RegisterToCode["$a1"]=5;
  496. RegisterToCode["$a2"]=6;
  497. RegisterToCode["$a3"]=7;
  498. RegisterToCode["$t0"]=8;
  499. RegisterToCode["$t1"]=9;
  500. RegisterToCode["$t2"]=10;
  501. RegisterToCode["$t3"]=11;
  502. RegisterToCode["$t4"]=12;
  503. RegisterToCode["$t5"]=13;
  504. RegisterToCode["$t6"]=14;
  505. RegisterToCode["$t7"]=15;
  506. RegisterToCode["$s0"]=16;
  507. RegisterToCode["$s1"]=17;
  508. RegisterToCode["$s2"]=18;
  509. RegisterToCode["$s3"]=19;
  510. RegisterToCode["$s4"]=20;
  511. RegisterToCode["$s5"]=21;
  512. RegisterToCode["$s6"]=22;
  513. RegisterToCode["$s7"]=23;
  514. RegisterToCode["$t8"]=24;
  515. RegisterToCode["$t9"]=25;
  516. RegisterToCode["$k0"]=26;
  517. RegisterToCode["$k1"]=27;
  518. RegisterToCode["$gp"]=28;
  519. RegisterToCode["$sp"]=29;
  520. RegisterToCode["$s8"]=30;
  521. RegisterToCode["$fp"]=30;
  522. RegisterToCode["$ra"]=31;
  523. //=======================//
  524. NameToFunct["add"]=0x20;
  525. NameToFunct["and"]=0x24;
  526. NameToFunct["jr"]=0x08;
  527. NameToFunct["nor"]=0x27;
  528. NameToFunct["or"]=0x25;
  529. NameToFunct["slt"]=0x2A;
  530. NameToFunct["sll"]=0x00;
  531. //======================//
  532. BinToHex["0000"]="0";
  533. BinToHex["0001"]="1";
  534. BinToHex["0010"]="2";
  535. BinToHex["0011"]="3";
  536. BinToHex["0100"]="4";
  537. BinToHex["0101"]="5";
  538. BinToHex["0110"]="6";
  539. BinToHex["0111"]="7";
  540. BinToHex["1000"]="8";
  541. BinToHex["1001"]="9";
  542. BinToHex["1010"]="A";
  543. BinToHex["1011"]="B";
  544. BinToHex["1100"]="C";
  545. BinToHex["1101"]="D";
  546. BinToHex["1110"]="E";
  547. BinToHex["1111"]="F";
  548. }
  549. void readLine(string s)
  550. {
  551. line = s;
  552. //getline(cin,line);
  553. }
  554. void setJumpLabels(map<string, int> map)
  555. {
  556. JumpLabels = map;
  557. }
  558. string readItem()
  559. {
  560. string item="";
  561. int i=0;
  562. while(i<int(line.size()) && !validChar(line[i]))i++;
  563. while(i<int(line.size()) && validChar(line[i]))item+=line[i++];
  564. while(i<int(line.size()) && !validChar(line[i]))i++;
  565. line=line.substr(i);
  566. return item;
  567. }
  568. void setName()
  569. {
  570. name=readItem();
  571. }
  572.  
  573. void setFormat()
  574. {
  575. format=NameToFormat[name];
  576. }
  577.  
  578. void setOpcode()
  579. {
  580. opcode=NameToOpcode[name];
  581. }
  582. void setOperands()
  583. {
  584.  
  585. if(name=="lw" || name=="sw")
  586. {
  587. operand2=readItem();
  588. address=strToInt(readItem());
  589. operand1=readItem();
  590. }
  591. else if(name=="sll")
  592. {
  593. destination=readItem();
  594. operand2=readItem();
  595. shamt=strToInt(readItem());
  596. }
  597. else if(name == "jr")
  598. {
  599. operand1=readItem();
  600. }
  601. else if(name == "beq")
  602. {
  603. operand1=readItem();
  604. operand2=readItem();
  605. address=JumpLabels[readItem()];
  606. }
  607. else if(format=='R')
  608. {
  609. destination=readItem();
  610. operand1=readItem();
  611. operand2=readItem();
  612. }
  613. else if(format=='I')
  614. {
  615. operand2=readItem();
  616. operand1=readItem();
  617. address=strToInt(readItem());
  618. }
  619. else if(format == 'J')
  620. {
  621. jaddress = JumpLabels[readItem()];
  622. }
  623.  
  624.  
  625. }
  626. void setShamt()
  627. {
  628. if(name=="sll")
  629. shamt=strToInt(operand2);
  630. else shamt=0;
  631. }
  632. void setEncoding()
  633. {
  634. code="";
  635. if(format=='I')
  636. {
  637. code+=bits(opcode,6);
  638. code+=bits(RegisterToCode[operand1],5);
  639. code+=bits(RegisterToCode[operand2],5);
  640. code+=bits(address,16);
  641. }
  642. else if(format=='R')
  643. {
  644. code+=bits(opcode,6);
  645. code+=bits(RegisterToCode[operand1],5);
  646. code+=bits(RegisterToCode[operand2],5);
  647. code+=bits(RegisterToCode[destination],5);
  648. code+=bits(shamt,5);
  649. code+=bits(NameToFunct[name],6);
  650. }
  651. else if (format == 'J')
  652. {
  653. code+=bits(opcode, 6);
  654. code+=bits(jaddress,26);
  655. }
  656.  
  657. }
  658. public:
  659. Instruction()
  660. {
  661. shamt=0;
  662. opcode=0;
  663. operand1 = "";
  664. operand2 = "";
  665. destination = "";
  666. name = "";
  667. format = 0;
  668. funct = 0;
  669. address = 0;
  670. jaddress = 0;
  671. }
  672. string getCode()
  673. {
  674. if(base==BIN)return code;
  675. else return Hex(code);
  676. }
  677.  
  678. void process(string readline, map<std::string, int> map, int code_base) //helper function to call of the private functions
  679. {
  680. setBase(code_base);
  681. setData();
  682. readLine(readline);
  683. setJumpLabels(map);
  684. setName();
  685. setFormat();
  686. setOpcode();
  687. setOperands();
  688. setShamt();
  689. setEncoding();
  690. }
  691.  
  692. };
  693.  
  694. std::map<std::string, int> Instruction::JumpLabels; //that line was necessary for the map to work
  695.  
  696. int main()
  697. {
  698. //freopen("Input.txt", "r", stdin);
  699. //freopen("Output.txt","w",stdout);
  700. map<std::string,int> JumpLabels;
  701. std::vector<string> v;
  702. string line;
  703. int n =0;
  704. while(!cin.eof()) //get the jump labels then populate the instruction vector
  705. {
  706. getline(cin, line);
  707. int i=0;
  708. i = line.find(":");
  709. if(i != -1)
  710. {
  711. JumpLabels[line.substr(0,i)] = n;
  712. line=line.substr(i+1);
  713. }
  714. if(line == "" || line == " ") continue;
  715. v.push_back(line);
  716. n++;
  717. }
  718.  
  719. int i = 0; //represents instruction number
  720. while(i < int(v.size()))
  721. {
  722. Instruction X;
  723. X.process(v[i], JumpLabels, BIN); //we only pass the instruction line and then jump labels map
  724. cout<<X.getCode()<<endl;
  725. i++;
  726. }
  727. return 0;
  728. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement