Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 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,currentPC;
  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"]=0x2B;
  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()] - (currentPC / 4) - 1;
  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 setEncoding()
  263. {
  264. code="";
  265. if(format=='I')
  266. {
  267. code+=bits(opcode,6);
  268. code+=bits(RegisterToCode[operand1],5);
  269. code+=bits(RegisterToCode[operand2],5);
  270. code+=bits(address,16);
  271. }
  272. else if(format=='R')
  273. {
  274. code+=bits(opcode,6);
  275. code+=bits(RegisterToCode[operand1],5);
  276. code+=bits(RegisterToCode[operand2],5);
  277. code+=bits(RegisterToCode[destination],5);
  278. code+=bits(shamt,5);
  279. code+=bits(NameToFunct[name],6);
  280. }
  281. else if (format == 'J')
  282. {
  283. code+=bits(opcode, 6);
  284. code+=bits(jaddress,26);
  285. }
  286.  
  287. }
  288. void setCurrentPC(int x)
  289. {
  290. currentPC = x;
  291. }
  292. public:
  293. Instruction()
  294. {
  295. shamt=0;
  296. opcode=0;
  297. operand1 = "";
  298. operand2 = "";
  299. destination = "";
  300. name = "";
  301. format = 0;
  302. funct = 0;
  303. address = 0;
  304. jaddress = 0;
  305. }
  306. string getCode()
  307. {
  308. if(base==BIN)return code;
  309. else return Hex(code);
  310. }
  311.  
  312. void process(string readline, map<std::string, int> map, int code_base, int pc) //helper function to call of the private functions
  313. {
  314. setBase(code_base);
  315. setCurrentPC(pc);
  316. setData();
  317. readLine(readline);
  318. setJumpLabels(map);
  319. setName();
  320. setFormat();
  321. setOpcode();
  322. setOperands();
  323. setEncoding();
  324. }
  325.  
  326. };
  327.  
  328. std::map<std::string, int> Instruction::JumpLabels; //that line was necessary for the map to work
  329.  
  330. int main()
  331. {
  332. freopen("Input.txt", "r", stdin);
  333. freopen("Output.list","w",stdout);
  334. map<std::string,int> JumpLabels;
  335. std::vector<string> v;
  336. string line;
  337. int n =0;
  338. while(!cin.eof()) //get the jump labels then populate the instruction vector
  339. {
  340. getline(cin, line);
  341. int i=0;
  342. i = line.find(":");
  343. if(i != -1)
  344. {
  345. JumpLabels[line.substr(0,i)] = n;
  346. line=line.substr(i+1);
  347. }
  348. if(line == "" || line == " ") continue;
  349. v.push_back(line);
  350. n++;
  351. }
  352.  
  353. int i = 0; //represents instruction number
  354. while(i < int(v.size()))
  355. {
  356. Instruction X;
  357. X.process(v[i], JumpLabels, HEX, i*4); //we only pass the instruction line and then jump labels map
  358. cout<<X.getCode()<<endl;
  359. i++;
  360. }
  361. return 0;
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement