一個簡單的虛擬機,虛擬機有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<<" ";