This project is about a .NET library, written in C#, which allows you to construct a representation of machine code (for example x86-64) using managed objects. The representation can then be assembled by this library into a physical object file such as bin
or elf
.
The inspiration for this project came from managed operating system projects such as SharpOS and Cosmos that have no way to assemble the machine instructions into machine code other than using external tools such as NASM.
You can find the full API documentation at your project homepage.
Using SharpAssembler is really easy. For example, here is the SharpAssembler version of a simple program that prints Hello world
using interrupt 0x80
.
var arch = new Architecture(CpuType.AmdBulldozer, DataSize.Bit32); BinObjectFile objectFile = new BinObjectFile("helloworld", arch); Section text = objectFile.AddNewSection(".text", SectionType.Program); 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 data = objectFile.AddNewSection(".data", SectionType.Data); data.Add(new Label("str")); data.Add(new DeclareString("Hello World\n")); data.Add(new Define("len", (context) => { Symbol strSymbol; context.SymbolTable.TryGetValue("str", out strSymbol); return new SimpleExpression(context.Address - strSymbol.Address); })); using (FileStream fs = File.Create("helloworld.bin")) using (BinaryWriter writer = new BinaryWriter(fs)) objectFile.Assemble(writer);
The above example creates a bin
object file named helloworld.bin
that contains the 'Hello World' program.