flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > [Tutorial] Using FASM to create python modules

Thread Post new topic Reply to topic

Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
I've being playing with Python, and I have to say it's a wonderful language!
It really makes life easier.

Now, I'm here to show you how to use FASM to create new modules for using in Python. Razz

1) The first step is to create a DLL in FASM:

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE

; void showMessage(char * msg);

proc showMessage, msg
     invoke MessageBox,0,[msg],[msg],0

proc addnums, x : DWORD, y : DWORD
     mov eax, [x]
     add eax, [y]

section '.idata' import data readable writeable

  library user,'USER32.DLL'

  import user,\

section '.edata' export data readable

  export 'testDLL.DLL',\

section '.reloc' fixups data discardable

As you can see, it's pretty easy to do. You can just modify the template that comes in \examples folder.

2) Now we can wrap the calling of that function in a nice python module (you can wrap as many functions as you need):
This is python code:
# fasmmodule.py
from ctypes import *

mydll = windll.LoadLibrary("testDLL.dll")

def showMessage(n):

def addnums(x, y):
    return mydll._addnums(x, y)

3) Now you can use that function just like you do with any Python Module:
# test.py
from fasmmodule import *

showMessage("Hello world!")

print addnums(2,3)

There you go! Use the power of ASM in a easy and syntax-clear scripting language known as Python!
Just remember to put the DLL and the Module on your path or script directory!
You can even generate the EXE with py2exe to distribute your application.

Feel free to leave comments!
And I hope you like my simple tutorial.

EDIT: Added addnums function example. Should be enough if you want to do you complex calculus in FASM and use it your Python program. I'll add more data passing soon.

Last edited by OzzY on 07 May 2007, 18:45; edited 1 time in total
Post 07 May 2007, 18:15
View user's profile Send private message Reply with quote
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
maybe you could experiment with more data passing in/out of ASM DLL. For example some structures
Post 07 May 2007, 18:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Nikolay Petrov

Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov

Filename: PyConsole.rar
Filesize: 121.25 KB
Downloaded: 852 Time(s)

Post 20 May 2007, 12:19
View user's profile Send private message Reply with quote

Joined: 09 Jul 2007
Posts: 6
I been doing very similar thing with with "pyFasm" library in which you can using inline fasm code in python ... verry neat but still buggy and incomplete and works only in linux
Post 09 Jul 2007, 05:10
View user's profile Send private message Reply with quote

Joined: 07 Jul 2007
Posts: 61
I've been doing the same thing (Accessing a FASM DLL) for Lua. It's almost exactly the same, only you have to access the Lua stack explicitly. This looks much easier.

1st post! Very Happy
Post 09 Jul 2007, 08:35
View user's profile Send private message Reply with quote

Joined: 13 Sep 2013
Posts: 3
Location: Nigeria
Instead of creating a python function for all the exported functions in the DLL, you could just:
def bind(name):
    mydll = windll.LoadLibrary('testDLL.dll')
    if hasattr(mydll,name):
        func = getattr(mydll,name)
        return func
        raise AttributeError("Function '%s' does not exist in mydll!" % name)

And then bind a function like:
showMessage = bind('_showMessage')
addnums = bind('_addnums')

It makes your code cleaner and saves you some time that would be wasted typing.

Code until your brain explodes! Very Happy
Post 16 Sep 2013, 16:38
View user's profile Send private message 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 GitHub, YouTube, Twitter.

Website powered by rwasa.