Skip to content

Commit e74cd36

Browse files
committed
Added jump and Compares
1 parent b481987 commit e74cd36

File tree

2 files changed

+184
-34
lines changed

2 files changed

+184
-34
lines changed

VM/Program.cs

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ static void Main(string[] args)
1919
else
2020
vm = new VirtualMachine(File.ReadAllBytes("vmtest"));
2121
vm.Run(0);
22-
23-
Console.WriteLine("");
22+
Console.WriteLine();
2423
Console.WriteLine("---STACK BEGIN---");
2524
foreach (var s in vm.Stack)
2625
{
2726
Console.WriteLine(s.ToString());
2827
}
2928
Console.WriteLine("---STACK END---");
3029
Console.ReadLine();
31-
3230
}
3331
}
3432

@@ -54,18 +52,16 @@ public VirtualMachine(byte[] b)
5452
CSharpTypesSectionOffset = Marshal.ReadInt32(MemoryPointer, 16);
5553
ClearSectionOffset = CodeSectionOffset + TextSectionOffset + CSharpTypesSectionOffset;
5654
MemoryPointer = IntPtr.Add(MemoryPointer, 24);
57-
Console.WriteLine("VM Size: " + int.MaxValue);
58-
Console.WriteLine("Image Size: " + ImageSize);
5955
}
6056

61-
private object pop()
57+
private object Pop()
6258
{
6359
object rez = Stack[Stack.Length - 1];
6460
Array.Resize(ref Stack, Stack.Length - 1);
6561
return rez;
6662
}
6763

68-
private void push(object val)
64+
public void Push(object val)
6965
{
7066
Array.Resize(ref Stack, Stack.Length + 1);
7167
Stack[Stack.Length - 1] = val;
@@ -88,21 +84,38 @@ public void Run(int Offset)
8884
TextSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset)), str,
8985
0, str.Length);
9086
Offset += 8;
91-
push(Encoding.UTF8.GetString(str));
87+
Push(Encoding.UTF8.GetString(str));
9288
break;
9389
}
9490
case 0x51: //LdInt
9591
{
96-
push(Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset));
92+
Push(Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset));
9793
Offset += 4;
9894
break;
9995
}
100-
case 0x52: //LdInt
96+
case 0x52: //LdByte
10197
{
102-
push(Marshal.ReadByte(MemoryPointer, CodeSectionOffset + Offset));
98+
Push(Marshal.ReadByte(MemoryPointer, CodeSectionOffset + Offset));
10399
Offset += 1;
104100
break;
105101
}
102+
case 0x60: // Br
103+
{
104+
Offset = CodeSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
105+
}
106+
break;
107+
case 0x61: // BrTrue
108+
{
109+
if((int)Pop() == 1)
110+
Offset = CodeSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
111+
}
112+
break;
113+
case 0x62: // BrFalse
114+
{
115+
if ((int)Pop() == 0)
116+
Offset = CodeSectionOffset + Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
117+
}
118+
break;
106119
case 0x40: //Call C# Method
107120
{
108121
Run(Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset));
@@ -135,51 +148,86 @@ public void Run(int Offset)
135148
CSharpTypesSectionOffset + Marshal.ReadInt32(MemoryPointer,
136149
CodeSectionOffset + Offset + 24 + i * 8)), temp, 0, temp.Length);
137150
types[i] = Type.GetType(Encoding.UTF8.GetString(temp));
138-
arguments[i] = pop();
151+
arguments[i] = Pop();
139152
}
140153

141154
var method = Type.GetType(type).GetMethod(voidname, types);
142155

143156
var rez = method.Invoke(null, arguments);
144157
if (rez != null)
145158
{
146-
push(rez);
159+
Push(rez);
147160
}
148161

149162
Offset += 20 + argcount * 8;
150163
break;
151164
}
165+
case 0xA1:
166+
{
167+
var val2 = (int)Pop();
168+
var val1 = (int)Pop();
169+
if (val1 == val2)
170+
Push(1);
171+
else
172+
Push(0);
173+
break;
174+
}
175+
case 0xA2:
176+
{
177+
var val2 = (int)Pop();
178+
var val1 = (int)Pop();
179+
if (val1 > val2)
180+
Push(1);
181+
else
182+
Push(0);
183+
break;
184+
}
185+
case 0xA3:
186+
{
187+
var val2 = (int)Pop();
188+
var val1 = (int)Pop();
189+
if (val1 < val2)
190+
Push(1);
191+
else
192+
Push(0);
193+
break;
194+
}
152195
case 0x91: //Pop
153196
{
154-
pop();
197+
Pop();
198+
break;
199+
}
200+
case 0x92: //Dup
201+
{
202+
Push(Stack[Stack.Length - 1]);
155203
break;
156204
}
157205
case 0x93: //Push Top stack object to Global Variables
158206
{
159207
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
160208
Offset += 4;
161-
GlobalVariables[index] = pop();
209+
GlobalVariables[index] = Pop();
162210
break;
163211
}
164212
case 0x94: // Load Global Variable to Stack
165213
{
166214
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
167215
Offset += 4;
168-
push(GlobalVariables[index]);
216+
Push(GlobalVariables[index]);
169217
break;
170218
}
171219
case 0x95: //Push Top stack object to Local Variables
172220
{
173221
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
174222
Offset += 4;
175-
LocalVariables[index] = pop();
223+
LocalVariables[index] = Pop();
176224
break;
177225
}
178226
case 0x96: // Load Local Variable to Stack
179227
{
180228
int index = Marshal.ReadInt32(MemoryPointer, CodeSectionOffset + Offset);
181229
Offset += 4;
182-
push(LocalVariables[index]);
230+
Push(LocalVariables[index]);
183231
break;
184232
}
185233
case 0x22:

0 commit comments

Comments
 (0)