shar
shar

Reputation: 2078

Getting Assembly code for Python programs

I was just wondering if there is any way of generating the assembly code equivalent to a python program. Something similar to the GCC's -S option which would give assembly code for C programs.

Upvotes: 0

Views: 352

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798616

Absolutely. But you don't need to invoke it from outside.

>>> import dis
>>> def foo():
...   a = b + 2
...   print bar
...   baz()
... 
>>> dis.dis(foo)
  2           0 LOAD_GLOBAL              0 (b)
              3 LOAD_CONST               1 (2)
              6 BINARY_ADD          
              7 STORE_FAST               0 (a)

  3          10 LOAD_GLOBAL              1 (bar)
             13 PRINT_ITEM          
             14 PRINT_NEWLINE       

  4          15 LOAD_GLOBAL              2 (baz)
             18 CALL_FUNCTION            0
             21 POP_TOP             
             22 LOAD_CONST               0 (None)
             25 RETURN_VALUE        

Upvotes: 4

Related Questions