Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Memory.h"
- #include <fstream>
- #include <string>
- #include <iostream>
- #include <regex>
- using std::ifstream;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
- using std::regex;
- int MQ(0),
- AC(0);
- bool error(false);
- Memory memory;
- void execute(int command, int address)
- {
- string tmp;
- if (command == 0)
- {
- AC = MQ;
- }
- else if (command == 1)
- {
- tmp = memory.get(address);
- MQ = atoi(tmp.c_str());
- }
- else if (command == 2)
- {
- tmp = memory.get(address);
- AC = atoi(tmp.c_str());
- }
- else if (command == 3)
- {
- tmp = std::to_string(AC);
- memory.set(address, tmp);
- }
- else if (command == 4)
- {
- tmp = memory.get(address);
- AC = -atoi(tmp.c_str());
- }
- else if (command == 5)
- {
- tmp = memory.get(address);
- AC = abs(atoi(tmp.c_str()));
- }
- else if (command == 6)
- {
- tmp = memory.get(address);
- AC = -abs(atoi(tmp.c_str()));
- }
- }
- bool decode(string data)
- {
- int address;
- string tmp;
- std::match_results<std::string::const_iterator> result;
- regex p1("(L|l)oad MQ");
- regex p2("(L|l)oad MQ,M\\((\\d{1,3})\\)");
- regex p3("(L|l)oad M\\((\\d{1,3})\\)");
- regex p4("(S|s)tor M\\((\\d{1,3})\\)");
- regex p5("(L|l)oad -M\\((\\d{1,3})\\)");
- regex p6("(L|l)oad \\|M\\((\\d{1,3})\\)\\|");
- regex p7("(L|l)oad -\\|M\\((\\d{1,3})\\)\\|");
- regex p8("halt");
- regex p9("begin");
- regex p10("\\.\.*");
- regex p11("([a-zA-Z][a-zA-Z0-9]|\\d+)");
- regex p12("nop");
- if (std::regex_match(data, result, p2))
- {
- cout << endl << 1 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(1, address);
- return false;
- }
- else if (std::regex_match(data, result, p1))
- {
- cout << endl << 0 << endl;
- address = -1;
- execute(0, address);
- return false;
- }
- else if (std::regex_match(data, result, p3))
- {
- cout << endl << 2 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(2, address);
- return false;
- }
- else if (std::regex_match(data, result, p4))
- {
- cout << endl << 3 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(3, address);
- return false;
- }
- else if (std::regex_match(data, result, p5))
- {
- cout << endl << 4 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(4, address);
- return false;
- }
- else if (std::regex_match(data, result, p6))
- {
- cout << endl << 5 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(5, address);
- return false;
- }
- else if (std::regex_match(data, result, p7))
- {
- cout << endl << 6 << endl;
- tmp = result[2];
- address = atoi(tmp.c_str());
- execute(6, address);
- return false;
- }
- else if (std::regex_match(data, result, p8))
- {
- cout << endl << 7 << endl;
- return true;
- }
- else if (std::regex_match(data, result, p9))
- {
- cout << endl << 8 << endl;
- return false;
- }
- else if (std::regex_match(data, result, p10))
- {
- cout << endl << 9 << endl;
- return false;
- }
- else if (std::regex_match(data, result, p11))
- {
- cout << endl << 10 << endl;
- return false;
- }
- else if (std::regex_match(data, result, p12))
- {
- cout << endl << 11 << endl;
- return false;
- }
- else
- {
- cout << "Error: Data could not be processed." << endl;
- error = true;
- return false;
- }
- }
- int main()
- {
- string filename,
- line,
- MBR,
- IR;
- int PC,
- MAR;
- bool halt(false);
- cout << "Input the name of a file of assembly code: ";
- cin >> filename;
- ifstream infile(filename);
- while (infile.fail())
- {
- infile.clear();
- cout << endl << "File was not found.\nInput the name of a file of assembly code: ";
- cin >> filename;
- infile.open(filename);
- }
- cout << endl << "Checking code" << endl;
- regex pattern("([0-9]+)\\s+(\.+)");
- std::match_results<std::string::const_iterator> result;
- int address;
- string linenumber;
- string data;
- getline(infile, line);
- if (std::regex_search(line, result, pattern))
- {
- linenumber = result[1];
- address = atoi(linenumber.c_str());
- data = result[2];
- if (data == "begin")
- {
- PC = address;
- }
- }
- while (!infile.fail())
- {
- memory.set(address, data);
- cout << address << " " << memory.get(address) << endl;
- getline(infile, line);
- if (std::regex_search(line, result, pattern))
- {
- linenumber = result[1];
- address = atoi(linenumber.c_str());
- data = result[2];
- if (data == "begin")
- {
- PC = address;
- }
- }
- }
- cout << endl << "Execution of program" << endl << endl;
- while (!halt)
- {
- MAR = PC;
- MBR = memory.get(MAR);
- IR = MBR;
- cout << "PC = " << PC << " IR = " << IR << endl;
- PC++;
- halt = decode(IR);
- if (error)
- {
- return 0;
- }
- cout << "PC = " << PC << " AC = " << AC << " MQ = " << MQ << endl;
- cout << "_________________________________________________________" << endl;
- }
- cout << endl << "memory" << endl << endl;
- int count = 0;
- while (count < (PC))
- {
- cout << count << " " << memory.get(count) << endl;
- count++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement