flat assembler
Message board for the users of flat assembler.

Index > Main > Use FASM like a function

Author
Thread Post new topic Reply to topic
cjacobi



Joined: 28 Jan 2013
Posts: 6
Location: Palo Alto
cjacobi
I wonder whether somebody has made a version of the flat assembler callable as a function which takes the input source and gives the output binary directly from memory instead of using files. (X86 - Win32)

My application (C#, but that is not important) calls the flat assembler very frequent with smallish inputs. Creating files and explicitly loading the assembler has a devastating performance impact.

Chris

Chris Jacobi
jacobi@acm.org
Thats what I'm using it for: www.whitehawksoftware.com



[url][/url]
Post 28 Jan 2013, 19:41
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
cjacobi,

FASMDLL, perhaps?
Post 28 Jan 2013, 21:22
View user's profile Send private message Reply with quote
cjacobi



Joined: 28 Jan 2013
Posts: 6
Location: Palo Alto
cjacobi
Answering myself: I have seen a solution. I guess it is one more case that one only has to ask to find a solution.

See
flat assembler > Compiler Internals > fasm as DLL
http://board.flatassembler.net/topic.php?t=6239

Chris
Post 28 Jan 2013, 21:24
View user's profile Send private message Visit poster's website Reply with quote
cjacobi



Joined: 28 Jan 2013
Posts: 6
Location: Palo Alto
cjacobi
baldr wrote:
cjacobi,

FASMDLL, perhaps?


YES
thank you
Chris
Post 28 Jan 2013, 21:27
View user's profile Send private message Visit poster's website Reply with quote
cjacobi



Joined: 28 Jan 2013
Posts: 6
Location: Palo Alto
cjacobi
FASMDLL is really working great for me and very fast.
I'm attaching my little test program in C#, just for the case somebody else could use FASMDLL that way.

As I can't find the attachment, I simply add it to the post below.

Code:

// (C) 2013 White Hawk Software, Chris Jacobi
// Very rudimentary call of fasm from C#; needs to be embellished before real use.
// ...The native FASM dll goes into this applications bin/Debug directory.
// Permission to use or modify given to the FASM community.
   
using sys  = System;
using iops = System.Runtime.InteropServices;

namespace FasmTest
{
    class Program
    {
        
        //fasm_GetVersion()
        //...

        //fasm_Assemble(lpSource,lpMemory,cbMemorySize,nPassesLimit,hDisplayPipe)
        //Single threaded
        [iops.DllImport("FASM.dll")]
        public static extern int fasm_Assemble(
            string lpSource,
            byte[] lpMemory,
            uint   cbMemorySize,
            uint   nPassesLimit = 100,
            uint   hDisplayPipe = 0); //0 is only value which is supported here
            
        
        // Return values from fasm_Assemble
        public const int fasm_ok        = 0;
        public const int fasm_working   = 1;
        public const int fasm_error     = 2;
        public const int fasm_invalidParameter          = -1;
        public const int fasm_outOfMemory               = -2;
        public const int fasm_stackOverflow             = -3;
        public const int fasm_sourceNotFound            = -4;
        public const int fasm_unexpectedEndOfSource     = -5;
        public const int fasm_cannotGenerateCode            = -6;
        public const int fasm_formatLimitationsExceeded = -7;
        public const int fasm_writeFailed                       = -8;
  
        
        // errorcodes from fasm_Assemble
        // ...
        
        
        
        
        static byte[]  gMemory      = null;
        static uint    gStartAddr   = 0;
        
        const  int     memorySize   = 2000;
        static string  gInputString = "use32\n    push eax\n      retn\n";
        
        
        public static uint read4Bytes(ref byte[] buff, uint idx) {
            return ((uint)buff[idx]                +
                    (uint)buff[idx + 1] * 256      +
                    (uint)buff[idx + 2] * 0x10000  +
                    (uint)buff[idx + 3] * 0x1000000);   
        } //read4Bytes

        
        static void fasmTest() {
            if (gMemory == null ) {
                //Allocate memory
                gMemory = new byte[memorySize];
                //Pin memory
                iops.GCHandle handle = iops.GCHandle.Alloc(gMemory, iops.GCHandleType.Pinned);  //Won't be unpinned.
                //Compute real pointer
                int iAddr  = handle.AddrOfPinnedObject().ToInt32();
                gStartAddr = unchecked((uint) iAddr);
                sys.Console.WriteLine("Buffer start: 0x{0:x}", gStartAddr);
            }
            
            int returnVal = fasm_Assemble(
                lpSource: gInputString,
                lpMemory: gMemory,
                cbMemorySize: memorySize,
                nPassesLimit: 100,
                hDisplayPipe: 0);           
            uint cond   = read4Bytes(ref gMemory, 0);
            uint length = read4Bytes(ref gMemory, 4);
            uint ptr    = read4Bytes(ref gMemory, 8);
            uint index  = unchecked(ptr - gStartAddr);
            
            if (returnVal != 0) {
                sys.Console.WriteLine("Error from fasm_Assemble");
                return;
            }
            sys.Console.WriteLine("Returns:");
            sys.Console.WriteLine("   condition: {0}", cond);
            sys.Console.WriteLine("   ptr: 0x{0:x},   length: {1}", ptr, length);
            sys.Console.WriteLine("   index: {0}", index);
            if (index >= memorySize || index + length >= memorySize) {
                sys.Console.WriteLine("Result from fasm_Assemble looks wrong");
                return;
            }
            for (uint i = 0; i < length; i++) {
                sys.Console.Write("0x{0:x} ", gMemory[index + i]);
                if (i % 8 == 7 && i + 1 != length) {
                    sys.Console.WriteLine();
                }
            }
            sys.Console.WriteLine();
        } //fasmTest
        
        
        public static void Main(string[] args) {
            sys.Console.WriteLine("Calling fasm_Assemble as a DLL");            
            for (int i = 0; i < 100; i++) {
                fasmTest();
            }
            sys.Console.Write("Press any key to continue . . . ");
            sys.Console.ReadKey(true);
        } //Main
        
        
    } //class Program
} //namespace    
Post 31 Jan 2013, 07:52
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.