1

I have medium amateur skills in Python and I'm beginner in asm and haven't any knowledge of C-language.

I know that python C-extensions must follow specific interface to work fine.

Is this possible to write python extension in pure Assembly with the right interface and full functionality? The second question is would it be efficient enough if case of doing it right?

While googling I haven't found any examples of code or some articles or solutions about this question.

And this ISN'T the question about running asm-code from within Python so it's not duplicate of topics on SO.

Крайст
  • 776
  • 1
  • 9
  • 22
  • 4
    Of course it's *possible*; just produce the same machine code that the C compiler would produce for the Python structures. But why go through that pain? – Martijn Pieters Jan 27 '13 at 10:57
  • @MartijnPieters, I thought that right interface can be implemented from within `asm`-code without intervening steps. I answered below why go through that pain. It's not curiosity - it's something about possibility. – Крайст Jan 27 '13 at 12:16
  • Oh, guys. Maybe It's simpler than writing extensions - to make simple `DLL` in `asm` with some helpful functions and use `Python`'s `ctypes` instead? – Крайст Jan 27 '13 at 12:18

3 Answers3

6

In theory - it is possible.

In practice - it is highly impractical to do so.

There are very very few cases where there is justified usage of Assembly over C, and even if you face such a situation, it is highly unlikely you will be working with Python in that case.

Also note, that the compiler can optimize the C code to extremely efficient assembly. In fact it is highly unlikely that you will hand write assembly and it will be more efficient that the compiler output, unless you are have extremely potent assembly skills, or have been writing assembly all your life..

NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
  • I totally agree with you. When I've got some time to get some skills with other programming language I've chose `asm` just because it's quiet close to the hardware. And I have one project that will make many calculations of floating point numbers, calculate sinuses and cosinuses etc. to make some images processing. More of that it will run on `Django` in the internet. I thought that `asm` can help me with cpu load decreasing on hosting or server. – Крайст Jan 27 '13 at 12:06
  • So you think that I must forget about `asm` and start to learn `C` instead for more practical efficiency? – Крайст Jan 27 '13 at 12:08
  • 2
    Pretty much yes, C will do 99% of what asm can and will do. – NlightNFotis Jan 27 '13 at 12:19
  • Great! Is there any best tutorials for learning `C` after `Python`? – Крайст Jan 27 '13 at 12:20
  • 1
    If you are talking about online tutorials, you can grab the basics of C at http://www.cprogramming.com and for a little bit more applied knowledge go to http://c.learncodethehardway.org/book/ – NlightNFotis Jan 27 '13 at 12:21
  • C is compiled to machine code too, it's going to be just as close to the hardware as the assembly you write. But modern C compilers are going to beat you at writing *efficient* machine code. If you haven't even learned to code in C yet you have, frankly, no hope in hell to best a compiler. – Martijn Pieters Jan 27 '13 at 12:22
  • @MartijnPieters I believe you wanted to write "If you haven't learn to code in ASM yet" in that last part! – NlightNFotis Jan 27 '13 at 12:23
  • 2
    @NlightNFotis: no, I forgot an `even` in there. If the OP hasn't learned C yet, then I think we can safely assume their ASM skills are not going to be impressive either. :-) In Dutch there is a saying: "He has heard the bells ring but doesn't know where to find the clapper"; I fear it applies here. – Martijn Pieters Jan 27 '13 at 12:24
  • Thank you guys. I'm sure that your help saved my day ) `** Gone to learn C` – Крайст Jan 27 '13 at 12:25
  • @MartijnPieters Sorry for being annoying, but I wanted to know what's your opinion in the resources I pointed him to for learning c. – NlightNFotis Jan 27 '13 at 12:35
  • @NlightNFotis: Sorry, I am not going to venture there; we have absolutely no idea about what level of competence the OP has in programming in general for example. And it's been too long for me personally to be able to judge any C learning resource anymore. :-) – Martijn Pieters Jan 27 '13 at 12:38
  • I have school level of math, 3 years of amateur programming in Python (homegrown scripts) and have reading a number of books about programming and hardware (`Structured Computer Organization` by Tanenbaum, `SICP` by MIT, `Discrete Mathematics` by Haggarty) - all on the beginning stage at the moment. – Крайст Jan 27 '13 at 12:45
2

You could write your asm as inline asm inside your c extention, as for efficiency...

Teapot.

Efficiency isn't measured by the choice of language, its measured by how well its implemented and how well its designed.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • Many floating point operations has two bad effects in `Python`: slow speed and accuracy loss. Okay, I've learned base solution from all of the answers: learn `C` - and you'll get more freedom with `Python` and others =) Excuse me, but what is `Teapot` in context of your answer? – Крайст Jan 27 '13 at 12:12
  • 1
    I think you underestimate fp operations in Python; they are still implemented in C in any case. Use `numpy` for intensive operations if you really have a lot of fp transformations to process. – Martijn Pieters Jan 27 '13 at 12:30
  • @MartijnPieters, Sorry, I've forgot about `numpy`. Thanks for remind about that ) – Крайст Jan 27 '13 at 12:38
1

How to do it:

I don't know if you could do it 'purely' in assembly, but: If you make a "proxy class" (lets call it that way) in C that calls the assembly function, and you then write the assembler with the C convention, then, by simply compiling the assembler code:

nasm -felf64 -g -F dwarf assembly_function.asm

and then, using a setup.py file containing:

from distutils.core import setup, Extension
setup(name='assembly_include_name', version='1.0', ext_modules=[Extension('assembly_include_name', ['c_assembly_proxy.c'],extra_objects=["assembly_function.o"])])

you will be able to do what you wanted. Notice that you have to add the parameter "extra_objects" to the Extension constructor in order to tell python to link the assembly code, otherwise it will crash saying that it can't find the function's name.

Why would you do it:

If you want to use SSE instructions (SSE2, SSE3...) regardless the optimization the compiler could make.

Extension api: https://docs.python.org/2/extending/extending.html

disutils.core reference: https://docs.python.org/2/distutils/apiref.html?highlight=distutils.core#module-distutils.core

  • Compilers do generate and optimize code using SSE instructions. – Ross Ridge May 26 '15 at 15:04
  • Realy,Even if i don't use the SSE instructions? I mean, the compiler can determinate whether to use or not SSE instructions? Could you give me some example? it would be intresting. – Esteban Rey May 26 '15 at 16:55
  • 1
    Yes. A fairly trivial example is how most compilers can use the scalar SSE instructions to do basic floating point math. A more sophisticated example is automatic vectorization of loops. Here's a example of the later where a particular version of Visual C++ generated an SSE 4.1 instruction incorrectly: http://stackoverflow.com/questions/25494681/sse-4-instructions-generated-by-visual-studio-2013-update-2-and-update-3 – Ross Ridge May 26 '15 at 17:11