1

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.

Birdd
  • 47
  • 2
  • 11
  • 1
    Read all lines in a list, add new text to beginning of the list and then write it to output file, – fhnaseer Apr 19 '13 at 07:05
  • http://stackoverflow.com/questions/10341188/string-concatenation-using-operator – Sayse Apr 19 '13 at 07:07
  • Hello Neil, Can you have a look at the answers provided and select any one as the answer. That will give you 5 points and get you closer to the up-voting privilege that is at 15 – Patrick D'Souza Apr 19 '13 at 16:49

3 Answers3

0
var inputLines = File.ReadAllLines(inputFilePath);
for (int i=0; i<inputLines.Length; i++)
    inputLines[i] = "Some Text" + inputLines[i];
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
  • I changed the variable "inputFilePath" to inputFile, because I assume that's what you meant and it's throwing this error: Error 1 The best overloaded method match for 'System.IO.File.ReadAllLines(string)' has some invalid arguments Error 2 Argument1:cannot convert from System.IO.StreamReader to string – Birdd Apr 19 '13 at 07:18
  • var inputFilePath = @"C:\input.txt"; – fhnaseer Apr 19 '13 at 07:31
  • inputFilePath is a string, and it should work. If file is not present then you will get some exception, but that will be different. – fhnaseer Apr 19 '13 at 07:31
0

Assuming your prefixes are in another list

var prefixes = new List<string>(/* som values */);

var ix = 0;

var result = fileContents.Select(x => string.Join(" ", prefixes[ix++], x)).ToArray();

If you need to join on line numbers (from a dictionary)

var prefixes = new Dictionary<int, string>(); // Needs values

var result = new List<string>();

for (var i = 0; i < fileContents.Count; i++){
   string prefix;

   if (prefixes.TryGetValue(i, out prefix){ result.Add(string.Join(" ", prefix, fileContent[i])) }
   else { result.Add(fileContent[i]);}
}
faester
  • 14,886
  • 5
  • 45
  • 56
  • My prefixes are generate per line using the i variable in the loop, like this: for (int i = 0; i < inputLines.Length; i++) { int addedMemory = 0x4 * i; int memoryAddress = addedMemory + startingAddress; string memory = memoryAddress.ToString("x"); inputLines[i] = memory + inputLines[i]; } – Birdd Apr 19 '13 at 07:25
0

You could use StringBuilder as a optimization of Faisal's code, otherwise its perfect for your needs

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();

System.Text.StringBuilder sb = new System.Text.StringBuilder();
string inputLines = System.IO.File.ReadAllLines(inp);
 for (int i = 0; i < inputLines.Length; i++)
     sb.Append("Some Text" + inputLines[i] + Environment.NewLine);

File.WriteAllText(otp, sb.ToString())
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • I changed the variable "inputFilePath" to inputFile, because I assume that's what you meant and it's throwing this error: Error 1 The best overloaded method match for 'System.IO.File.ReadAllLines(string)' has some invalid arguments Error 2 Argument1:cannot convert from System.IO.StreamReader to string – Birdd Apr 19 '13 at 07:26
  • It should be inp as per your requirement, I have edited the post – Patrick D'Souza Apr 19 '13 at 07:31
  • I have merged the post with yours as a complete block – Patrick D'Souza Apr 19 '13 at 07:36