May 09, 2005
NeoSwiff's Inline Assembly
Not only does NeoSwiff parse C# code to create .swf files, it also doubles as FLASM, allowing you to write inline assembly language that gets compiled directly to .swf bytecode. Here's how...
My last entry was for my little experiment application QuickDefine in which I wanted to integrate mProjector and NeoSwiff. I had a hard time calling the mProjector commands directly from NeoSwiff, and had to write an ActionScript file compiled with MTASC to glue the whole thing together.
Not anymore.
Through the power of NeoSwiff's inline assembler that was recently discovered, I can program at the bytecode level directly from within my class files. C# has a magical "unsafe" keyword that lets me write assembly language inline in a method body. Here's a small example:
/** * Manipulates the stack directly. Note the use * of the unsafe keyword which allows us to use * the __asm keyword. */ private unsafe string[] listFiles(string path) { __asm { push path push 1 // number of arguments push "mFile" getv // get the mFile global object push "listFiles" callm // call mFile.listFiles method } }
I can use the above method like so:
// use mProjector to get some Windows integration functionality // -- call mFile.listFiles via inline assembler code string[] files = listFiles("c:\\"); // iterate over the files and display them in the mProjector // trace viewer foreach (string fileName in files) { // mTrace is another function that call's mApplication.trace // whose output I can see by opening mProjector's trace // viewer tool mTrace(fileName); }
Notice the use of the unsafe keyword, and how the assembly code is placed within the __asm code block. Pretty slick, huh? I had mentioned this to Jesse and he promptly called me a geek.. then I realized that getting excited about inline assembly is something only a geek would do, so I guess the term was fitting.
The whole concept of assembly programming is pretty advanced, but also very powerful because it's so low level. I used to use FLASM to optimize bytecode of a .swf file, but the process was painful. You would compile a .swf in Flash, open it in FLASM and tweak the assembly, then re-assemble with FLASM. If you ever had to re-compile the .swf from Flash you'd lose all of your changes, so you had to save backups and document what you changed to make re-applying the changes easier.
With inline assembly, I get the benefits of optimizing certain portions of code without the pain of having the "extra" FLASM step. The workflow is much improved in this way, and I can bring my .swf bytecode optimizations directly into the C# world.
There will be full documentation published at some point. But, the assembly language has everything you would expect.. gets, sets, branches, jumps, registers, "init object", math operations (add / subtract / increment / etc.), and even labels (so you can jump to a label, as opposed to an address). The power that this brings to the table should make you give NeoSwiff another look once the docs are released. This is a very serious contender, and is shaping up to be an amazing product.
So... what I've been doing is wrapping up the mProjector commands into a NeoSwiff .scl library. Now, I can write code like this:
using mProjector; // ... later on in a method call: mApplication.trace("This is an example message");
Which makes using mProjector as easy in NeoSwiff as it is in ActionScript. The only part I haven't figured out yet how to add event listeners for the events. That is, this doesn't work yet...
mApplication.onSystemTray += new EventHandler(SystemTrayClick);
... but I'm sure there's a way somehow - just haven't discovered it yet. Once that final piece of the puzzle is in place, I'll probably publish my .scl library for others to use to easily integrate mProjector and NeoSwiff.
As an aside, if you program in assembly it's very easy to corrupt the stack. If you return a value from a function you simply push a value on the stack, but you have to be very careful. NeoSwiff provides two commands to help in debugging situations where you may inadvertently corrupt the stack:
System.Diagnostics.Debugger.BeginStackCheck(); the_unsafe_method(); // Raises an exception if the unsafe method has corrupted the stack System.Diagnostics.Debugger.EndStackCheck();
The geek in me loves this stuff...

Comments
I wasn't kidding. Only a true g33k codes Flash in assembly... wtf...
Posted by: JesterXL at May 9, 2005 08:46 AM
Interesting ;)
Posted by: Kenny Bunch at May 9, 2005 09:17 AM
What benfits are there to coding Flash in assembly? I've heard that there are performance gains, and code-protection possibilities, but I've never seen real-world examples of significant gains...
Posted by: Tom Lee at May 9, 2005 12:35 PM
There are many constructs that can be represented at a low level in assembly that can't be represented in a high level, for instance accessing registers directly. Have a look at the Flasm Optimization listing for some other ideas:
http://flasm.sourceforge.net/#optimization
Not only can you make your code more efficient and increase performance, but you can make it impossible to be represented in a high level language... although, in NeoSwiff's case this isn't a problem as the .swf files are automatically obfuscated for you.
Posted by: darron at May 9, 2005 02:21 PM
Inline assembly. Now there's another reason why I'm getting drawn to using Neoswift. Only I asked the GlobFX some questions about it, and they never got back to me. Can anyone here help?
1. Can it be used to write Central applications?
2. Can it be mixed with standard .swfs. loadMovie(), and calls to functions inside the .swf? - or does the obfuscation make this difficult?
3. Can it compile to FlashLite or Flash 6 for Palm PCs?
Posted by: Daniel Freeman at May 9, 2005 05:42 PM
Darron,
Is there a way NeoSwiff can be used to load swf movies and use flash as the UI layer and use C# for the controller / model part of the application?
It would be ideal to be able to use flash as your GUI and then C# / or Java for your application logic.
Designers and UI gurus could come up with awesome flash components / skins for the app and the developers could concentrate on the application functionality.
Let me know, you seem to be the guru on the web when it comes to Flash / C# / Java application integration.
Posted by: corbin at May 12, 2005 12:07 AM