日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个简单的虚拟机

發布時間:2025/6/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个简单的虚拟机 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的一个简单的虚拟机的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。