A Simple Virtual Machine in C Language

Source: Internet
Author: User

A Simple Virtual Machine with 100 characters of memory, several registers-instruction counters, instruction registers, operation code registers, memory address registers, and accumulation registers. You can -- input 01, output 02, load data to register 03, store data to memory 04, simply add 05 minus 06 by 07 division 08 operation, transfer 09, less than zero transfer 10, it is equal to zero transfer 11 and termination 12. After the program is entered, enter 0000.

 

Print the large numbers of the two numbers.

0120 // input one count to memory 20

0121 // input one count to memory 21

0320 // load data of memory 20 to the Accumulators

0621 // accumulators data minus memory 21 Data

1007 // if it is less than zero, transfer it to memory 07

0220 // output data of memory 20

1200 // termination

0221 // output data of memory 21

1200 // termination

0000 // input ended

 

Run

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 File

# Include <iostream>

# Include "leaf_core_virtual_machine.hpp"

 

Using namespace std;

 

Int main (int argc, char * argv [])

{

CVirtualMachine virtual_machine; // create a virtual machine object

Int program [100]; // stores the program

Int buffer;

 

// Start the VM

Virtual_machine.boot ();

 

// Input Program

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;

 

// Load the program to the VM memory

Virtual_machine.load_program (program );

// Print the program

Virtual_machine.print_program ();

// Run the program

Virtual_machine.run_program ();

 

// Shut down the Virtual Machine

Virtual_machine.shut ();

 

Char ch;

Cout <"Window closing" <endl;

Cin> ch;

Return 0;

}

 

// Leaf_core_virtual_machine.hpp File

# Ifndef _ leaf_core_virtual_machine_hpp __

# Define _ leaf_core_virtual_machine_hpp __

 

Class CVirtualMachine {

Private:

Int m_memory [100]; // memory

Int m_instruction_counter; // command counter

Int m_instruction_register; // instruction register

Int m_opcode; // operation code

Int m_address; // memory address

Int m_accumulator; // register

Public:

// Virtual Machine operation code

Static const int const_input = 1; // store user input data to the memory

Static const int const_print = 2; // output memory data to the screen

Static const int const_load = 3; // load memory data to the Register

Static const int const_store = 4; // store register data to the memory

Static const int const_plus = 5; // Add memory data to register data

Static const int const_minus = 6; // register data minus memory data

Static const int const_multiply = 7; // multiply register data by memory data

Static const int const_divide = 8; // register data divided by memory data

Static const int const_branch = 9; // Transfer

Static const int const_branch_below = 10; // register data smaller than zero transfer

Static const int const_branch_zero = 11; // register data is equal to zero transfer

Static const int const_halt = 12; // terminate

Public:

CVirtualMachine ();

~ CVirtualMachine ();

 

Void boot (); // start the VM

Void shut (); // shut down the VM

Void load_program (int * program); // load the program to the VM memory

Void print_program (); // print the program

Void run_program (); // run the program

};

 

# Endif

# Include <iostream>

 

// Leaf_core_virtual_machine.cpp File

# Include "leaf_core_virtual_machine.hpp"

 

Using namespace std;

 

// Initialize the VM

CVirtualMachine: CVirtualMachine ()

{

M_instruction_counter = 0;

M_instruction_register = 0;

M_opcode = 0;

M_address = 0;

}

 

CVirtualMachine ::~ CVirtualMachine ()

{

}

 

// Start the VM

Void CVirtualMachine: boot ()

{

Cout <"Virtual machine is booting..." <endl;

Cout <"Complete booting." <endl;

}

 

// Shut down the Virtual Machine

Void CVirtualMachine: shut ()

{

Cout <"Virtual machine is shutting..." <endl;

Cout <"Complete shutting." <endl;

}

 

// Load the program to the VM memory

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;

}

 

// Print the program

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.