public static Dictionary<string, byte[]> AssembleCode(Dictionary<string, string[]> sections, int offset, Dictionary<string, ushort> constants, Dictionary<string, int> data)
{
// Select only sections which are text or rstXX and replace constants/data, convert hex values, remove whitespaced or comment only lines
// then add a space on the end for anchoring.
var sectionsRstText = sections.Where(kvp => rsts.Contains(kvp.Key) || kvp.Key == "text")
.Select(kvp => new KeyValuePair<string, string[]>(kvp.Key, ReplaceAllConstantsData(ChangeFromHex(kvp.Value), constants, data)
.RemoveWhitespaceComment()
.Select(s => s + " ")
.ToArray()))
.ToDictionary();
// Add any rstXX that have been missing with empty code sections
foreach (var rst in rsts)
if(!sectionsRstText.ContainsKey(rst))
sectionsRstText.Add(rst, new string[0]);
// Change all the lines of code to RegexOpcodes give more useful information
var regexOpcodes = sectionsRstText.Select(kvp => new KeyValuePair<string, RegexOpcode[]>(kvp.Key, GetRegexOpcodes(kvp.Value))).ToDictionary();
// Sort the list so that rst00, rst08 ... text are in order
var sectionsRstTextSorted = sectionsRstText.ToList();
sectionsRstTextSorted.Sort((p, q) => p.Key.CompareTo(q.Key));
// Get the labels for _all_ the sections
var labels = GetLabels(sectionsRstTextSorted, regexOpcodes, offset);
// Get the assembled bytes for _all the sections
var assembledCode = sectionsRstTextSorted.Select(kvp => new KeyValuePair<string, byte[]>(kvp.Key, GetCode(kvp.Value, labels, regexOpcodes[kvp.Key]))).ToDictionary();
return assembledCode;
}