c语言中的一个简单的虚拟机
一个简单的虚拟机,虚拟机有100个字的内存,几个寄存器——指令计数器、指令寄存器、操作码寄存器、内存地址寄存器、累加寄存器。可以——输入01、输出02、加载数据到寄存器03、存放数据到内存04、简单的加05减06乘07除08运算、转移09、小于零转移10、等于零转移11、终止12。程序输入完,输入0000结束。
打印两个数中的较大数
0120 //输入一个数到内存20
0121 //输入一个数到内存21
0320 //加载内存20的数据到累加器
0621 //累加器数据减去内存21 的数据
1007 //如果小于零转移到内存07
0220 //输出内存20的数据
1200 //终止
0221 //输出内存21的数据
1200 //终止
0000 //输入结束
运行
Virtual machine is booting...
Complete booting.
Please input your program.
00 0120
01 0121
02 0320
03 0621
04 1007
05 0220
06 1200
07 0221
08 1200
09 0000
Good job.
Virtual machine is loading the program...
Complete loading.
Virtual machine is printing the program...
00 0120
01 0121
02 0320
03 0621
04 1007
05 0220
06 1200
07 0221
08 1200
Complete printing.
Virtual machine is running the program...
opcode address
01 20
100
opcode address
01 21
200
opcode address
03 20
opcode address
06 21
opcode address
10 07
opcode address
02 21
200
Complete running
Virtual machine is shutting...
Complete shutting.
//leaf_core_test.cpp文件
#include <iostream>
#include "leaf_core_virtual_machine.hpp"
using namespace std;
int main(int argc, char *argv[])
{
CVirtualMachine virtual_machine; //创建虚拟机对象
int program[100]; //存放程序
int buffer;
//启动虚拟机
virtual_machine.boot();
//输入程序
cout<<"Please enter your program"<<endl;
cout<<"00 ";
cin>>buffer;
for (int index = 0; (program[index] = buffer)!=0; index++) {
cout<<(index+1)/10<<(index+1)%10<<" ";
cin>>buffer;
}
cout<<"Good job. "<<endl;
//加载程序到虚拟机内存
virtual_machine.load_program(program);
//打印程序
virtual_machine.print_program();
//运行程序
virtual_machine.run_program();
//关闭虚拟机
virtual_machine.shut();
char ch;
cout<<"Window closing"<<endl;
cin>>ch;
return 0;
}
//leaf_core_virtual_machine.hpp文件
#ifndef __leaf_core_virtual_machine_hpp__
#define __leaf_core_virtual_machine_hpp__
class CVirtualMachine {
private:
int m_memory[100]; //内存
int m_instruction_counter; //指令计数器
int m_instruction_register; //指令寄存器
int m_opcode; //操作码
int m_address; //内存地址
int m_accumulator; //寄存器
public:
//Virtual Machine操作码
static const int const_input = 1; //存放用户输入数据到内存
static const int const_print = 2; //输出内存数据到屏幕
static const int const_load = 3; //加载内存数据到寄存器
static const int const_store = 4; //存放寄存器数据到内存
static const int const_plus = 5; //寄存器数据加上内存数据
static const int const_minus = 6; //寄存器数据减去内存数据
static const int const_multiply = 7; //寄存器数据乘以内存数据
static const int const_divide = 8; //寄存器数据除以内存数据
static const int const_branch = 9; //转移
static const int const_branch_below = 10; //寄存器数据小于零转移
static const int const_branch_zero = 11; //寄存器数据等于零转移
static const int const_halt = 12; //终止
public:
CVirtualMachine();
~CVirtualMachine();
void boot(); //启动虚拟机
void shut(); //关闭虚拟机
void load_program(int *program); //加载程序到虚拟机内存
void print_program(); //打印程序
void run_program(); //运行程序
};
#endif
#include <iostream>
//leaf_core_virtual_machine.cpp文件
#include "leaf_core_virtual_machine.hpp"
using namespace std;
//初始化虚拟机
CVirtualMachine::CVirtualMachine()
{
m_instruction_counter = 0;
m_instruction_register = 0;
m_opcode = 0;
m_address = 0;
}
CVirtualMachine::~CVirtualMachine()
{
}
//启动虚拟机
void CVirtualMachine::boot()
{
cout<<"Virtual machine is booting..."<<endl;
cout<<"Complete booting. "<<endl;
}
//关闭虚拟机
void CVirtualMachine::shut()
{
cout<<"Virtual machine is shutting..."<<endl;
cout<<"Complete shutting. "<<endl;
}
//加载程序到虚拟机内存
void CVirtualMachine::load_program(int *program)
{
int index;
cout<<"Virtual machine is loading the program..."<<endl;
for (index = 0; index<100 && program[index]!=0; index++) {
m_memory[index] = program[index];
}
m_memory[index] = program[index];
cout<<"Complete loading. "<<endl;
}
//打印程序
void CVirtualMachine::print_program()
{
int index;
cout<<"Virtual machine is printing the program..."<<endl;
for (index = 0; index<100 && m_memory[index]!=0; index++) {
cout<<index/10<<index%10<<" ";
补充:软件开发 , C语言 ,