⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svmc.cpp~rf20e2f2.tmp

📁 简单的虚拟机代码 可以看看!! !! !!!1
💻 TMP
字号:
#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 EOF to stop entering your program.   ***" << endl;
	
	do
	{
		cout << setw(2) << setfill('0') << counter << " ?";

	}while(cin >> memory[counter++]);

	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;
}

void SVMC::nextMove()
{
	instructionRegister = memory[counter++];
	operationCode = instructionRegister/100;
	operand = instructionRegister%100;

	switch(operationCode)
	{
	case 10:
		cin >> memory[operand];
		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];
		break;
	case 31:
		accumulator -= memory[operand];
		break;
	case 32:
		accumulator *= memory[operand];
		break;
	case 33:
		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:
		break;
	default:
		break;
	}
}

void SVMC::excute()
{
	loadInstructions();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -