flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > High Level Languages > [Tutorial] Using FASM to create python modules

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
[Tutorial] Using FASM to create python modules
Hello!
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:

Code:

; TESTDLL.DLL

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

section '.code' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

; void showMessage(char * msg);

proc showMessagemsg
     invoke MessageBox,0,[msg],[msg],0
ret
endp

proc addnumsx : DWORDy : DWORD
     mov eax, [x]
     add eax, [y]
     ret
endp

section '.idata' import data readable writeable

  library user,'USER32.DLL'

  import user,\
         MessageBox,'MessageBoxA'

section '.edata' export data readable

  export 'testDLL.DLL',\
         showMessage,'_showMessage',\
         addnums,'_addnums'

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:

Code:

fasmmodule.py
from ctypes import *

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

def showMessage(n):
    mydll._showMessage(n)

def addnums(xy):
    return mydll._addnums(xy)




3) Now you can use that function just like you do with any Python Module:

Code:

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
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
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
Wink


Description:
Download
Filename: PyConsole.rar
Filesize: 121.25 KB
Downloaded: 581 Time(s)


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



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
Rahsennor



Joined: 07 Jul 2007
Posts: 54
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
DVicthor



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:

Code:

def bind(name):
    mydll = windll.LoadLibrary('testDLL.dll')
    if hasattr(mydll,name):
        func = getattr(mydll,name)
        return func
    else:
        raise AttributeError("Function '%s' does not exist in mydll!" % name)



And then bind a function like:

Code:

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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.