Esempio n. 1
0
        // convert assembly code to bytecode
        private static bool AssembleCode(string file)
        {
            Output.Clear();

            // parse lines
            for (int i = 0; i < LabelOutput.Count; i++)
            {
                // get current line
                LabelOutput[i] = LabelOutput[i].Replace(",", "");
                string line = LabelOutput[i];

                // get arguments
                List <string> args = StringArrayToList(line.Split(' '));

                // remove blank arguments
                for (int j = 0; j < args.Count; j++)
                {
                    args[j] = StringRemoveBlanks(args[j].ToCharArray());
                    if (IsBlank(args[j]))
                    {
                        args.RemoveAt(j); j--;
                    }
                }

                if (LabelOutput[i].Length > 1 && args[0] != "SHORT" && args[0] != "BYTE" && args[0] != "CHAR" && args[0] != "WORD")
                {
                    for (int j = 0; j < Instructions.List.Count; j++)
                    {
                        // is instruction valid?
                        if (args[0] == Instructions.List[j].Name)
                        {
                            // add instruction
                            Output.Add(Instructions.List[j].OpCode);
                            string format = Instructions.List[j].Format;
                            if (Instructions.List[j].Arguments == 0)
                            {
                                break;
                            }

                            if (format == "b")
                            {
                                byte b = (byte)ParseArgument(args[1]);
                                Output.Add(b);
                            }
                            else if (format == "s")
                            {
                                ushort addr  = (ushort)ParseArgument(args[1]);
                                byte   left  = (byte)((addr & 0xFF00) >> 8);
                                byte   right = (byte)(addr & 0x00FF);
                                Output.Add(left);
                                Output.Add(right);
                            }
                            else if (format == "bb")
                            {
                                byte b1 = (byte)(ParseArgument(args[1]));
                                byte b2 = (byte)(ParseArgument(args[2]));
                                Output.Add(b1);
                                Output.Add(b2);
                            }
                            else if (format == "bs")
                            {
                                bool ignore = false;
                                for (int n = 0; n < Variables.Count; n++)
                                {
                                    if (args[1] == Variables[n].Name)
                                    {
                                        ignore = true; break;
                                    }
                                    else
                                    {
                                        ignore = false;
                                    }
                                }
                                if (!ignore)
                                {
                                    byte   b     = (byte)(ParseArgument(args[1]));
                                    ushort addr  = (ushort)ParseArgument(args[2]);
                                    byte   left  = (byte)((addr & 0xFF00) >> 8);
                                    byte   right = (byte)(addr & 0x00FF);
                                    Output.Add(b);
                                    Output.Add(left);
                                    Output.Add(right);
                                }
                            }
                            else if (format == "bbb")
                            {
                                byte b1 = (byte)(ParseArgument(args[1]));
                                byte b2 = (byte)(ParseArgument(args[2]));
                                byte b3 = (byte)(ParseArgument(args[3]));
                                Output.Add(b1);
                                Output.Add(b2);
                                Output.Add(b3);
                            }
                            else
                            {
                                CLI.Write("[ERROR] ", Color.Red); CLI.WriteLine("Invalid arguments at line " + i.ToString());
                            }
                            break;
                        }
                    }
                }
            }

            // write output
            CLI.WriteLine("Displaying binary output: ", Color.Gray);
            int l = CLI.CursorY;

            for (int j = 0; j < Output.Count; j++)
            {
                if (l >= CLI.Height)
                {
                    Console.ReadLine(); Console.Clear(); l = 0;
                }
                CLI.Write(IntToHex(Output[j], "X2"), Color.Magenta);
                if (CLI.CursorX < 77)
                {
                    CLI.Write(" ");
                }
                else
                {
                    CLI.WriteLine(""); l++;
                }
            }
            CLI.WriteLine("");

            // save file
            PMFAT.WriteAllBytes(file, Output.ToArray());

            return(true);
        }