14

Is it possible to convert Python programs to a Microprocessor standard assembly language like IEEE-694? The assembly syntax is close to this one or this other one: http://www.ethicalhacker.net/content/view/152/2/

Community
  • 1
  • 1
Academia
  • 3,984
  • 6
  • 32
  • 49

3 Answers3

16

Compile python to C, then use a C compiler of your choice to get it down to assembly.

Alternatively, use PyPy, specifying LLVM as the target, and use the LLVM Static Compiler to yield assembly language for your target architecture.

Community
  • 1
  • 1
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
7

Not in the same way as C, FORTRAN, COBOL, etc. Languages that support lambda calculus or automatic memory management cannot be compiled directly to assembly. An interpreter can, however, be provided in microcode or in a bootstrap program to bridge the gap and allow "compiled" Python, LISP, etc. (Some operations, such as garbage collection, are still carried out within the embedded interpreter packaged into the compiled binary.)

wberry
  • 18,519
  • 8
  • 53
  • 85
  • Thanks for inf. Actually,I'm thinking in compiling python to C, then disassembling with GDB. But still trying to find something better. – Academia May 18 '12 at 23:28
  • Actually that is a common misconception, common lisp has been a compiled language forever, and it even provides the disassemble function. Check http://wiki.c2.com/?LispSucksInAssembly , some Lisps like corman lisp directly loaded assembly into memory without any other representation. – SerialDev Sep 21 '18 at 11:34
  • @SerialDev I meant that a garbage collector is required for full support for functional programming with closures. A simple fixed point combinator cannot work without one. This is not a LISP thing, it's a theoretical outcome. A subset of LISP that did not allow fixed point functions could be compiled without embedded GC, or embedded GC could be included in a compiled binary to close the gap. And including compiled libraries into LISP runtime is perfectly reasonable, and supported in Python as well. – wberry Sep 21 '18 at 12:34
5

Since Python is a dynamically typed language, this would only be possible if the assembly program would use the runtime environment / library of Python to dynamically get objects.

So it would only be possible with some overhead.

But there is RPython from the PyPy project. It is a restricted subset of the Python language (it is not longer dynamically typed and lacks most modules from Python's standard library). RPython programs can be translated to machine code (AFAIK it generates C code as a intermediate code).

Python itself generates a intermediate code for it's virtual machine. If you want to have a look at this code, use the dis module from the Python standard library. This generates a assembly-like representation of your Python function. Keep in mind that a "real" microprocessor would not be able to use this and that the result might change with the Python version you are using.

  • 1
    I want to get a very basic and low-level assembly (without macros). I won't run it on a real microprocessor. – Academia May 18 '12 at 22:29