public void CreateContextTest() { BinObjectFileFormat format = new BinObjectFileFormat(); var arch = new X86Architecture(); BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test"); var context = arch.CreateContext(objectFile); Assert.IsNotNull(context); Assert.IsInstanceOf <Context>(context); Assert.AreEqual(objectFile, context.Representation); }
public void Do() { BinObjectFileFormat format = new BinObjectFileFormat(); var arch = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32); BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "helloworld"); Section textSection = objectFile.Sections.AddNew(SectionType.Program); var text = textSection.Contents; text.Add(new Label("main")); text.Add(new Mov(Register.EDX, new Reference("len"))); text.Add(new Mov(Register.ECX, new Reference("str"))); text.Add(new Mov(Register.EBX, 1)); text.Add(new Mov(Register.EAX, 4)); text.Add(new Int(0x80)); text.Add(new Mov(Register.EBX, 0)); text.Add(new Mov(Register.EAX, 1)); text.Add(new Int(0x80)); Section dataSection = objectFile.Sections.AddNew(SectionType.Data); var data = dataSection.Contents; data.Add(new Label("str")); data.Add(new DeclareString("Hello World\n")); data.Add(new Define("len", (context) => { Symbol strSymbol = context.SymbolTable["str"]; return(new SimpleExpression(context.Address - strSymbol.Value)); })); byte[] result = Assemble(objectFile); byte[] expected = new byte[] { 0xBA, 0x0C, 0x00, 0x00, 0x00, // mov EDX, len 0xB9, 0x30, 0x00, 0x00, 0x00, // mov ECX, str 0xBB, 0x01, 0x00, 0x00, 0x00, // mov EBX, 1 0xB8, 0x04, 0x00, 0x00, 0x00, // mov EAX, 4 0xCD, 0x80, // int 0x80 0xBB, 0x00, 0x00, 0x00, 0x00, // mov EBX, 0 0xB8, 0x01, 0x00, 0x00, 0x00, // mov EAX, 1 0xCD, 0x80, // int 0x80 // Padding 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // "Hello World\n" 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x0A }; Assert.AreEqual(expected, result); }
/// <summary> /// Assembles the given instruction. /// </summary> /// <param name="instruction">The <see cref="X86Instruction"/> to assemble.</param> /// <param name="mode">The mode in which to assemble.</param> /// <returns>The bytes representing the assembled instruction.</returns> /// <exception cref="AssemblerException"> /// An assembler exception occurred. /// </exception> private byte[] Assemble(X86Instruction instruction, DataSize mode) { byte[] actual = null; BinObjectFileFormat format = new BinObjectFileFormat(); var arch = new X86Architecture(CpuType.AmdBulldozer, mode); BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test"); Section textSection = objectFile.Sections.AddNew(SectionType.Program); var text = textSection.Contents; text.Add(instruction); using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) { objectFile.Format.CreateAssembler(objectFile).Assemble(writer); actual = ms.ToArray(); } } return(actual); }
public void Do2() { BinObjectFileFormat format = new BinObjectFileFormat(); var arch = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32); BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "helloworld"); Section textSection = objectFile.Sections.AddNew(SectionType.Program); var text = textSection.Contents; text.Add(new Label("main")); text.Add(new Mov(Register.EDX, new Reference("len"))); text.Add(new Mov(Register.ECX, new Reference("str"))); text.Add(new Mov(Register.EBX, 1)); text.Add(new Mov(Register.EAX, 4)); text.Add(new Int(0x80)); text.Add(new Mov(Register.EBX, 0)); text.Add(new Mov(Register.EAX, 1)); text.Add(new Int(0x80)); Section dataSection = objectFile.Sections.AddNew(SectionType.Data); var data = dataSection.Contents; data.Add(new Label("str")); data.Add(new DeclareString("Hello World\n")); data.Add(new Define("len", (context) => { Symbol strSymbol = context.SymbolTable["str"]; return(new SimpleExpression(context.Address - strSymbol.Value)); })); using (FileStream fs = File.Create("helloworld.bin")) using (BinaryWriter writer = new BinaryWriter(fs)) objectFile.Format.CreateAssembler(objectFile).Assemble(writer); }
/// <summary> /// Assembles the given instruction. /// </summary> /// <param name="instruction">The <see cref="X86Instruction"/> instance to test.</param> /// <param name="nasmInstruction">The NASM string representation of the same instruction.</param> /// <param name="mode">The mode (16-bit, 32-bit or 64-bit) to use.</param> /// <returns>A (expected, actual) tuple.</returns> private Tuple <byte[], byte[]> AssembleInstruction(X86Instruction instruction, string nasmInstruction, DataSize mode) { #region Contract if (!Enum.IsDefined(typeof(DataSize), mode)) { throw new InvalidEnumArgumentException("mode", (int)mode, typeof(DataSize)); } if (mode != DataSize.Bit16 && mode != DataSize.Bit32 && mode != DataSize.Bit64) { throw new ArgumentException(null, "mode"); } #endregion // Assemble the NASM instruction. byte[] expected = null; if (nasmInstruction != null) { StringBuilder sb = new StringBuilder(); switch (mode) { case DataSize.Bit16: sb.AppendLine("[BITS 16]"); break; case DataSize.Bit32: sb.AppendLine("[BITS 32]"); break; case DataSize.Bit64: sb.AppendLine("[BITS 64]"); break; default: throw new NotSupportedException(); } sb.AppendLine(nasmInstruction); string feedback; expected = RunAssembler(sb.ToString(), out feedback); if (feedback != null && feedback.Length > 0) { Console.WriteLine("Assembler feedback:"); Console.WriteLine(feedback); } } // Assemble the SharpAssembler instruction. byte[] actual = null; if (instruction != null) { BinObjectFileFormat format = new BinObjectFileFormat(); var arch = new X86Architecture(CpuType.AmdBulldozer, mode); BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test"); Section textSection = objectFile.Sections.AddNew(SectionType.Program); var text = textSection.Contents; text.Add(instruction); try { using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) { objectFile.Format.CreateAssembler(objectFile).Assemble(writer); actual = ms.ToArray(); } } } catch (AssemblerException ex) { Console.WriteLine(ex); actual = null; } } return(new Tuple <byte[], byte[]>(expected, actual)); }