-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressHandler.cs
More file actions
60 lines (55 loc) · 1.96 KB
/
AddressHandler.cs
File metadata and controls
60 lines (55 loc) · 1.96 KB
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
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Globalization;
namespace BasmProgramHandler
{
public class AddressHandler
{
public static Dictionary<int, string> HandleProgram(string filename, List<int> breakpoints)
{
// TODO: implement token enum
// Variables initialization
int offset = 0;
int counter = 1;
string[] program;
var mappedProgram = new Dictionary<int, string>();
var breaks = new Dictionary<int, string>();
var labels = new Dictionary<string, int>();
// Convert input basm program
// to array where every string
// represent one command
program = File.ReadAllLines(filename);
foreach (string command in program)
{
if (breakpoints.Contains(counter))
breaks.Add(offset, command);
// If command is comment or blank line then skip it
if (Regex.Match(command, "^#").Success || command == "")
{
counter += 1;
continue;
}
// If we have ORG command, then update current offset
if (Regex.Match(command, "ORG *").Success)
{
offset = Int32.Parse(Regex.Match(command, "ORG\\s*([0-9A-Fa-f]+)").Groups[1].Value, NumberStyles.HexNumber);
counter += 1;
continue;
}
// If command is label
if (Regex.Match(command, ":$").Success)
{
labels.Add(command, offset);
counter += 1;
continue;
}
mappedProgram.Add(offset, command);
offset += 1;
counter += 1;
}
return breaks;
}
}
}