Giter VIP home page Giter VIP logo

reloaded.assembler's Introduction

Project Reloaded: Assembler Library



x86 Assembly is like IKEA Furniture

Coverage NuGet

Introduction

Reloaded.Assembler is a minimal .NET wrapper around the simple, easy to use Flat Assembler written by Tomasz Grysztar.

It combines the standard tried and tested FASM DLL and a and the recent experimental official FASMX64 DLL to provide JIT, on the fly assembly of user supplied mnemonics inside x86 and x64 programs.

Getting Started

To get started, install the package from NuGet and simply create a new instance of the Assembler class from the Reloaded.Assembler namespace:

var assembler = new Assembler();

And, uh... well... that's it.

From there you can call GetVersion() to retrieve the version of FASM assembler that the wrapper wraps around and assemble mnemonics with Assemble().

Example

var asm = new Assembler();
string[] mnemonics = new[]
{
    "use32",
    "jmp dword [0x123456]"
};
byte[] actual = asm.Assemble(mnemonics);
// Result: 0xFF, 0x25, 0x56, 0x34, 0x12, 0x00

Just don't forget to dispose the assembler when you're done 😉,

assembler.Dispose();

Small Tip

If the assembly operations fail, the wrapper library will throw an exception with a summary of the error.

You can obtain a slightly more detailed versions of the exceptions by catching them explicitly and checking their properties.

try { asm.Assemble(mnemonics); }
catch (FasmException ex)
{
    // Assembler result (e.g. Error, OutOfMemory) : ex.Result
    // Original text given to the assembler: ex.Mnemonics
    // Line of text error occured in: ex.Line
    // The error itself: ex.ErrorCode
}

Reloaded Assembler Compared to FASM.NET

Reloaded.Assembler is not the only standalone library that exposes FASM to the world of .NET. For a while now, there has been another wrapper worth mentioning willing to fulfill the same purpose.

Below is a quick list of differences you should expect when using Reloaded.Assembler as opposed to FASM.NET; and some of the reasons why I decided to write this library

Advantages

  • Does not require the Visual C++ Runtime to operate.
  • Can be used in both x64 and x86 applications vs only x86.
  • No memory allocation on each assembly request, reuses the same buffers resulting in better performance.

Other Differences

  • Reloaded.Assembler is written in pure C#, FASM.NET is written in C++/CLI.
  • Reloaded.Assembler has a slightly more minimal interface.

Misc Notes

Version 1.0.0 of the library uses custom modified FASMX64. (Official version at the time had a bug in DLLEntryPoint that prevented it from loading).

Version 1.0.1 and onward use the official FASMX64 DLL.

Other Links

Flat Assembler forums post : https://board.flatassembler.net/topic.php?p=207558#207558

This post briefly describes the changes I made to the experimental FASMX64 DLL for version 1.0.0 of this library that made it consumable from C# (and likely other high level languages).

Contributions

As with the standard for all of the Reloaded-Project, repositories; contributions are very welcome and encouraged.

Feel free to implement new features, make bug fixes or suggestions so long as they are accompanied by an issue with a clear description of the pull request 😉.

reloaded.assembler's People

Contributors

sewer56 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

reloaded.assembler's Issues

Linux support

Hi!

First of all, thanks for your amazing work! It's really nice to be able to run an assembler directly in C# code! :D

Second, I'd like to know if Linux support is planned. That is, detecting the OS having a FASM binary for that instead of just assuming the OS being Windows. It'd be a really nice touch in my opinion.

GetExecutingDLLDirectory fails when loaded via LoadFromStream

The calls to GetExecutingDLLDirectory fail with the following exception:

System.ArgumentNullException: Value cannot be null. (Parameter 'path')
   at System.IO.Directory.GetParent(String path)

when the reloaded.Assembler is loaded with LoadFromStream

I am using LoadFromStream to prevent file locking to allow me to compile new versions and reload them.

Is it possible to rely on the current AssemblyLoadContext to find the given DLL before defaulting to the current method?

"Object reference not set" when using include

In the normal fasm.exe it is no problem to use statements like:
include 'win64a.inc'
This uses the "INCLUDE" environment variable to search the win64a.inc of fasm.

In reloaded, the following error occurs:
at Reloaded.Assembler.Definitions.FasmState.GetLineHeader()
at Reloaded.Assembler.Assembler.Assemble(String mnemonics, UInt16 passLimit)

Has anyone ever did an include in reloaded.assembler?

Conflict with Reloaded.Memory.Sigscan

If i use Sigcan together with Assembler, the sigscan would throw error like below

            Scanner Scans = new Scanner(Client.Proc, Client.Proc.MainModule);
            var scanResult = Scans.FindPattern_Compiled(pattern).Offset;


System.TypeLoadException:“Method 'FindPattern_Avx2' in type 'Reloaded.Memory.Sigscan.Scanner' from assembly 'Reloaded.Memory.Sigscan, Version=3.1.4.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.”

Keep getting exception when creating new assembler

Var asm = new Assembler();

Keeps throwing the following error:

System.ArgumentNullException: Value cannot be null parameter name: ptr

I installed from the NuGet Package. Do you have any ideas what would cause this?

Help Needed

I wrote a class (90% copied from yours) that converts asm string code to byte array using Fasm.

I am able to convert the code below when I compile for x86 whereas if I do it for x64 then it throws a null reference exception at certain place in the class, though the code is same and just platform was switched (I can share the class if needed).

Could it be related to the way memory is allocated in the process (which is allocated using VirtualAllocEx)...?

string[] testAsmCode = new string[]
{
"push ebp",
"mov eax, [ebp+8]",
"mov edx, [ebp+12]",
"add eax, edx",
"pop ebp",
"ret"
};

AsmToByteArray(testAsmCode, out byte[] asmBytes);

Any help would be appreciated...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.