Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: harold on July 17, 2011, 02:16:19 pm

Title: SharpAsm - use assembly directly from C#
Post by: harold on July 17, 2011, 02:16:19 pm
I don't know how many people here use C# (not many, going by the Programming Help topics..)
But this might be interesting anyway.

SharpAsm is a small library I wrote that lets you write a function in assembly and use it directly from C#.
It's very easy to use, for example:
Code: [Select]
Asm asm = new Asm(@"
    bsr rax, rcx
    ret");
Log2Delegate fastLog = asm.GetDelegate<Log2Delegate>();
// now use fastLog like any normal delegate
Obviously assembling the code takes a little time. When I use this library, I usually create all asm routines in a static constructor and then keep reusing the same delegate for all instances.

Source (the assembler): http://dl.dropbox.com/u/27035142/SharpAsm/Assembler.cs
Source (executable memory manager): http://dl.dropbox.com/u/27035142/SharpAsm/ExecutableMemory.cs

The assembler uses string manipulation instead of actual parsing, and is kind of kludgy. It also doesn't include all opcodes and doesn't always use the smallest encoding (eg not the special eax forms).

edit: almost all of SSE, SSE2, SSE3, SSSE3 and SSE4.1 is now supported. Just redownload the files. Please report any bugs .. if anyone actually uses this (uhm no)
Title: Re: SharpAsm - use assembly directly from C#
Post by: Juju on July 18, 2011, 01:13:51 am
Cool.

I know you can use asm from C, then compile your C/Asm code as a dll, then use it as unmanaged code in your C# program, but this would be easier.
Title: Re: SharpAsm - use assembly directly from C#
Post by: BlakPilar on July 18, 2011, 01:24:20 am
Ooo, I do, I do! I use C#! It's my favorite programming language! ;D

So this is kind of like a C#-ASM interpreter?
Title: Re: SharpAsm - use assembly directly from C#
Post by: z80man on July 18, 2011, 02:58:55 am
Ooo, I do, I do! I use C#! It's my favorite programming language! ;D

So this is kind of like a C#-ASM interpreter?
Looks more like a way to embed asm code inside of C# programs. I don't normally use C# but this is pretty cool to see. It really helps with speeding up the bottlenecks while keeping the simplicity of the language
Title: Re: SharpAsm - use assembly directly from C#
Post by: harold on July 18, 2011, 02:59:37 pm
To clarify, the code is stored in the executable as a string, and will only be assembled when the constructor of the relevant Asm object is ran. You don't have you use it like that though - it can also take an array of bytes, which saves you the overhead of assembling the code at runtime, but it's less convenient.
Title: Re: SharpAsm - use assembly directly from C#
Post by: harold on April 02, 2012, 08:20:03 am
Huge update: tons of instructions added, mostly vector instructions. Also some bugs fixed, where sometimes you could use instructions with mmx operands when the instruction doesn't support that, and sometimes using 16bit operands would actually crash the assembler. 16bit and 8bit stuff still doesn't really work and I probably won't bother too much with it anyway unless someone asks.
Title: Re: SharpAsm - use assembly directly from C#
Post by: felice on October 27, 2012, 11:29:37 am
Hi,

May I know how to apply SharpAsm directly from c#? I would like to use it for Emotion detection.
Im fresh to ASM and C#. Could you please kindly guide me step by step.


Thank you very much
Title: Re: SharpAsm - use assembly directly from C#
Post by: Scipi on October 28, 2012, 01:52:58 am
In what way are you trying to detect emotion? Most likely, depending upon what you're trying to detect emotion from, say like, text, you could probably do it in C# with reasonable efficiency.
Title: Re: SharpAsm - use assembly directly from C#
Post by: felice on October 29, 2012, 05:29:58 am
Hi,

It's recognizing basic facial expression using ASM in C#.

Im looking for Active shape model in C# (SharpASM). Any idea?

Thanks
Title: Re: SharpAsm - use assembly directly from C#
Post by: Scipi on October 29, 2012, 12:51:32 pm
Well, it seems like you would write the assembly in an Asm object.

https://sourceforge.net/projects/sharpassembler/ (https://sourceforge.net/projects/sharpassembler/)

There's the sourceforge project which includes a readme. It should help. :)