📄 svmc.cpp
字号:
#include "stdafx.h"
using namespace std;
SVMC::SVMC()
:accumulator(0),
counter(0),
instructionRegister(0),
operationCode(0),
operand(0)
{
for(int i = 0;i < 100;i++)
{
memory[i] = 0;
}
cout << "*** Welcome to Simpletron! ***" << endl;
cout << endl << "Simple Virtual Mechine created!" << endl;
}
SVMC::SVMC(const SVMC & value)
{
if(this == &value)
{
return;
}
for(int i = 0;i < 100;i++)
{
memory[i] = value.memory[i];
}
accumulator = value.accumulator;
counter = value.counter;
instructionRegister = value.instructionRegister;
operand = value.operand;
operationCode = value.operationCode;
cout << "*** Welcome to Simpletron! ***" << endl;
cout << endl << "*** Simple Virtual Mechine created! ***" << endl;
}
void SVMC::init()
{
for(int i = 0;i < 100;i++)
{
memory[i] = 0;
}
accumulator = 0;
counter = 0;
instructionRegister = 0;
operand = 0;
operationCode = 0;
cout << endl << "*** Simple Virtual Mechine initiated! ***" << endl;
}
void SVMC::loadInstructions()
{
init();
cout << "*** Please enter your program one instruction ***" << endl;
cout << "*** (or data word) at a time. I will type the ***" << endl;
cout << "*** location number and a question mark (?). ***" << endl;
cout << "*** You then type the word for that location. ***" << endl;
cout << "*** Type -1 to stop entering your program. ***" << endl;
do
{
int tmp = 0;
do
{
cout << setw(2) << setfill('0') << counter << " ?";
cin >> tmp;
}while(tmp < -1 || tmp > 9999);
memory[counter++] = tmp;
}while(memory[counter -1] != -1);
counter = 0;
cout << "*** Instructions loaded successfully ! ***" << endl;
}
void SVMC::print() const
{
cout << "*** States ***" << endl;
cout << "REGISTERS:" << endl;
cout << setw(20) << left << setfill(' ') << "Accumulator" << setw(5) << internal << showpos << setfill('0') << accumulator << endl;
cout << setw(20) << left << setfill(' ') << "Counter" << " " << setw(2) << noshowpos << internal << setfill('0') << counter << endl;
cout << setw(20) << left << setfill(' ') << "InstructionRegister" << setw(5) << internal << showpos << setfill('0') << instructionRegister << endl;
cout << setw(20) << left << setfill(' ') << "OperationCode" << " " << setw(2) << noshowpos << internal << setfill('0') << operationCode << endl;
cout << setw(20) << left << setfill(' ') << "Operand" << " " << setw(2) << noshowpos << internal << setfill('0') << operand << endl;
cout << endl << "MEMORY:" << endl << " ";
for(int i = 0;i < 10;i++)
{
cout << noshowpos << setfill(' ') << setw(5) << i;
}
for(int i = 0;i < 100;i++)
{
if(i % 10 == 0)
{
cout << setw(2) << noshowpos << endl << i;
}
cout << setw(5) << setfill('0') << showpos << memory[i];
}
cout << endl << "*** finished ***" << endl;
}
bool SVMC::nextMove()
{
instructionRegister = memory[counter++];
operationCode = instructionRegister/100;
operand = instructionRegister%100;
try
{
int tmp = 0;
switch(operationCode)
{
case 10:
do
{
cout << "*** Please enter a data between -9999 and +9999 ***" << endl;
cin >> tmp;
}while(tmp < -9999 || tmp > 9999);
memory[operand] = tmp;
cin.ignore();
break;
case 11:
cout << memory[operand];
break;
case 20:
accumulator = memory[operand];
break;
case 21:
memory[operand] = accumulator;
break;
case 30:
accumulator += memory[operand];
if(accumulator > 9999 || accumulator < -9999)
{
cout << "*** ERROR! *** Accumulator Register out of bound at instruction #" << counter << "! ***" << endl;
print();
return true;
}
break;
case 31:
accumulator -= memory[operand];
if(accumulator > 9999 || accumulator < -9999)
{
cout << "*** ERROR! *** Accumulator Register out of bound at instruction #" << counter << "! ***" << endl;
print();
return true;
}
break;
case 32:
accumulator *= memory[operand];
if(accumulator > 9999 || accumulator < -9999)
{
cout << "*** ERROR! *** Accumulator Register out of bound at instruction #" << counter << "! ***" << endl;
print();
return true;
}
break;
case 33:
if(memory[operand] == 0)
{
cout << "*** Attempt to divide by zero ***" << endl;
cout << "*** Simpletron execution abnormally terminated ***" << endl;
print();
return true;
}
accumulator /= memory[operand];
break;
case 40:
counter = operand;
break;
case 41:
if(accumulator < 0)
{
counter = operand;
}
break;
case 42:
if(accumulator == 0)
{
counter = operand;
}
break;
case 43:
return true;
default:
break;
}
}
catch(...)
{
cout << "*** ERROR! *** Memory & Instruction ERROR at #" << counter << " ! *** ERROR! ***" << endl;
print();
return true;
}
return false;
}
void SVMC::excute()
{
loadInstructions();
while(!nextMove())
{
//print();
cout << endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -