예제 #1
0
        public static void Main(string[] args)
        {
            if (args.Length == 0) // check on correct parameter usage
            {
                Console.WriteLine("Usage: ASSEM source [ immediate ]");
            }
            else
            {
                string sourceName = args[0];
                bool   immediate  = args.Length > 1;
                string codeName   = newFileName(sourceName, ".cod");
                PVM.Init();

                bool assembledOK = PVMAsm.Assemble(sourceName);
                int  codeLength  = PVMAsm.CodeLength();
                int  initSP      = PVMAsm.StackBase();
                PVM.ListCode(codeName, codeLength);
                if (!assembledOK || codeLength == 0)
                {
                    Console.WriteLine("Unable to interpret code");
                }
                else
                {
                    if (immediate)
                    {
                        PVM.QuickInterpret(codeLength, initSP);
                    }
                    char reply = 'n';
                    do
                    {
                        Console.Write("\n\nInterpret [y/N]? ");
                        reply = (Console.ReadLine() + " ").ToUpper()[0];
                        if (reply == 'Y')
                        {
                            PVM.Interpret(codeLength, initSP);
                        }
                    } while (reply == 'Y');
                }
            }
        }
예제 #2
0
 public static void Branch(string mnemonic, Label adr) {
 // Inline assembly of two word branch style instruction with Label operand
   Emit(PVM.OpCode(mnemonic)); Emit(adr.Address());
 }
예제 #3
0
 public static void TwoWord(string mnemonic, int adr) {
 // Inline assembly of two word instruction with integer operand
   Emit(PVM.OpCode(mnemonic)); Emit(adr);
 }
예제 #4
0
 public static void OneWord(string mnemonic) {
 // Inline assembly of one word instruction with no operand
   Emit(PVM.OpCode(mnemonic));
 }
예제 #5
0
        public static bool Assemble(string sourceName)
        {
            // Assembles source code from an input file sourceName and loads codeLen
            // words of code directly into memory PVM.mem[0 .. codeLen-1],
            // storing strings in the string pool at the top of memory in
            // PVM.mem[stkBase .. memSize-1].
            //
            // Returns true if assembly succeeded
            //
            // Instruction format :
            //    Instruction  = [ Label ] Opcode [ AddressField ] [ Comment ]
            //    Label        = [ "{" ] Integer [ "}" ]
            //    Opcode       = Mnemonic
            //    AddressField = Integer | "String"
            //    Comment      = String
            //
            // A string AddressField may only be used with a PRNS opcode
            // Instructions are supplied one to a line; terminated at end of input file

            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }
예제 #6
0
        public static void Main(string[] args)
        {
            bool   mergeErrors = false;
            bool   immediate   = false;
            string inputName   = null;

            // ------------------------ process command line parameters:

            Console.WriteLine("Assem compiler 1.00");

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().Equals("-l"))
                {
                    mergeErrors = true;
                }
                else if (args[i].ToLower().Equals("-i"))
                {
                    immediate = true;
                }
                else
                {
                    inputName = args[i];
                }
            }
            if (inputName == null)
            {
                Console.WriteLine("No input file specified");
                Console.WriteLine("Usage: Parva [-l] source.pav [-l]");
                Console.WriteLine("-l directs source listing to listing.txt");
                Console.WriteLine("-i proceeds directly to interpreter");
                System.Environment.Exit(1);
            }

            // ------------------------ parser and scanner initialization

            int pos = inputName.LastIndexOf('/');

            if (pos < 0)
            {
                pos = inputName.LastIndexOf('\\');
            }
            string dir = inputName.Substring(0, pos + 1);

            string outputName = null;

            pos = inputName.LastIndexOf('.');
            if (pos < 0)
            {
                outputName = inputName + ".pretty";
            }
            else
            {
                outputName = inputName.Substring(0, pos) + ".pretty";
            }
            Parser.pretty = new OutFile(outputName);
            if (Parser.pretty.OpenError())
            {
                Console.WriteLine("cannot open " + outputName);
                System.Environment.Exit(1);
            }

            Scanner.Init(inputName);
            Errors.Init(inputName, dir, mergeErrors);
            PVM.Init();
//      Table.Init();

            // ------------------------ compilation

            Parser.Parse();
            Errors.Summarize();

            // ------------------------ interpretation

            bool   assembledOK = Parser.Successful();
            int    initSP      = CodeGen.GetInitSP();
            string codeName    = newFileName(inputName, ".cod");
            int    codeLength  = CodeGen.GetCodeLength();

            PVM.ListCode(codeName, codeLength);
            if (!assembledOK || codeLength == 0)
            {
                Console.WriteLine("Unable to interpret code");
                System.Environment.Exit(1);
            }
            else
            {
                if (immediate)
                {
                    PVM.QuickInterpret(codeLength, initSP);
                }
                char reply;
                do
                {
                    Console.Write("\n\nInterpret [y/N]? ");
                    reply = (Console.ReadLine() + " ").ToUpper()[0];
                    if (reply == 'Y')
                    {
                        PVM.Interpret(codeLength, initSP);
                    }
                } while (reply == 'Y');
            }
        }
예제 #7
0
        public static bool Assemble(string sourceName)
        {
            string mnemonic; // mnemonic for matching
            char   quote;    // string delimiter

            src = new InFile(sourceName);
            if (src.OpenError())
            {
                Console.WriteLine("Could not open input file\n");
                System.Environment.Exit(1);
            }
            Console.WriteLine("Assembling code ... \n");
            codeLen = 0;
            stkBase = PVM.memSize - 1;
            okay    = true;
            do
            {
                SkipLabel();                         // ignore labels at start of line
                if (!src.EOF())                      // we must have a line
                {
                    mnemonic = ReadMnemonic();       // unpack mnemonic
                    if (mnemonic == null)
                    {
                        continue;                    // ignore directives
                    }
                    int op = PVM.OpCode(mnemonic);   // look it up
                    PVM.mem[codeLen] = op;           // store in memory
                    switch (op)
                    {
                    case PVM.prns:                   // requires a string address field
                        do
                        {
                            quote = src.ReadChar();  // search for opening quote character
                        }while (quote != '"' && quote != '\'' && quote != '\n');
                        codeLen          = (codeLen + 1) % PVM.memSize;
                        PVM.mem[codeLen] = stkBase - 1; // pointer to start of string
                        if (quote == '\n')
                        {
                            Error("Missing string address", codeLen);
                        }
                        else                         // stack string in literal pool
                        {
                            ch = src.ReadChar();
                            while (ch != quote && ch != '\n')
                            {
                                if (ch == '\\')
                                {
                                    ch = src.ReadChar();
                                    if (ch == '\n')  // closing quote missing
                                    {
                                        Error("Malformed string", codeLen);
                                    }
                                    switch (ch)
                                    {
                                    case 't': ch = '\t'; break;

                                    case 'n': ch = '\n'; break;

                                    case '\"': ch = '\"'; break;

                                    case '\'': ch = '\''; break;

                                    default: break;
                                    }
                                }
                                stkBase--;
                                PVM.mem[stkBase] = ch;
                                ch = src.ReadChar();
                            }
                            if (ch == '\n')          // closing quote missing
                            {
                                Error("Malformed string", codeLen);
                            }
                        }
                        stkBase--;
                        PVM.mem[stkBase] = 0;        // terminate string with nul
                        break;

                    case PVM.brn:                    // all require numeric address field
                    case PVM.bze:
                    case PVM.dsp:
                    case PVM.lda:
                    case PVM.ldc:
                    case PVM.ldl:
                    case PVM.stl:
                        codeLen = (codeLen + 1) % PVM.memSize;
                        if (ch == '\n')              // no field could be found
                        {
                            Error("Missing address", codeLen);
                        }
                        else                         // unpack it and store
                        {
                            PVM.mem[codeLen] = src.ReadInt();
                            if (src.Error())
                            {
                                Error("Bad address", codeLen);
                            }
                        }
                        break;

                    case PVM.nul:                    // unrecognized mnemonic
                        Console.Write(mnemonic);
                        Error(" - Invalid opcode", codeLen);
                        break;

                    default:                         // no address needed
                        break;
                    }
                    if (!src.EOL())
                    {
                        src.ReadLn();                // skip comments
                    }
                    codeLen = (codeLen + 1) % PVM.memSize;
                }
            } while (!src.EOF());
            for (int i = codeLen; i < stkBase; i++)  // fill with invalid OpCodes
            {
                PVM.mem[i] = PVM.nul;
            }
            return(okay);
        }