コード例 #1
0
 public void ReadInstruction(int line, bool section)
 {
     CurrentInstruction = section ? InputText[line] : InputData[line];
     if (CurrentInstruction.Contains("#"))
     {
         CurrentInstruction = CurrentInstruction.Substring(0, CurrentInstruction.IndexOf("#"));
     }
     CurrentLine = line;
 }
コード例 #2
0
        private int ParseInstruction()
        {
            int i = 0, j = 0; //temp vars

            RemoveSpaces(CurrentInstruction);

            if (CurrentInstruction.Contains(":"))
            {
                return(-2);
            }

            if (CurrentInstruction.Length < 4)
            {
                gui.ReportError("Error: Unknown operation");
                return(-2);
            }

            for (j = 0; j < CurrentInstruction.Length; j++)
            {
                var temp = CurrentInstruction.ElementAt(j);
                if (temp == ' ' || temp == '\t')
                {
                    break;
                }
            }

            string op = CurrentInstruction.Substring(0, j);

            if (CurrentInstruction.Length > 0 && j < CurrentInstruction.Length - 1)
            {
                CurrentInstruction = CurrentInstruction.Substring(j + 1);
            }

            int OperationID = -1;

            for (i = 0; i < InstructionSet.Length; i++)
            {
                if (op == InstructionSet[i])
                {
                    OperationID = i;
                    break;
                }
            }
            if (OperationID == -1)
            {
                gui.ReportError("Error:Unknown operation");
                return(-2);
            }
            if (OperationID < 13) // R Format
            {
                for (int count = 0; count < 3 - (OperationID > 8 ? 1 : 0); count++)
                {
                    RemoveSpaces(CurrentInstruction);
                    if (!FindRegister(count))
                    {
                        return(-2);
                    }
                    RemoveSpaces(CurrentInstruction);
                    if (count == 2 - (OperationID > 8 ? 1 : 0))
                    {
                        break;
                    }
                    if (!assertRemoveComma())
                    {
                        return(-2);
                    }
                }
                if (CurrentInstruction != "")
                {
                    gui.ReportError("Error: Extra arguments provided");
                    return(-2);
                }
            }
            else if (OperationID < 17) // I format
            {
                for (int count = 0; count < 2; count++)
                {
                    RemoveSpaces(CurrentInstruction);
                    FindRegister(count);
                    RemoveSpaces(CurrentInstruction);
                    assertRemoveComma();
                }
                RemoveSpaces(CurrentInstruction);
                string tempString = findLabel();
                int    temp;
                if (!int.TryParse(tempString, out temp))
                {
                    gui.ReportError("Error: Not a valid Immediate argument");
                    return(-2);
                }
                args[2] = temp;
            }
            else if (OperationID < 21) // lw sw ldc1 sdc1
            {
                string tempString = "";
                int    offset;
                RemoveSpaces(CurrentInstruction);
                FindRegister(0);
                RemoveSpaces(CurrentInstruction);
                assertRemoveComma();
                RemoveSpaces(CurrentInstruction);
                if ((CurrentInstruction.ElementAt(0) > 47 && CurrentInstruction.ElementAt(0) < 58) || CurrentInstruction.ElementAt(0) == '-')
                {
                    j = 0;
                    while (j < CurrentInstruction.Length && CurrentInstruction.ElementAt(j) != ' ' &&
                           CurrentInstruction.ElementAt(j) != '\t' && CurrentInstruction.ElementAt(j) != '(')
                    {
                        tempString = tempString + CurrentInstruction.ElementAt(j);
                        j++;
                    }
                    if (j == CurrentInstruction.Length)
                    {
                        gui.ReportError("Error: '(' expected");
                        return(-2);
                    }
                    int temp;
                    if (!int.TryParse(tempString, out temp))
                    {
                        gui.ReportError("Error: not a valid offset");
                        return(-2);
                    }
                    offset             = temp;
                    CurrentInstruction = CurrentInstruction.Substring(j);
                    RemoveSpaces(CurrentInstruction);
                    if (CurrentInstruction == "" || CurrentInstruction.ElementAt(0) != '(' || CurrentInstruction.Length < 2)
                    {
                        gui.ReportError("Error: '(' expected");
                        return(-2);
                    }
                    CurrentInstruction = CurrentInstruction.Substring(1);
                    RemoveSpaces(CurrentInstruction);
                    FindRegister(1);
                    RemoveSpaces(CurrentInstruction);
                    if (CurrentInstruction == "" || CurrentInstruction.ElementAt(0) != ')')
                    {
                        gui.ReportError("Error: ')' expected");
                        return(-2);
                    }
                    CurrentInstruction = CurrentInstruction.Substring(1);
                    OnlySpaces(0, CurrentInstruction.Length, CurrentInstruction);
                    args[2] = offset;
                    if (args[2] == -1)
                    {
                        gui.ReportError("Error: invalid offset");
                        return(-2);
                    }
                }
                else //label
                {
                    tempString = findLabel();
                    bool foundLocation = false;
                    for (j = 0; j < MemoryTable.Count; j++)
                    {
                        if (tempString == MemoryTable[j].Label)
                        {
                            foundLocation = true;

                            args[1] = j;
                            break;
                        }
                    }
                    if (!foundLocation)
                    {
                        gui.ReportError("Error: invalid label");
                        return(-2);
                    }
                    args[2] = -1;
                }
            }
            else if (OperationID < 23) // beq blt
            {
                for (int count = 0; count < 2; count++)
                {
                    RemoveSpaces(CurrentInstruction);
                    FindRegister(count);
                    RemoveSpaces(CurrentInstruction);
                    assertRemoveComma();
                }
                RemoveSpaces(CurrentInstruction);
                string tempString = findLabel();
                bool   found      = false;
                for (j = 0; j < LabelTable.Count; j++)
                {
                    if (tempString == LabelTable[j].Label)
                    {
                        found   = true;
                        args[2] = LabelTable[j].Address;
                        break;
                    }
                }
                if (!found)
                {
                    gui.ReportError("Error: invalid label");
                    return(-2);
                }
            }
            else if (OperationID < 26) // j bclt bclf
            {
                RemoveSpaces(CurrentInstruction);
                bool   found      = false;
                string tempString = findLabel();
                for (j = 0; j < LabelTable.Count; j++)
                {
                    if (tempString == LabelTable[j].Label)
                    {
                        found   = true;
                        args[0] = LabelTable[j].Address;
                    }
                }
                if (!found)
                {
                    gui.ReportError("Error: invalid label");
                    return(-2);
                }
            }
            else if (OperationID < 28) // mflo mfhi
            {
                RemoveSpaces(CurrentInstruction);
                if (!FindRegister(0))
                {
                    return(-2);
                }
                if (!OnlySpaces(0, CurrentInstruction.Length, CurrentInstruction))
                {
                    return(-2);
                }
            }

            return(OperationID);
        }