Data Structuresand Algorithms Good OldJava AdvancedInterview Topics Cloud andDatabases Web Designand Development Must-knowTools Good ToKnow

## Python disassembler

Python disassembler converts byte codes to a human-readable representation for analysis.
```
>>> import dis

# Small example
>>> def tmpFunc():
...   x = 2
...   y = 3
...   return x+y
...

# The disassembler produces the output in the following format:
# source line no, the instruction 'address' within the code object, opcode name, arguments passed to the opcode.
>>> dis.dis(tmpFunc)
3 STORE_FAST               0 (x)

9 STORE_FAST               1 (y)

19 RETURN_VALUE

# Slightly bigger example
>>> def dummyFunc():
...    print (2*2)
...    a = 1
...    b = 2
...    if (a < b):
...      print (a, ">", b)
...    else:
...      print (b, ">", a)
...

# source line no, the instruction 'address' within the code object, opcode name, arguments passed to the opcode.
>>> dis.dis (dummyFunc)
6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
9 POP_TOP

13 STORE_FAST               0 (a)

19 STORE_FAST               1 (b)

28 COMPARE_OP               0 (<)
31 POP_JUMP_IF_FALSE       53

46 CALL_FUNCTION            3 (3 positional, 0 keyword pair)
49 POP_TOP
50 JUMP_FORWARD            16 (to 69)

8     >>   53 LOAD_GLOBAL              0 (print)
65 CALL_FUNCTION            3 (3 positional, 0 keyword pair)
68 POP_TOP
72 RETURN_VALUE
>>>

# One can also use show_code to get more code metrics
>>> dis.show_code(tmpFunc)
Name:              tmpFunc
Filename:          <stdin>
Argument count:    0
Kw-only arguments: 0
Number of locals:  2
Stack size:        2
Flags:             OPTIMIZED, NEWLOCALS, NOFREE
Constants:
0: None
1: 2
2: 3
Variable names:
0: x
1: y

```

Serious disassemblers must try help(dis)

