示例#1
0
        private string[] GetASMLinesFromBytes(byte[] bytes, uint pc, bool littleEndian, bool useRegAliases)
        {
            string asmText = GetASMTextFromBytes(bytes, pc, littleEndian, useRegAliases);

            string[] lines = asmText.Split('\n');
            lines = ASMStringHelper.RemoveFromLines(lines, "\r");
            return(lines);
        }
示例#2
0
        public static uint ProcessStartPC(string asm, uint pc)
        {
            // If the ASM starts with a .org statement, use that address as the PC
            string[] lines = asm.Split('\n');
            lines = ASMStringHelper.RemoveFromLines(lines, "\r");

            foreach (string line in lines)
            {
                if (!string.IsNullOrEmpty(line))
                {
                    string             processLine     = ASMStringHelper.RemoveComment(line).ToLower();
                    string[]           parts           = ASMStringHelper.SplitLine(processLine);
                    ASMProcessPCResult processPCResult = ASMPCHelper.ProcessOrg(pc, parts);
                    pc = processPCResult.PC;
                    break;
                }
            }

            return(pc);
        }
示例#3
0
        private string[] GetASMLines(string asm, uint pc, bool littleEndian, bool useRegAliases, bool reEncode = true)
        {
            string asmText = reEncode ? GetASMText(asm, pc, littleEndian, useRegAliases) : asm;

            string[] lines = asmText.Split('\n');
            lines = ASMStringHelper.RemoveFromLines(lines, "\r");

            if (!reEncode)
            {
                List <string> newLines = new List <string>(lines.Length);

                foreach (string line in lines)
                {
                    string newLine = ASMStringHelper.RemoveLeadingBracketBlock(line);
                    newLine = ASMStringHelper.RemoveLeadingSpaces(newLine);
                    newLines.Add(newLine);
                }

                lines = newLines.ToArray();
            }

            return(lines);
        }
示例#4
0
        public ASMEncoderResult EncodeASM(string asm, uint pc, string spacePadding, bool includeAddress, bool littleEndian, bool clearErrorText)
        {
            if (clearErrorText)
            {
                ClearErrorText();
            }

            string[] lines = asm.Split('\n');
            lines = ASMStringHelper.RemoveFromLines(lines, "\r");
            string[] processLines = AddLabelLines(lines);

            string oldErrorText = _errorTextBuilder.ToString();

            EncodeLine[] encodeLines = PreprocessLines(processLines, pc);

            int encodeLineIndex = 0;
            int lineIndex       = 0;

            StringBuilder newTextASMBuilder     = new StringBuilder();
            StringBuilder newTextASMLineBuilder = new StringBuilder();
            StringBuilder encodedASMBuilder     = new StringBuilder();

            List <byte> byteList = new List <byte>();

            if (oldErrorText != _errorTextBuilder.ToString())
            {
                return(new ASMEncoderResult(encodedASMBuilder.ToString(), byteList.ToArray(), asm, _errorTextBuilder.ToString()));
            }

            foreach (string line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    newTextASMBuilder.AppendLine();
                    continue;
                }

                newTextASMLineBuilder.Length = 0;

                string modLine     = ASMStringHelper.RemoveLeadingBracketBlock(line);
                string processLine = ASMStringHelper.RemoveLeadingSpaces(modLine);
                processLine = ASMStringHelper.RemoveComment(processLine).ToLower();
                string[] parts = ASMStringHelper.SplitLine(processLine);

                //pc = ASMPCHelper.ProcessOrg(pc, parts);
                ASMProcessPCResult processPCResult = ASMPCHelper.ProcessOrg(pc, parts);
                pc = processPCResult.PC;
                _errorTextBuilder.Append(processPCResult.ErrorMessage);

                parts = ASMStringHelper.RemoveLabel(parts);

                // If this is an ASM command, pass off line to encoding routine
                EncodingFormat encodingOrNull = FormatHelper.FindFormatByCommand(parts[0]);
                if (encodingOrNull != null)
                {
                    if (includeAddress)
                    {
                        newTextASMLineBuilder.Append("[0x");
                        newTextASMLineBuilder.Append(ASMValueHelper.UnsignedToHex_WithLength(pc, 8));
                        newTextASMLineBuilder.Append("] ");
                    }

                    newTextASMLineBuilder.AppendLine(modLine);

                    EncodeLine eLine = new EncodeLine();

                    if (encodeLines.Length > 0)
                    {
                        eLine = encodeLines[encodeLineIndex];
                    }

                    while ((eLine.LineIndex == lineIndex) && (encodeLineIndex < encodeLines.Length))
                    {
                        encodingOrNull = FormatHelper.FindFormatByCommand(eLine.LineParts[0]);
                        EncodingFormat encoding = encodingOrNull;

                        ASMSingleEncodeResult singleEncodeResult = TryEncodeSingle(eLine.LineParts, encoding, pc, modLine, littleEndian);
                        encodedASMBuilder.Append(spacePadding);
                        encodedASMBuilder.Append(singleEncodeResult.ASMText);
                        encodedASMBuilder.AppendLine();
                        //encodedASMBuilder.Append("\r\n");
                        byteList.AddRange(singleEncodeResult.Bytes);

                        encodeLineIndex++;
                        pc += 4;

                        if (encodeLineIndex < encodeLines.Length)
                        {
                            eLine = encodeLines[encodeLineIndex];
                        }
                    }

                    lineIndex++;
                }
                else
                {
                    if (!string.IsNullOrEmpty(parts[0]))
                    {
                        if ((parts[0] != ".org") && (parts[0] != ".label") && (parts[0] != ".eqv") && (!parts[0].EndsWith(":")))
                        {
                            _errorTextBuilder.AppendLine("WARNING: Ignoring unknown command \"" + parts[0] + "\".");
                        }
                    }
                }

                if (string.IsNullOrEmpty(newTextASMLineBuilder.ToString()))
                {
                    newTextASMLineBuilder.AppendLine(modLine);
                }

                newTextASMBuilder.Append(newTextASMLineBuilder.ToString());
            }

            string newTextASM = newTextASMBuilder.ToString();

            if (newTextASM.EndsWith("\r\n"))
            {
                newTextASMBuilder.Length -= 2;
            }
            else if (newTextASM.EndsWith("\n"))
            {
                newTextASMBuilder.Length -= 1;
            }

            newTextASM = newTextASMBuilder.ToString();
            return(new ASMEncoderResult(encodedASMBuilder.ToString(), byteList.ToArray(), newTextASM, _errorTextBuilder.ToString()));
        }