示例#1
0
        public void ReplaceAddressWithSymbol(int addressToReplace, string symbolName, bool extendSearchToAllNumbers)
        {
            if (Variables.ContainsKey(symbolName))
            {
                if (Variables[symbolName].GetValue(Variables, null) != addressToReplace)
                {
                    throw new Exception("Symbol " + symbolName + " already has a different meaning");
                }
            }
            else
            {
                Variables[symbolName] = new NumberOperand(addressToReplace);
            }
            foreach (ProgramLine line in Lines)
            {
                if (line.Type == ProgramLineType.OpCodeInstruction)
                {
                    if (line.InstructionType.Param1Type == AddressingMode.Extended ||
                        line.InstructionType.Param1Type == AddressingMode.Relative ||
                        (extendSearchToAllNumbers && line.InstructionType.Param1Type == AddressingMode.Immediate16))
                    {
                        if (line.OpCodeParameters[0].NumberExpression.GetValue(Variables, line) == addressToReplace)
                        {
                            ReplaceAddressWithSymbolInProgramLine(line, 0, line.InstructionType.Param1Type == AddressingMode.Relative, symbolName);
                        }
                    }
                    if (line.InstructionType.Param2Type == AddressingMode.Extended ||
                        line.InstructionType.Param2Type == AddressingMode.Relative ||
                        (extendSearchToAllNumbers && line.InstructionType.Param2Type == AddressingMode.Immediate16))
                    {
                        if (line.OpCodeParameters[1].NumberExpression.GetValue(Variables, line) == addressToReplace)
                        {
                            ReplaceAddressWithSymbolInProgramLine(line, 1, line.InstructionType.Param2Type == AddressingMode.Relative, symbolName);
                        }
                    }
                }
                else if (extendSearchToAllNumbers && line.Type == ProgramLineType.AssemblerDirective)
                {
                    if (line.DirectiveName == "DEFW" && line.DirectiveParameters[0].NumberExpression.GetValue(Variables, line) == addressToReplace)
                    {
                        // Parse new line from text
                        string newDirectiveText = line.DirectiveNameToken.TextWithLeadingWhiteSpace;
                        if (line.LabelToken != null)
                        {
                            newDirectiveText = line.LabelToken.TextWithLeadingWhiteSpace + newDirectiveText;
                        }
                        if (line.DirectiveParameters[0].Tokens[0].LeadingWhiteSpace != null)
                        {
                            newDirectiveText += line.DirectiveParameters[0].Tokens[0].LeadingWhiteSpace.Text;
                        }
                        else
                        {
                            newDirectiveText += " ";
                        }
                        newDirectiveText += symbolName;
                        ProgramLine newDirectiveLine = Assembler.ParseProgramLine(newDirectiveText);

                        // Copy textual representation to the original program Line
                        line.CopyTextualRepresentationFrom(newDirectiveLine);
                    }
                }
            }
        }
        public void ReplaceAddressWithSymbol(int addressToReplace, string symbolName, bool extendSearchToAllNumbers)
        {
            if (Variables.ContainsKey(symbolName))
            {
                if (Variables[symbolName].GetValue(Variables, null) != addressToReplace)
                {
                    throw new Exception("Symbol " + symbolName + " already has a different meaning");
                }

            }
            else
            {
                Variables[symbolName] = new NumberOperand(addressToReplace);
            }
            foreach (ProgramLine line in Lines)
            {
                if (line.Type == ProgramLineType.OpCodeInstruction)
                {
                    if (line.InstructionType.Param1Type == AddressingMode.Extended ||
                        line.InstructionType.Param1Type == AddressingMode.Relative ||
                        (extendSearchToAllNumbers && line.InstructionType.Param1Type == AddressingMode.Immediate16))
                    {
                        if (line.OpCodeParameters[0].NumberExpression.GetValue(Variables, line) == addressToReplace)
                        {
                            ReplaceAddressWithSymbolInProgramLine(line, 0, line.InstructionType.Param1Type == AddressingMode.Relative, symbolName);
                        }
                    }
                    if (line.InstructionType.Param2Type == AddressingMode.Extended ||
                        line.InstructionType.Param2Type == AddressingMode.Relative ||
                        (extendSearchToAllNumbers && line.InstructionType.Param2Type == AddressingMode.Immediate16))
                    {
                        if (line.OpCodeParameters[1].NumberExpression.GetValue(Variables, line) == addressToReplace)
                        {
                            ReplaceAddressWithSymbolInProgramLine(line, 1, line.InstructionType.Param2Type == AddressingMode.Relative, symbolName);
                        }
                    }
                }
                else if(extendSearchToAllNumbers && line.Type == ProgramLineType.AssemblerDirective)
                {
                    if (line.DirectiveName == "DEFW" && line.DirectiveParameters[0].NumberExpression.GetValue(Variables, line) == addressToReplace)
                    {
                        // Parse new line from text
                        string newDirectiveText = line.DirectiveNameToken.TextWithLeadingWhiteSpace;
                        if (line.LabelToken != null)
                        {
                            newDirectiveText = line.LabelToken.TextWithLeadingWhiteSpace + newDirectiveText;
                        }
                        if (line.DirectiveParameters[0].Tokens[0].LeadingWhiteSpace != null)
                        {
                            newDirectiveText += line.DirectiveParameters[0].Tokens[0].LeadingWhiteSpace.Text;
                        }
                        else
                        {
                            newDirectiveText += " ";
                        }
                        newDirectiveText += symbolName;
                        ProgramLine newDirectiveLine = Assembler.ParseProgramLine(newDirectiveText);

                        // Copy textual representation to the original program Line
                        line.CopyTextualRepresentationFrom(newDirectiveLine);
                    }
                }
            }
        }