Translates instructions to Intel ASM syntax
Inheritance: Translator
Ejemplo n.º 1
0
        private static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Mosa.Tool.Disassembler.Intel.Disassembler [www.mosa-project.org]");
            Console.WriteLine("Copyright 2016. New BSD License.");
            Console.WriteLine("Written by Phil Garcia ([email protected])");
            Console.WriteLine();

            if (args.Length < 2)
            {
                Console.WriteLine("Usage: Disassembler -offset <offset> -address <address> -o <output file> <source file>");
                Console.Error.WriteLine("ERROR: Missing arguments");
                return -1;
            }

            try
            {
                var options = new Options();
                options.LoadArguments(args);

                // Need a new instance of translator every time as they aren't thread safe
                var translator = new IntelTranslator();

                // Configure the translator to output instruction addresses and instruction binary as hex
                translator.IncludeAddress = true;
                translator.IncludeBinary = true;

                var code2 = File.ReadAllBytes(options.InputFile);

                var code = new byte[code2.Length];

                for (ulong i = options.FileOffset; i < (ulong)code2.Length; i++)
                {
                    code[i - options.FileOffset] = code2[i];
                }

                //using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, options.StartingAddress, true, Vendor.Any, options.FileOffset))
                using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, options.StartingAddress, true, Vendor.Any))
                {
                    using (var dest = File.CreateText(options.OutputFile))
                    {
                        foreach (var instruction in disasm.Disassemble())
                        {
                            var inst = translator.Translate(instruction);
                            dest.WriteLine(inst);

                            if (options.Length != 0 && instruction.PC > options.StartingAddress + options.Length)
                                break;
                        }
                    }
                }

                return 0;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception: {0}", e.ToString());
                return -1;
            }
        }
Ejemplo n.º 2
0
        private void GenerateASMFile()
        {
            // Need a new instance of translator every time as they aren't thread safe
            var translator = new IntelTranslator();

            // Configure the translator to output instruction addresses and instruction binary as hex
            translator.IncludeAddress = true;
            translator.IncludeBinary = true;

            var asmfile = Path.Combine(Options.DestinationDirectory, Path.GetFileNameWithoutExtension(Options.SourceFile) + ".asm");

            var textSection = Linker.LinkerSections[(int)SectionKind.Text];

            var map = new Dictionary<ulong, string>();

            foreach (var symbol in Linker.Symbols)
            {
                if (map.ContainsKey(symbol.VirtualAddress))
                    continue;

                map.Add(symbol.VirtualAddress, symbol.Name);
            }

            uint multibootHeaderLength = MultibootHeaderLength;
            ulong startingAddress = textSection.VirtualAddress + multibootHeaderLength;
            uint fileOffset = textSection.FileOffset + multibootHeaderLength;
            uint length = textSection.Size;

            var code2 = File.ReadAllBytes(CompiledFile);

            var code = new byte[code2.Length];

            for (ulong i = fileOffset; i < (ulong)code2.Length; i++)
            {
                code[i - fileOffset] = code2[i];
            }

            using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, startingAddress, true, Vendor.Any))
            {
                using (var dest = File.CreateText(asmfile))
                {
                    if (map.ContainsKey(startingAddress))
                    {
                        dest.WriteLine("; " + map[startingAddress]);
                    }

                    foreach (var instruction in disasm.Disassemble())
                    {
                        var inst = translator.Translate(instruction);
                        dest.WriteLine(inst);

                        if (map.ContainsKey(instruction.PC))
                        {
                            dest.WriteLine("; " + map[instruction.PC]);
                        }

                        if (instruction.PC > startingAddress + length)
                            break;
                    }
                }
            }
        }