一个简单的虚拟机

一个简单的虚拟机,虚拟机有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
文件

// Copyright (c) LeafCore
#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\t " ;
    cin
>> buffer;
    
for  ( int  index  =   0 ; (program[index]  =  buffer) != 0 ; index ++ ) {
        cout
<< (index + 1 ) / 10 << (index + 1 ) % 10 << " \t " ;
        cin
>> buffer;
    }
    cout
<< " Good job.\n " << 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文件

// Copyright (c) LeafCore
#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


//leaf_core_virtual_machine.cpp文件

// Copyright (c) LeafCore
#include  < iostream >
#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.\n " << endl;
}

// 关闭虚拟机
void  CVirtualMachine::shut()
{
    cout
<< " Virtual machine is shutting " << endl;
    cout
<< " Complete shutting.\n " << 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.\n " << 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 << " \t " ;
        cout
<< m_memory[index] / 1000 << m_memory[index] % 1000 / 100
            
<< m_memory[index] % 100 / 10 << m_memory[index] % 10 << endl;
    }
    cout
<< " Complete printing.\n " << endl;
}

// 运行程序
void  CVirtualMachine::run_program()
{
    
int  index;
    cout
<< " Virtual machine is running the program " << endl;

    
// 取出指令
    m_instruction_counter  =   0 ;
    m_instruction_register 
=  m_memory[m_instruction_counter ++ ];
    
// 指令解码
    m_opcode  =  m_instruction_register / 100 ;
    m_address 
=  m_instruction_register % 100 ;
    
for  (index  =   0 ; index < 100   &&  m_opcode != const_halt; index ++ ) {
        cout
<< " opcode\taddress " << endl;
        cout
<< m_opcode / 10 << m_opcode % 10 << " \t " << m_address / 10 << m_address % 10 << endl;

        
// 执行指令
         switch  (m_opcode) {
        
case  const_input:
            
int  buffer;
            cin
>> buffer;
            m_memory[m_address] 
=  buffer;
            
break ;
        
case  const_print:
            cout
<< m_memory[m_address] << endl;
            
break ;
        
case  const_load:
            m_accumulator 
=  m_memory[m_address];
            
break ;
        
case  const_store:
            m_memory[m_address] 
=  m_accumulator;
            
break ;
        
case  const_plus:
            m_accumulator 
+=  m_memory[m_address];
            
break ;
        
case  const_minus:
            m_accumulator 
-=  m_memory[m_address];
            
break ;
        
case  const_multiply:
            m_accumulator 
*=  m_memory[m_address];
            
break ;
        
case  const_divide:
            m_accumulator 
/=  m_memory[m_address];
            
break ;
        
case  const_branch:
            m_instruction_counter 
=  m_address;
            
break ;
        
case  const_branch_below:
            
if  (m_accumulator < 0 ) {
                m_instruction_counter 
=  m_address;
            }
            
break ;
        
case  const_branch_zero:
            
if  (m_accumulator == 0 ) {
                m_instruction_counter 
=  m_address;
            }
            
break ;
        
default :
            
break ;
        }
        cout
<< endl;

        
// 取出指令
        m_instruction_register  =  m_memory[m_instruction_counter ++ ];
        
// 指令解码
        m_opcode  =  m_instruction_register / 100 ;
        m_address 
=  m_instruction_register % 100 ;
    }
    cout
<< " Complete running.\n " << endl;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值