I recently decided that copy / pasting memory patches to use in my framework was really tedious, so I decided to get FASM to work in C# since i could not be bothered to use fasm_managed, since it is in C++/CLI and therefore the devil.
This is a just a simple .cs file, containing the necessary code to call fasm_assemble from the FASM library (less than 30 lines of code)
The only purpose of this code was to assemble asm to bytes, it does not contain code to write the bytes to memory or call asm procedures, it is all trivial to implement though.
Have a look at it here.
Usage:
Code:
string asm = @"push ebp
mov ebp, esp
mov esp, ebp
pop ebp
ret";
try
{
byte[] bytes = Asm.Assemble(asm);
foreach (var b in bytes)
{
Console.Write(b.ToString("X2") + " ");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
Error examples:
Code:
Assembling failed!
State: FASM_ERROR
Error: FASMERR_ILLEGAL_INSTRUCTION
line: 5
Assembling failed!
State: FASM_ERROR
Error: FASMERR_INVALID_OPERAND
line: 4
Remember to include the FASM library in your output directory, or wherever you decide to call it from, otherwise bad things will happen.
If you would like to get FASM library yourself, you can get it here, remember to change the DllImport according to the name of the assembly.
Credits:
- Shynd for fasm_managed, the C++/CLI wrapper for FASM
- Tomasz Grysztar for FASM
Enjoy.