## Disassembler opcodes

 ```>>> dis.opmap 'CONTINUE_LOOP': 119 'DUP_TOP_TWO': 5 'ROT_THREE': 3 'DELETE_GLOBAL': 98 'UNARY_INVERT': 15 'RAISE_VARARGS': 130 'DUP_TOP': 4 'BINARY_MULTIPLY': 20 'STORE_MAP': 54 'JUMP_IF_TRUE_OR_POP': 112 'GET_ITER': 68 'UNPACK_SEQUENCE': 92 'YIELD_VALUE': 86 'UNARY_NEGATIVE': 11 'BINARY_POWER': 19 'PRINT_EXPR': 70 'LOAD_BUILD_CLASS': 71 'UNPACK_EX': 94 'COMPARE_OP': 107 'POP_BLOCK': 87 'BINARY_FLOOR_DIVIDE': 26 'INPLACE_SUBTRACT': 56 'STORE_LOCALS': 69 'LOAD_GLOBAL': 116 'BINARY_SUBTRACT': 24 'EXTENDED_ARG': 144 'CALL_FUNCTION_KW': 141 'STORE_DEREF': 137 'IMPORT_STAR': 84 'BINARY_OR': 66 'BUILD_LIST': 103 'INPLACE_OR': 79 'BINARY_ADD': 23 'LIST_APPEND': 145 'DELETE_SUBSCR': 61 'RETURN_VALUE': 83 'SETUP_EXCEPT': 121 'INPLACE_ADD': 55 'INPLACE_LSHIFT': 75 'SETUP_LOOP': 120 'STORE_NAME': 90 'UNARY_NOT':12 'INPLACE_XOR': 78 'INPLACE_POWER': 67 'POP_EXCEPT': 89 'CALL_FUNCTION_VAR_KW': 142 'CALL_FUNCTION_VAR': 140 'UNARY_POSITIVE': 10 'LOAD_DEREF': 136 'NOP': 9 'IMPORT_FROM': 109 'YIELD_FROM': 72 'STORE_SUBSCR': 60 'DELETE_FAST': 126 'INPLACE_FLOOR_DIVIDE': 28 'LOAD_FAST': 124 'STORE_FAST': 125 'IMPORT_NAME': 108 'BINARY_LSHIFT': 62 'BREAK_LOOP': 80 'FOR_ITER': 93 'INPLACE_RSHIFT': 76 'DELETE_NAME': 91 'LOAD_ATTR': 106 'MAP_ADD': 147 'LOAD_CONST': 100 'BUILD_SLICE': 133 'BINARY_MODULO': 22 'MAKE_CLOSURE': 134 'JUMP_ABSOLUTE': 113 'INPLACE_TRUE_DIVIDE': 29 'JUMP_IF _FALSE_OR_POP': 111 'SET_ADD': 146 'LOAD_CLOSURE': 135 'BINARY_AND': 64 'STORE_ATTR': 95 'ROT_TWO': 2 'DELETE_ATTR': 96 'WITH_CLEANUP': 81 'POP_TOP': 1 'BINARY_TRUE_DIVIDE': 27 'INPLACE_MULTIPLY': 57 'LOAD_NAME': 101 'BUILD_SET': 104 'DELETE_DEREF': 138 'BINARY_SUBSCR': 25 'CALL_FUNCTION': 131 'INPLACE_MODULO': 59 'STORE_GLOBAL': 97 'POP_JUMP_IF_FALSE': 114 'BUILD_TUPLE': 102 'SETUP_FINALLY': 122 'END_FINALLY': 88 'POP_JUMP_IF_TRUE': 115 'JUMP_FORWARD': 110 'INPLACE_AND': 77 'MAKE_FUNCTION': 132 'BUILD_MAP': 105 'BINARY_XOR': 65 'SETUP_WITH': 143 'BINARY_RSHIFT': 63 ``` ```>>> [print(x) for x in dis.opname] <0> POP_TOP ROT_TWO ROT_THREE DUP_TOP DUP_TOP_TWO <6> <7> <8> NOP UNARY_POSITIVE UNARY_NEGATIVE UNARY_NOT <13> <14> UNARY_INVERT <16> <17> <18> BINARY_POWER BINARY_MULTIPLY <21> BINARY_MODULO BINARY_ADD BINARY_SUBTRACT BINARY_SUBSCR BINARY_FLOOR_DIVIDE BINARY_TRUE_DIVIDE INPLACE_FLOOR_DIVIDE INPLACE_TRUE_DIVIDE <30> <31> <32> <33> <34> <35> <36> <37> <38> <39> <40> <41> <42> <43> <44> <45> <46> <47> <48> <49> <50> <51> <52> <53> STORE_MAP INPLACE_ADD INPLACE_SUBTRACT INPLACE_MULTIPLY <58> INPLACE_MODULO STORE_SUBSCR DELETE_SUBSCR BINARY_LSHIFT BINARY_RSHIFT BINARY_AND BINARY_XOR BINARY_OR INPLACE_POWER GET_ITER STORE_LOCALS PRINT_EXPR LOAD_BUILD_CLASS YIELD_FROM <73> <74> INPLACE_LSHIFT INPLACE_RSHIFT INPLACE_AND INPLACE_XOR INPLACE_OR BREAK_LOOP WITH_CLEANUP <82> RETURN_VALUE IMPORT_STAR <85> YIELD_VALUE POP_BLOCK END_FINALLY POP_EXCEPT STORE_NAME DELETE_NAME UNPACK_SEQUENCE FOR_ITER UNPACK_EX STORE_ATTR DELETE_ATTR STORE_GLOBAL DELETE_GLOBAL <99> LOAD_CONST LOAD_NAME BUILD_TUPLE BUILD_LIST BUILD_SET BUILD_MAP LOAD_ATTR COMPARE_OP IMPORT_NAME IMPORT_FROM JUMP_FORWARD JUMP_IF_FALSE_OR_POP JUMP_IF_TRUE_OR_POP JUMP_ABSOLUTE POP_JUMP_IF_FALSE POP_JUMP_IF_TRUE LOAD_GLOBAL <117> <118> CONTINUE_LOOP SETUP_LOOP SETUP_EXCEPT SETUP_FINALLY <123> LOAD_FAST STORE_FAST DELETE_FAST <127> <128> <129> RAISE_VARARGS CALL_FUNCTION MAKE_FUNCTION BUILD_SLICE MAKE_CLOSURE LOAD_CLOSURE LOAD_DEREF STORE_DEREF DELETE_DEREF <139> CALL_FUNCTION_VAR CALL_FUNCTION_KW CALL_FUNCTION_VAR_KW SETUP_WITH EXTENDED_ARG LIST_APPEND SET_ADD MAP_ADD <148> - <255> ```

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

 Name: Email: (Your email is not shared with anybody) Comment: