For a homework assignment I have to create a very simplified assembler for MIPS code. So we take in an input file of MIPS instruction then output a file with the associated binary for the input code. Each line of code must be mapped to a "memory" location which is just a hexadecimal value in front of the line, but we add/assign this "memory".
Therefore, what I would like to do is read in each line from a text file and at the front I would like to append a value (starting memory address in hex + (line number * 4.) Then I would like to re-read the file. If I need to read the whole file in, create a new file with the memory assigned, then read that file that's fine but I imagine probably unneccesary.
Our professor suggested a list, so here's what I have so far:
Console.WriteLine("Please enter the path to the input file:");
string inp = Console.ReadLine();
Console.WriteLine("Please enter the name of the new file:");
string otp = Console.ReadLine();
StreamReader inputFile = new StreamReader(inp);
StreamWriter outputFile = new StreamWriter(otp);
List<string> fileContents = new List<string>();
while ((inp = inputFile.ReadLine()) != null)
fileContents.Add(inp);
So my question is: How do I add a string to the beginning of each item in that list (fileContents)?
Edit: Followup on this: I have managed to do all of this so far, I've brought in my whole document, mapped memory locations to each line, etc. However, I need to further edit some of the lines that are in the list "inputLines" by deleting some information from them.
The format will always be [0] Memory Address [1] Label or, if no label in this line then registers, operations, etc. [2]-[?] registers, operations, etc. Once I've mapped my memory to each line, any line that has a label I want to put into a dictionary with the index as the label and the memory address as the value contained, then get rid of the label. So - how do I delete that information from any line that contains it?
//go through each line, if any line's first "token" is a label,
//put it in the dictionary as the index with the memory address as the value
//delete the label from the line
for (int i = 0; i < inputLines.Length; i++)
{
string[] token = inputLines[i].Split(new char[] { ' ', ',', '(', ')', ':' }, StringSplitOptions.RemoveEmptyEntries);
string possibleLabel = token[1];
if (opcodes.ContainsKey(possibleLabel) == false)
{
labeltable.Add(possibleLabel, token[0]);
//at this point I want to delete the possibleLabel from the inputLines[i] and not deal with it anymore.
}
}
That does correctly map to my dictionary, so not worried about that part.