// Simpletron.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "Integer.h"
// 10:READ
// 11:WRITE
// 12:WRITE NEWLINE
// 20:LOAD
// 21:STORE
// 30:ADD
// 31:SUBTRACT
// 32:MULTUPLY
// 33:DIVIDE
// 34:MODULUS
// 40:BRANCH
// 41:BRANCHNEG
// 42:BRANCHZERO
// 43:HALT
// 44:DEBUG (00=no debug,01=all debug,02=register debug,03=memory debug)
using namespace std;
void Tokenize(const string &str, vector<string> &tokens, const string &delimiters);
int main(int argc, char* argv[]) {
int accumulator = 0;
int counter = 0;
int choice = 0;
int i = 0, j = 0;
int temp = 0;
float f_temp;
int debug = 0;
vector<string> toks;
string code = "";
int memory[1000];
for (i=0;i<1000;i++) {
memory[i] = 00000;
}
int operand, operationcode;
ifstream infile;
infile.open("smfile.txt");
for (i=0;i<1000;i++) {
if (!infile.eof()) {
getline(infile,code);
Tokenize(code,toks," ");
//cout<< code << "\n";
if (toks.size() == 2) {
memory[counter] = atoi(toks[1].c_str());
} else if (toks.size() == 3) {
memory[counter] = atoi(toks[2].c_str());
} else if (toks.size() == 1) {
memory[counter] = atoi(toks[0].c_str());
}
//cout<< toks.size() << " " << memory[counter] << "\n";
temp = toks.size();
for (j=0;j<temp;j++) {
toks.pop_back();
}
counter++;
} else {
break;
}
}
/*cout<< counter << "\n";
cout<<"MEMORY:\n";
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<" 0";
for (i=0;i<1000;i++) {
if (j == 10) {
cout<<"\n" << i+1;
j = 0;
}
if (memory[i] < 10) {
cout<<" 000" << memory[i];
} else if (memory[i] < 100) {
cout<<" 00" << memory[i];
} else if (memory[i] < 1000) {
cout<<" 0" << memory[i];
} else {
cout<<" " << memory[i];
}
j++;
}
for (i=0;i<1000;i++) {
if (counter < 10) {
cout<<"0" << counter << "? ";
cin>> choice;
//cout<<"\n";
if (choice == 99999) {
break;
} else {
memory[counter] = choice;
counter++;
}
} else {
cout<< counter << "? ";
cin>> choice;
//cout<<"\n";
if (choice == 99999) {
break;
} else {
memory[counter] = choice;
counter++;
}
}
}*/
if (counter == 0) {
cout<<"*** ERROR: PROGRAM COULD NOT BE ENTERED ***\n";
goto exit;
}
cout<<"*** Program entering ends ***\n";
cout<<"*** Program execution begins ***\n";
counter = -1;
i = 0;
while (i == 0) {
counter++;
//cout<<"\n" << counter << "\n\n";
f_temp = memory[counter]/1000;
operationcode = f_temp;
operand = memory[counter];
operand -= operationcode*1000;
//cout<< operationcode << operand << "\n";
if (operationcode == 10) {
cout<<"? ";
cin>> choice;
memory[operand] = choice;
} else if (operationcode == 11) {
cout<< memory[operand] << "\n";
} else if (operationcode == 12) {
cout<<"\n";
} else if (operationcode == 20) {
accumulator = memory[operand];
} else if (operationcode == 21) {
memory[operand] = accumulator;
} else if (operationcode == 30) {
accumulator += memory[operand];
} else if (operationcode == 31) {
accumulator -= memory[operand];
} else if (operationcode == 32) {
accumulator *= memory[operand];
} else if (operationcode == 33) {
accumulator /= memory[operand];
} else if (operationcode == 34) {
accumulator %= memory[operand];
} else if (operationcode == 40) {
//cout<<"\n0\n\n";
counter = operand-1;
} else if (operationcode == 41) {
//cout<<"\n1\n\n";
if (accumulator < 0) {
//cout<<"oper: " << operand << "\n";
counter = operand-1;
}
} else if (operationcode == 42) {
//cout<<"\n2\n\n";
if (accumulator == 0) {
counter = operand-1;
}
} else if (operationcode == 43) {
break;
cout<<"*** Execution terminated ***\n";
} else if (operationcode == 44) {
debug = operand;
}
if (debug == 1) {
cout<<"REGISTERS:\n";
cout<<"accumulator: " << accumulator << "\n";
cout<<"counter: " << counter << "\n\n";
cout<<"MEMORY:\n";
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<" 0";
for (i=0;i<1000;i++) {
if (j == 10) {
cout<<"\n" << i;
j = 0;
}
if (memory[i] < 10) {
cout<<" 000" << memory[i];
} else if (memory[i] < 100) {
cout<<" 00" << memory[i];
} else if (memory[i] < 1000) {
cout<<" 0" << memory[i];
} else {
cout<<" " << memory[i];
}
j++;
}
} else if (debug == 1) {
cout<<"REGISTERS:\n";
cout<<"accumulator: " << accumulator << "\n";
cout<<"counter: " << counter << "\n\n";
} else if (debug == 2) {
cout<<"MEMORY:\n";
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<" 0";
for (i=0;i<1000;i++) {
if (j == 10) {
cout<<"\n" << i;
j = 0;
}
if (memory[i] < 10) {
cout<<" 000" << memory[i];
} else if (memory[i] < 100) {
cout<<" 00" << memory[i];
} else if (memory[i] < 1000) {
cout<<" 0" << memory[i];
} else {
cout<<" " << memory[i];
}
j++;
}
}
if (counter >= 1000) {
cout<<"*** ERROR: RAN OUT OF MEMORY. ***\n";
break;
}
}
system("pause");
cout<<"REGISTERS:\n";
cout<<"accumulator: " << accumulator << "\n";
cout<<"counter: " << counter << "\n\n";
cout<<"MEMORY:\n";
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<" 0";
j = 0;
for (i=0;i<1000;i++) {
if (j == 10) {
cout<<"\n" << i;
j = 0;
}
if (memory[i] == 0) {
cout<<" 00000";
} else if (memory[i] < 10) {
cout<<" 0000" << memory[i];
} else if (memory[i] == 10) {
cout<<" 000" << memory[i];
} else if (memory[i] < 100) {
cout<<" 000" << memory[i];
} else if (memory[i] == 100) {
cout<<" 00" << memory[i];
} else if (memory[i] <= 1000) {
cout<<" 0" << memory[i];
} else {
cout<<" " << memory[i];
}
j++;
}
cout<<"\n";
system("pause");
exit:;
return 0;
}
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters) {
string::size_type lastPos = str.find_first_not_of(delimiters,0);
string::size_type pos = str.find_first_of(delimiters,lastPos);
while (string::npos != pos || string::npos != lastPos) {
tokens.push_back(str.substr(lastPos,pos-lastPos));
lastPos = str.find_first_not_of(delimiters,pos);
pos = str.find_first_of(delimiters,lastPos);
}
}