Use C language to build a virtual machine framework

Source: Internet
Author: User

1. First, we need a structure to indicate all 16-bit and 8-bit registers of the CPU.

1 # pragma once
2 # include "... CommonBase. h"
3
4 struct Register_16_Bit
5 {
6 // General Register
7 struct
8 {
9 byte ah; // 8Bit
10 byte al; // 8Bit
11} AX;
12 struct
13 {
14 byte bh; // 8Bit
15 byte bl; // 8Bit
16} BX;
17 struct
18 {
19 byte ch; // 8Bit
20 byte cl; // 8Bit
21} CX;
22 struct
23 {
24 byte dh; // 8Bit
25 byte dl; // 8Bit
26} DX;
27
28 // Segment Register
29 ushort cs;
30 ushort ds;
31 ushort ss;
32 ushort es;
33
34 // Special Register
35 ushort ip address;
36 ushort sp;
37 ushort bp;
38 ushort si;
39 ushort di;
40 struct
41 {
42 byte of: 1;
43 byte df: 1;
44 byte if: 1;
45 byte tf: 1;
46 byte sf: 1;
47 byte zf: 1;
48 byte af: 1;
49 byte pf: 1;
50 byte cf: 1;
51 BYTE Reserve: 7;
52} FR;
53
54 Register_16_Bit ()
55 {
56 memset (this, 0, sizeof (Register_16_Bit ));
57}
58 };

2. Then we define an interface to abstract the analysis process.

1 # pragma once
2 # include "... CommonBase. h"
3
4 class CDisasmContext
5 {
6 public:
7 virtual bool Disasm (BYTE * & ptr) = 0;
8 protected:
9 enum OpCodeHeader;
10}; The Disasm function is used to analyze the machine code and determine the semantics.
OpCodeHeader is an enumeration type used to enumerate the command headers of each command.

3. We define a Jmp class to analyze Jmp commands.

1 # pragma once
2 # include ".. DisasmContext. h"
3
4 class CJmp: public CDisasmContext
5 {
6 public:
7 virtual bool Disasm (BYTE * & ptr );
8 protected:
9 enum OpCodeHeader
10 {
11 JMP_SHORT = 0xEB, // skip
12 JMP_NEAR = 0xE9, // skip
13 JMP_FAR = 0xEA, // skip between Segments
14 JMP_IND = 0xFF, // skip within (outside)
15 };
16
17 bool Jmp_Short (BYTE * & ptr );
18 bool Jmp_Near (BYTE * & ptr );
19 bool Jmp_Far (BYTE * & ptr );
20 };
The Disasm function is
1 bool CJmp: Disasm (BYTE * & ptr)
2 {
3 if (Jmp_Short (ptr) return true;
4 else if (Jmp_Near (ptr) return true;
5 else if (Jmp_Far (ptr) return true;
6 else return false;
7} 4. We also define a CMov class to analyze Mov commands (see LibraryDisasmMov)
5. Finally, we define a CDisasm class to drive the overall framework (see LibraryDisasmDisasm. h)
1 class CDisasm
2 {
3 public:
4 bool Disasm (BYTE * ptr, int size );
5 protected:
6 CJmp Jmp;
7 CMov Mov;
8}; its Disasm function is (see LibraryDisasmDisasm. cpp)
1 bool CDisasm: Disasm (BYTE * ptr, int size)
2 {
3 BYTE * end = ptr + size;
4 while (ptr <end)
5 {
6 if (Jmp. Disasm (ptr ));
7 else if (Mov. Disasm (ptr ));
8 else return false;
9}
10 return true;
11}

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.