Jump to content

Python to assembly, is this possible?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Symbolix

Symbolix

    Newbie

  • Members
  • Pip
  • 8 posts
Hi,
I decided to dive into the world of programming. I have been using MEL (Autodesk Maya scripting language) and Python as a scripting language recently. So I liked the idea of programming. So I decided to start exploring python and learn it a bit more in depth.

However, I have an old obsession that has been around since the day I got my first computer in the beginning of the 90's and it is "Assembly". Back in those days i did not have an internet connection or any of the remote resources to extend my knowledge so I ended up banging my head on the monitor of my 80286 PC and eventually give it up and grow up :)

As I started to build my Python dev environment on my Mac, I realised that Assembler is still around (not surprised) and that even it exist almost as a default programming tool under OSX, as NASM. Anyway... it it still complicated and ofcourse very low level. But still... if compiling Python code means:

Python -> C++ -> Assembler -> Machine code

Is it possible to see how that Assembler code looks like?

Will something like:
print "Hello world!"

Look like:
section .text				;section declaration


			;we must export the entry point to the ELF linker or

    global _start	;loader. They conventionally recognize _start as their

			;entry point. Use ld -e foo to override the default.


_start:


;write our string to stdout


        mov     edx,len ;third argument: message length

        mov     ecx,msg ;second argument: pointer to message to write

        mov     ebx,1   ;first argument: file handle (stdout)

        mov     eax,4   ;system call number (sys_write)

        int     0x80	;call kernel


;and exit


	mov	ebx,0	;first syscall argument: exit code

        mov     eax,1   ;system call number (sys_exit)

        int     0x80	;call kernel


section .data				;section declaration


msg     db      "Hello, world!",0xa	;our dear string

len     equ     $ - msg                 ;length of our dear string


Thanks.

Edited by Symbolix, 10 May 2010 - 07:51 AM.


#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Python is an interpreted language. It is not compiled into machine code, but to "byte code", interpreted at runtime by an interpreter. So that would be difficult. There are however some projects going on, working on compiling Python code into machine code (ShedSkin, I think one is called, there are probably more). Last time I checked, those were quite imperfect, though.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
Symbolix

Symbolix

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks marwex89,
I have looked at ShedSkin, did not mean anything to me :) Sorry. Total newbie.

However there are things like py2exe, py2bin... are not these converting the python to some kind of a final code so it can run on OSX or WIN?

#4
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I believe those only bundles the necessary libraries for execution into a package which is then temporarily unpacked before execution. No compiling process (or conversion to a different language) is done as far as I know.
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5
pedro3005

pedro3005

    Newbie

  • Members
  • PipPip
  • 14 posts
There are some programs that can translate Python down to Assembler. They are relatively functional, although some advanced functions of Python may not work. I can cite Pyastra as one:
Pyastra: python assembler translator
Good luck :)