-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMnemonics.cs
160 lines (133 loc) · 4.6 KB
/
Mnemonics.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Text;
using Instruction = vm.Instruction;
class Mnemonics
{
public static Dictionary<string, int> MapAddress(string[] mnemonics)
{
Dictionary<string, int> addresses = [];
int length = mnemonics.Length;
for (int i = 0; i < length; i++)
{
if (mnemonics[i].StartsWith('<') && !addresses.ContainsKey(mnemonics[i][1..mnemonics[i].Length]))
{
int index = GetIndex(mnemonics, mnemonics[i]);
addresses.TryAdd(mnemonics[i][1..(mnemonics[i].Length - 1)], index);
}
}
return addresses;
}
public static byte[] Mnemonic(string mnemo)
{
string[] mnemonics = mnemo.Split(' ');
Dictionary<string, int> addresses = MapAddress(mnemonics);
List<byte> buffer = [];
int length = mnemonics.Length;
for (int i = 0; i < length; i++)
{
var val = mnemonics[i];
if (Instruction.vInstruction.TryGetValue(val, out int value))
{
if (value == 23 || value == 22)
{
byte[] nbrArray = ByteManipulation.ToByteArray(addresses[mnemonics[i + 1][1..(mnemonics[i + 1].Length - 1)]].ToString());
buffer.Add(0);
foreach (var item in nbrArray)
{
buffer.Add(item);
}
buffer.Add((byte)value);
i++;
}
else if (value == 27)
{
buffer.Add(27);
int strSize;
string str;
try
{
strSize = int.Parse(mnemonics[i + 1]);
str = mnemonics[i + 2];
}
catch (IndexOutOfRangeException)
{
throw new Exception("Missing arguments for PUSH_STR");
}
i += 2;
byte[] nbrArray = ByteManipulation.ToByteArray(strSize.ToString());
foreach (var item in nbrArray)
{
buffer.Add(item);
}
byte[] byteStr = ByteManipulation.SerializeString(str, 4);
foreach (var item in byteStr)
{
buffer.Add(item);
}
}
else if (value == 28)
{
buffer.Add(28);
i++;
string c;
try
{
c = mnemonics[i];
}
catch (IndexOutOfRangeException)
{
throw new Exception("Missing arguments for PUSH_CHAR_CONST");
}
byte[] charArray = ByteManipulation.SerializeString(c, 4);
foreach (var item in charArray)
{
buffer.Add(item);
}
}
else buffer.Add((byte)value);
}
else if (val[0] == '<')
{
int index = addresses[val[1..(val.Length - 1)]];
byte[] nbrArray = ByteManipulation.ToByteArray(index.ToString());
foreach (var item in nbrArray)
{
buffer.Add(item);
}
}
else if (!int.TryParse(val, out int _))
{
continue;
}
else
{
byte[] nbrArray = ByteManipulation.ToByteArray(mnemonics[i]);
foreach (var item in nbrArray)
{
buffer.Add(item);
}
}
}
return [.. buffer];
}
private static int GetIndex(string[] mnemonics, string value)
{
int inc = 0;
int length = mnemonics.Length;
for (int i = 0; i < length; i++)
{
string val = mnemonics[i];
if (val.Length > 1 && val.EndsWith(':') && val[..(val.Length - 1)] == value[1..(value.Length - 1)])
{
return inc;
}
if (int.TryParse(val, out int _)) inc += 4;
else if (Instruction.vInstruction.ContainsKey(val.Trim()))
{
if (val == "JUMP" || val == "CJUMP") inc += 5;
if (val == "CALL") inc += 5;
else inc += 1;
}
}
return -1;
}
}