-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
112 lines (93 loc) · 4.19 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.nio.ByteBuffer;
public class Main {
public static void main(String[] args) throws Exception {
//VirtualMachine vm = new VirtualMachine();
/* byte[] summationOfTwentyThreeByteArray = {
//push 23
00,00,00,00,0x17,
//push 1
00,00,00,00,01,
//store 00 01
00,00,00,00,01,
0x0D,
//store 00 00
00,00,00,00,00,
0x0D,
//push 0
00,00,00,00,00,
//push 1
00,00,00,00,01,
//store 00 03
00,00,00,00,03,
0x0D,
//store 00 02
00,00,00,00,02,
0x0D,
//load 00 02 -starting the loop
0x19,
00,00,00,00,02,
0x0C,
//load 00 03
00,00,00,00,03,
0x0C,
//add
02,
//store 00 02
00,00,00,00,02,
0x0D,
//load 00 03
00,00,00,00,03,
0x0C,
//push 1
00,00,00,00,01,
//add
02,
//store 00 03
00,00,00,00,03,
0x0D,
//load 00 03
00,00,00,00,03,
0x0C,
//load 00 00,
00,00,00,00,00,
0x0C,
//eq
0x12,
0x1A,
00,00,00,00,0x2C,
//cjump - conditional jump to loop
0x0B,
//load 00 02
00,00,00,00,02,
0x0C,
//load 00 00
00,00,00,00,00,
0x0C,
//add - last argument
02,
//return
0x08
}; */
/* String mnemonics = "PUSH 00 00 00 17 PUSH 00 00 00 01 STORE 00 01 STORE 00 00 PUSH 00 00 00 00 PUSH 00 00 00 01 STORE 00 03 STORE 00 02 LOAD 00 02 LOAD 00 03 ADD STORE 00 02 LOAD 00 03 PUSH 00 00 00 01 ADD STORE 00 03 LOAD 00 03 LOAD 00 00 EQ CJUMP FF E0 LOAD 00 02 LOAD 00 00 ADD RETURN";
byte[] byteCode = vm.mnemonicsToByteCode(mnemonics);
System.out.println(vm.byteInterpreter(byteCode)); */
//System.out.println(vm.byteInterpreter(summationOfTwentyThreeByteArray));
/* byte[] byteCode = {00,00,00,00,01,00,00,00,00,00,0x0D,00,00,00,00,03,00,00,00,00,01,0x0D,00,00,00,00,01,0x0C,0x08};
System.out.println(vm.byteToMnemonicsString(summationOfTwentyThreeByteArray)); */
/* String mnemonics = "PUSH 00 00 00 17 PUSH 00 00 00 01 PUSH 00 00 00 01 STORE PUSH 00 00 00 0 STORE fallback: PUSH 00 00 00 00 PUSH 00 00 00 01 PUSH 00 00 00 03 STORE PUSH 00 00 00 02 STORE JUMPDEST PUSH 00 00 00 02 LOAD PUSH 00 00 00 03 LOAD ADD PUSH 00 00 00 02 STORE PUSH 00 00 00 03 LOAD PUSH 00 00 00 01 ADD PUSH 00 00 00 03 STORE PUSH 00 00 00 03 LOAD PUSH 00 00 00 00 LOAD EQ NOT PUSH 00 00 00 2C CJUMP PUSH 00 00 00 02 LOAD PUSH 00 00 00 00 LOAD GOTO fallback ADD RETURN";
//System.out.println(vm.regionMnemonicsToMnemonics(mnemonics));
String ifMnemonics = "PUSH 00 00 00 01 PUSH 00 00 00 00 STORE PUSH 00 00 00 00 LOAD PUSH 00 00 00 00 EQ PUSH 00 00 00 28 CJUMP PUSH 00 00 00 17 PUSH 00 00 00 2E JUMP JUMPDEST PUSH 00 00 00 45 JUMPDEST RETURN"; //return 69 if 2 last args of stack are equal and return 23 if not
System.out.println(vm.byteInterpreter(vm.mnemonicsToByteCode(ifMnemonics)));
*/
VirtualMachine vm = new VirtualMachine();
// Specify the bytecode hash from the database
int bytecodeHashInt = 23; // We will try to execute the bytecode with the 23 key hash value
byte[] bytecodeHash = ByteBuffer.allocate(4).putInt(bytecodeHashInt).array(); // Converting from int to an array of bytes
// Creating a bytecode snippet from mnemonics using the EXEC instruction then converting it to bytecode
String mnemonics = "EXEC " + vm.byteToString(bytecodeHash, 0, 4).trim() + " RETURN";
System.out.println("Bytecode representation to execute: " + mnemonics);
byte[] bytecodeFromDatabase = vm.mnemonicsToByteCode(mnemonics);
// Executing the bytecode from the database and printing its result
System.out.println(vm.byteInterpreter(bytecodeFromDatabase));
}
}