Exemplo n.º 1
0
        private bool SecondPass(FileInfo source)
        {
            // For debugging purposes
            int lineCounter = 0;

            using (StreamReader sr = new StreamReader(source.FullName))
            {
                string   line  = sr.ReadLine().ToUpper();
                string[] split = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // ignore whitespace
                if (split[0] != PseudoInstruction.Origin.Mnemonic)
                {
                    Console.WriteLine($"Line #{lineCounter}: {line} is not valid!\nProgram must begin with origin declaration.");
                    return(false);
                }
                else
                {
                    ushort address = Utilities.StringToAddress(split[1]);
                    InstructionCounter = address;
                    OutputToBuffer(address);
                    OutputToBuffer(ByteCounter);
                }
                while (!sr.EndOfStream)
                {
                    lineCounter++;
                    line  = sr.ReadLine().ToUpper();
                    split = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // ignore whitespace
                    if (Labels.ContainsKey(split[0]) && split.Length > 1)
                    {
                        if (split[1] == PseudoInstruction.Constant.Mnemonic)
                        {
                            byte value = Utilities.StringToByte(split[2]);
                            OutputToBuffer(value);
                        }
                        else
                        {
                            Console.WriteLine($"Line #{lineCounter}: {line} is not valid!\nExpecting {PseudoInstruction.Constant.Mnemonic}");
                            return(false);
                        }
                    }
                    else if (!PseudoInstructions.ContainsKey(split[0]) && split.Length > 1)
                    {
                        // Machine instruction
                        string operation = split[0];
                        string operand   = split[1];
                        ushort opValue;
                        if (Labels.ContainsKey(operand))
                        {
                            opValue = Labels[operand];
                        }
                        else
                        {
                            opValue = Utilities.StringToAddress(operand);
                        }
                        if (Mnemonics[operation].Size == 1)
                        {
                            byte value = (byte)((Mnemonics[operation].Opcode >> 8) | (byte)opValue);
                            OutputToBuffer(value);
                        }
                        else
                        {
                            ushort value = (ushort)(Mnemonics[operation].Opcode | opValue);
                            OutputToBuffer(value);
                        }
                    }
                }
            }
            OutputToBuffer(Checksum);
            return(true);
        }
Exemplo n.º 2
0
 private bool FirstPass(FileInfo source)
 {
     using (StreamReader sr = new StreamReader(source.FullName))
     {
         int      lineCounter = 0; // debug source file
         string   line;
         string[] split;
         while (!sr.EndOfStream)
         {
             line  = sr.ReadLine().ToUpper();
             split = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries); // ignore whitespace
             // Instruction lines must begin with whitespace.
             if (!char.IsWhiteSpace(line[0]))
             {
                 // Must be a symbol/label
                 string newLabel = split[0];
                 if (PseudoInstructions.ContainsKey(newLabel) || Mnemonics.ContainsKey(newLabel))
                 {
                     Console.WriteLine($"Line #{lineCounter}: {line} is not valid!\nCannot declare a symbol with the same name as an instruction or pseudo-instruction.");
                     return(false);
                 }
                 if (split.Length > 1)
                 {
                     // Declaring a new symbol.
                     if (split[1] == PseudoInstruction.Constant.Mnemonic)
                     {
                         Labels.Add(newLabel, InstructionCounter);
                         InstructionCounter += 1;
                         ByteCounter        += 1;
                     }
                     else
                     {
                         Console.WriteLine($"Line #{lineCounter}: {line} is not valid!\nInvalid pseudo-intruction.");
                         return(false);
                     }
                 }
                 else
                 {
                     // Declaring a new label.
                     Labels.Add(newLabel, InstructionCounter);
                 }
             }
             else
             {
                 string instruction = split[0];
                 string operand     = split[1];
                 if (instruction == PseudoInstruction.Origin.Mnemonic)
                 {
                     InstructionCounter = Utilities.StringToAddress(operand);
                 }
                 else if (instruction == PseudoInstruction.End.Mnemonic)
                 {
                     // should be eof
                 }
                 else if (Mnemonics.ContainsKey(instruction))
                 {
                     InstructionCounter += Mnemonics[instruction].Size;
                     ByteCounter        += Mnemonics[instruction].Size;
                 }
                 else
                 {
                     Console.WriteLine($"Line #{lineCounter}: {line} is not valid!\nInvalid instruction.");
                     return(false);
                 }
             }
             lineCounter++;
         }
     }
     return(true);
 }