-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
64 lines (51 loc) · 1.64 KB
/
Utils.cs
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
using Instruction = vm.Instruction;
class Utils
{
public static int ToUint32(byte[] arr)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(arr);
return (int)BitConverter.ToUInt32(arr, 0);
}
public static byte[] ToByteArray(string str)
{
int nbr = int.Parse(str);
byte[] nbrArray = BitConverter.GetBytes(nbr);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(nbrArray);
}
return nbrArray;
}
public static List<string> ByteCodeToMnemonics(byte[] bytecode)
{
List<string> mnemonic = [];
int length = bytecode.Length;
int i = 0;
while (i < length)
{
if (bytecode[i] == 0 || bytecode[i] == 24)
{
mnemonic.Add($"{i}: {Instruction.vInstruction.FirstOrDefault(x => x.Value == bytecode[i]).Key} {ToUint32(bytecode[i..(i + 5)])}");
i += 5;
continue;
}
mnemonic.Add($"{i}: {Instruction.vInstruction.FirstOrDefault(x => x.Value == bytecode[i]).Key}");
i++;
}
return mnemonic;
}
public static int GetIndex(string bytecode)
{
string[] arr = bytecode.Split(" ");
int length = arr.Length;
int inc = 0;
for (int i = 0; i < length; i++)
{
if (Instruction.vInstruction.TryGetValue(arr[i].ToString(), out int _)) inc++;
else if (int.TryParse(arr[i], out int _)) inc += 4;
else if (arr[i].StartsWith("</")) inc += 1;
}
return inc;
}
}