Advertisement
Guest User

Untitled

a guest
May 1st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #include "Memory.h"
  2. #include <fstream>
  3. #include <string>
  4. #include <iostream>
  5. #include <regex>
  6.  
  7. using std::ifstream;
  8. using std::string;
  9. using std::cout;
  10. using std::cin;
  11. using std::endl;
  12. using std::regex;
  13.  
  14. int MQ(0),
  15. AC(0);
  16. bool error(false);
  17. Memory memory;
  18.  
  19. void execute(int command, int address)
  20. {
  21. string tmp;
  22. if (command == 0)
  23. {
  24. AC = MQ;
  25. }
  26. else if (command == 1)
  27. {
  28. tmp = memory.get(address);
  29. MQ = atoi(tmp.c_str());
  30. }
  31. else if (command == 2)
  32. {
  33. tmp = memory.get(address);
  34. AC = atoi(tmp.c_str());
  35. }
  36. else if (command == 3)
  37. {
  38. tmp = std::to_string(AC);
  39. memory.set(address, tmp);
  40. }
  41. else if (command == 4)
  42. {
  43. tmp = memory.get(address);
  44. AC = -atoi(tmp.c_str());
  45. }
  46. else if (command == 5)
  47. {
  48. tmp = memory.get(address);
  49. AC = abs(atoi(tmp.c_str()));
  50. }
  51. else if (command == 6)
  52. {
  53. tmp = memory.get(address);
  54. AC = -abs(atoi(tmp.c_str()));
  55. }
  56. }
  57.  
  58. bool decode(string data)
  59. {
  60. int address;
  61. string tmp;
  62. std::match_results<std::string::const_iterator> result;
  63.  
  64. regex p1("(L|l)oad MQ");
  65. regex p2("(L|l)oad MQ,M\\((\\d{1,3})\\)");
  66. regex p3("(L|l)oad M\\((\\d{1,3})\\)");
  67. regex p4("(S|s)tor M\\((\\d{1,3})\\)");
  68. regex p5("(L|l)oad -M\\((\\d{1,3})\\)");
  69. regex p6("(L|l)oad \\|M\\((\\d{1,3})\\)\\|");
  70. regex p7("(L|l)oad -\\|M\\((\\d{1,3})\\)\\|");
  71. regex p8("halt");
  72. regex p9("begin");
  73. regex p10("\\.\.*");
  74. regex p11("([a-zA-Z][a-zA-Z0-9]|\\d+)");
  75. regex p12("nop");
  76.  
  77.  
  78. if (std::regex_match(data, result, p2))
  79. {
  80. cout << endl << 1 << endl;
  81. tmp = result[2];
  82. address = atoi(tmp.c_str());
  83. execute(1, address);
  84. return false;
  85. }
  86. else if (std::regex_match(data, result, p1))
  87. {
  88. cout << endl << 0 << endl;
  89. address = -1;
  90. execute(0, address);
  91. return false;
  92. }
  93. else if (std::regex_match(data, result, p3))
  94. {
  95. cout << endl << 2 << endl;
  96. tmp = result[2];
  97. address = atoi(tmp.c_str());
  98. execute(2, address);
  99. return false;
  100. }
  101. else if (std::regex_match(data, result, p4))
  102. {
  103. cout << endl << 3 << endl;
  104. tmp = result[2];
  105. address = atoi(tmp.c_str());
  106. execute(3, address);
  107. return false;
  108. }
  109. else if (std::regex_match(data, result, p5))
  110. {
  111. cout << endl << 4 << endl;
  112. tmp = result[2];
  113. address = atoi(tmp.c_str());
  114. execute(4, address);
  115. return false;
  116. }
  117. else if (std::regex_match(data, result, p6))
  118. {
  119. cout << endl << 5 << endl;
  120. tmp = result[2];
  121. address = atoi(tmp.c_str());
  122. execute(5, address);
  123. return false;
  124. }
  125. else if (std::regex_match(data, result, p7))
  126. {
  127. cout << endl << 6 << endl;
  128. tmp = result[2];
  129. address = atoi(tmp.c_str());
  130. execute(6, address);
  131. return false;
  132. }
  133. else if (std::regex_match(data, result, p8))
  134. {
  135. cout << endl << 7 << endl;
  136. return true;
  137. }
  138. else if (std::regex_match(data, result, p9))
  139. {
  140. cout << endl << 8 << endl;
  141. return false;
  142. }
  143. else if (std::regex_match(data, result, p10))
  144. {
  145. cout << endl << 9 << endl;
  146. return false;
  147. }
  148. else if (std::regex_match(data, result, p11))
  149. {
  150. cout << endl << 10 << endl;
  151. return false;
  152. }
  153. else if (std::regex_match(data, result, p12))
  154. {
  155. cout << endl << 11 << endl;
  156. return false;
  157. }
  158. else
  159. {
  160. cout << "Error: Data could not be processed." << endl;
  161. error = true;
  162. return false;
  163. }
  164. }
  165.  
  166. int main()
  167. {
  168. string filename,
  169. line,
  170. MBR,
  171. IR;
  172. int PC,
  173. MAR;
  174. bool halt(false);
  175.  
  176. cout << "Input the name of a file of assembly code: ";
  177. cin >> filename;
  178. ifstream infile(filename);
  179. while (infile.fail())
  180. {
  181. infile.clear();
  182. cout << endl << "File was not found.\nInput the name of a file of assembly code: ";
  183. cin >> filename;
  184. infile.open(filename);
  185. }
  186.  
  187. cout << endl << "Checking code" << endl;
  188. regex pattern("([0-9]+)\\s+(\.+)");
  189. std::match_results<std::string::const_iterator> result;
  190. int address;
  191. string linenumber;
  192. string data;
  193. getline(infile, line);
  194. if (std::regex_search(line, result, pattern))
  195. {
  196. linenumber = result[1];
  197. address = atoi(linenumber.c_str());
  198. data = result[2];
  199. if (data == "begin")
  200. {
  201. PC = address;
  202. }
  203. }
  204. while (!infile.fail())
  205. {
  206. memory.set(address, data);
  207. cout << address << " " << memory.get(address) << endl;
  208. getline(infile, line);
  209. if (std::regex_search(line, result, pattern))
  210. {
  211. linenumber = result[1];
  212. address = atoi(linenumber.c_str());
  213. data = result[2];
  214. if (data == "begin")
  215. {
  216. PC = address;
  217. }
  218. }
  219. }
  220.  
  221. cout << endl << "Execution of program" << endl << endl;
  222. while (!halt)
  223. {
  224. MAR = PC;
  225. MBR = memory.get(MAR);
  226. IR = MBR;
  227. cout << "PC = " << PC << " IR = " << IR << endl;
  228. PC++;
  229. halt = decode(IR);
  230. if (error)
  231. {
  232. return 0;
  233. }
  234. cout << "PC = " << PC << " AC = " << AC << " MQ = " << MQ << endl;
  235. cout << "_________________________________________________________" << endl;
  236. }
  237.  
  238. cout << endl << "memory" << endl << endl;
  239. int count = 0;
  240. while (count < (PC))
  241. {
  242. cout << count << " " << memory.get(count) << endl;
  243. count++;
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement