Exemplo n.º 1
0
        public static Symbol[] Symbols(PassOne passOne, string expression, out char sign)
        {
            sign = '+';
            List <Symbol> symbolsList = new List <Symbol>();

            foreach (var symString in Expressions.SymbolCheck(expression))
            {
                if (passOne.SymbolSearch(symString, out Symbol symbol))
                {
                    symbolsList.Add(symbol);
                }
            }
            if (expression.Contains("-"))
            {
                sign = '-';
            }
            return(symbolsList.ToArray());
        }
Exemplo n.º 2
0
        public static void StartTwo(string inFile, PassOne passOne)
        {
            //could not get the txt and obj file in the right place for some reason
            string       list        = inFile.Remove(inFile.IndexOf('.')) + ".txt";
            string       listPath    = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), list));
            StreamWriter objFile     = new StreamWriter(listPath);
            string       objFileName = inFile.Remove(inFile.IndexOf('.')) + ".obj";

            string[] Lines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), inFile));
            objFile.WriteLine("Pass Two");
            objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {5,-10}", "Line", "LC", "Label", "Operation", "Operand", "Object Code");
            int  lineNum        = 0;
            bool checkFormatOne = false;

            string[]      inLine = ReadLine(Lines[lineNum++]);
            string        LC     = "0";
            string        BC     = "0";
            string        objAdd = "0";
            string        objCode;
            StringBuilder objRecord = new StringBuilder();
            List <string> txtRecord;
            List <string> modRecord = new List <string>();

            if (inLine[3] == "START")
            {
                objRecord.AppendLine(string.Format("H^{0}^{1}^{2}", inLine[2], passOne.StartAdd.PadLeft(6, '0'), passOne.pLength.PadLeft(6, '0')));

                Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);

                inLine = ReadLine(Lines[lineNum++]);
            }

            txtRecord = new List <string>(new string[2] {
                "T^" + passOne.StartAdd.PadLeft(6, '0'), passOne.StartAdd
            });

            while (inLine[3] != "END")
            {
                objCode = string.Empty;
                LC      = ReadLine(Lines[lineNum])?[1];
                if (passOne.SearchTable(inLine[3], out Opcode opcode))
                {
                    if (inLine[4] != string.Empty && passOne.SymbolSearch(inLine[4], out Symbol found))
                    {
                        objAdd = found.value.ToString("X");
                        if (opcode.form == 4)
                        {
                            if (inLine[4].Contains("+") || inLine[4].Contains("-"))
                            {
                                Symbol[] symbols = Symbols(passOne, inLine[4], out char sign);
                                char[]   signs   = { '+', sign };

                                int i = 0;
                                foreach (var symbol in symbols)
                                {
                                    modRecord.Add(Modifier(symbol, (int.Parse(inLine[1], NumberStyles.HexNumber) + 1).ToString("X"), passOne.pName, "5", signs[i++]));
                                }
                            }
                            else if (passOne.SymbolSearch(inLine[4], out Symbol symbol))
                            {
                                modRecord.Add(Modifier(symbol, (int.Parse(inLine[1], NumberStyles.HexNumber) + 1).ToString("X"), passOne.pName, "5", '+'));
                            }
                        }
                    }
                    else if (inLine[4].Contains("="))
                    {
                        foreach (var literal in passOne.LiteralTable)
                        {
                            if (inLine[4] == literal.name)
                            {
                                objAdd = literal.address;
                            }
                        }
                    }
                    else
                    {
                        objAdd = "0";
                    }
                    objCode = ObjectCode(opcode, objAdd, inLine[4], LC, BC);

                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                    objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                }
                else if (inLine[3] == "BYTE")
                {
                    objCode = Expressions.ParseConstant(inLine[4]).value;
                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                    objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                }
                else if (inLine[3] == "WORD")
                {
                    try
                    {
                        if (inLine[4].Contains("+") || inLine[4].Contains("-"))
                        {
                            Symbol[] symbols = Symbols(passOne, inLine[4], out char sign);
                            char[]   signs   = { '+', sign };
                            int      i       = 0;
                            foreach (var symbol in symbols)
                            {
                                modRecord.Add(Modifier(symbol, inLine[1], passOne.pName, "6", signs[i++]));
                            }
                            Expressions.CheckExpression(sign, inLine[4].Split('+', '-'), symbols, out int value, out bool rFlag);
                            objCode = value.ToString("X").PadLeft(6, '0');
                        }
                        else if (passOne.SymbolSearch(inLine[4], out Symbol symbol))
                        {
                            modRecord.Add(Modifier(symbol, inLine[1], passOne.pName, "6", '+'));
                            objCode = symbol.value.ToString("X").PadLeft(6, '0');
                        }
                        else
                        {
                            objCode = int.Parse(inLine[4]).ToString("X").PadLeft(6, '0');
                        }

                        Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                        objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5, -10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                    }
                    catch
                    { Console.WriteLine("ummm"); }
                }
                else if (inLine[3] == "BASE")
                {
                    if (passOne.SymbolSearch(inLine[4], out Symbol symbol))
                    {
                        BC = symbol.value.ToString("X");
                    }
                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                    objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                }
                else if (inLine[3] == "EXTREF")
                {
                    objRecord.Append("R");
                    foreach (var refEntry in inLine[4].Split(','))
                    {
                        passOne.InsertSymbol(refEntry, "0", false, false, false);
                        objRecord.Append(refEntry);
                    }
                    objRecord.AppendLine();
                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                }
                else if (inLine[3] == "EXTDEF")
                {
                    objRecord.Append("D");
                    foreach (var defEntry in inLine[4].Split(','))
                    {
                        if (passOne.SymbolSearch(defEntry, out Symbol symbol))
                        {
                            objRecord.Append(defEntry + symbol.value.ToString("X").PadLeft(6, '0'));
                        }
                    }
                    objRecord.AppendLine();
                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                }
                else if (inLine[3] == "RESW" || inLine[3] == "RESB" || inLine[3] == "EQU")
                {
                    Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                    objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
                }
                else if (inLine[3] != "RESW" && inLine[3] != "RESB")
                {
                    if (!checkFormatOne)
                    {
                        inLine[2]      = inLine[3];
                        inLine[3]      = inLine[4];
                        inLine[4]      = string.Empty;
                        checkFormatOne = true;
                    }
                    else
                    {
                        inLine = ReadLine(Lines[lineNum++]);
                    }
                    continue;
                }
                if (objCode != string.Empty && txtRecord.Count < 12)
                {
                    txtRecord.Add(objCode);
                }
                else if (txtRecord.Count > 2)
                {
                    txtRecord[1] = (int.Parse(inLine[1], NumberStyles.HexNumber) - int.Parse(txtRecord[1], NumberStyles.HexNumber)).ToString("X").PadLeft(2, '0');
                    txtRecord.ForEach(x => objRecord.Append("^" + x));
                    objRecord.AppendLine();

                    txtRecord = new List <string>(new string[2] {
                        "T^" + inLine[1].PadLeft(6, '0'), inLine[1]
                    });

                    if (objCode != string.Empty)
                    {
                        txtRecord.Add(objCode);
                    }
                }
                checkFormatOne = false;
                inLine         = ReadLine(Lines[lineNum++]);
            }
            Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);
            objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4]);

            if (txtRecord.Count > 2)
            {
                try
                {
                    txtRecord[1] = (int.Parse(inLine[1], NumberStyles.HexNumber) - int.Parse(txtRecord[1]))
                                   .ToString("X").PadLeft(2, '0');
                }
                catch
                { }
                txtRecord.ForEach(x => objRecord.Append("^" + x));
                objRecord.AppendLine();

                txtRecord = new List <string>(new string[2] {
                    "T^" + inLine[1].PadLeft(6, '0'), inLine[1]
                });
            }

            foreach (var literal in passOne.LiteralTable)
            {
                inLine  = ReadLine(Lines[lineNum++]);
                objCode = literal.value;

                Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);
                objFile.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10} {5,-10}", inLine[0], inLine[1], inLine[2], inLine[3], inLine[4], objCode);

                if (txtRecord.Count < 12)
                {
                    txtRecord.Add(objCode);
                }
                else if (txtRecord.Count > 2)
                {
                    txtRecord[1] = (int.Parse(inLine[1], NumberStyles.HexNumber) - int.Parse(txtRecord[1], NumberStyles.HexNumber))
                                   .ToString("X").PadLeft(2, '0');
                    txtRecord.ForEach(x => objRecord.Append("^" + x));
                    objRecord.AppendLine();

                    txtRecord = new List <string>(new string[2] {
                        "T^" + inLine[1].PadLeft(6, '0'), inLine[1]
                    });

                    if (objCode != string.Empty)
                    {
                        txtRecord.Add(objCode);
                    }
                }
            }

            if (txtRecord.Count > 2)
            {
                try
                {
                    txtRecord[1] = (int.Parse(inLine[1], NumberStyles.HexNumber) - int.Parse(txtRecord[1]))
                                   .ToString("X").PadLeft(2, '0');
                }
                catch
                { }
                txtRecord.ForEach(x => objRecord.Append("^" + x));
                objRecord.AppendLine();
            }
            foreach (var moRecord in modRecord)
            {
                objRecord.AppendLine(moRecord);
            }

            objRecord.AppendLine("E^" + passOne.StartAdd.PadLeft(6, '0'));

            File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), objFileName), objRecord.ToString());
            Console.WriteLine("Program Length: {0}", passOne.pLength);
            Console.WriteLine();

            Console.WriteLine("Object Records");
            Console.WriteLine(objRecord.ToString());

            objFile.WriteLine("Program Length: {0}", passOne.pLength);
            objFile.WriteLine("__________________________________________________________________");
            objFile.WriteLine("                          Symbol Table                                  ");
            objFile.WriteLine("{0,-10} {1,-10} {2,-15} {3,-10} {4,-10}", "Label", "Value", "RFlag", "IFlag", "MFlag");
            objFile.WriteLine("__________________________________________________________________");
            foreach (var symbol in passOne.SymbolTable)
            {
                objFile.WriteLine(symbol);
            }

            objFile.WriteLine("_________________________________________________________________");
            objFile.WriteLine("                         Literal Table                           ");
            objFile.WriteLine("{0,-12} {1,-18} {2,-10} {3,-10}", "Name", "Value", "Length", "Address");
            objFile.WriteLine("_________________________________________________________________");
            foreach (var literal in passOne.LiteralTable)
            {
                objFile.WriteLine(literal);
            }
            objFile.Close();
        }