about summary refs log tree commit diff stats
path: root/example/expression/asm_to_ir.py
blob: 8ecc4f24cdf87da52e15d456a218faad94bfb430 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import print_function
from pdb import pm

from future.utils import viewitems

from miasm.arch.x86.arch import mn_x86
from miasm.core import parse_asm
from miasm.expression.expression import *
from miasm.core import asmblock
from miasm.arch.x86.ira import ir_a_x86_32
from miasm.analysis.data_flow import DeadRemoval
from miasm.core.locationdb import LocationDB


# First, asm code
loc_db = LocationDB()
asmcfg = parse_asm.parse_txt(
    mn_x86, 32, '''
main:
   MOV    EAX, 1
   MOV    EBX, 2
   MOV    ECX, 2
   MOV    DX, 2

loop:
   INC    EBX
   CMOVZ  EAX, EBX
   ADD    EAX, ECX
   JZ     loop
   RET
''',
    loc_db
)


loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0)
for block in asmcfg.blocks:
    print(block)


print("symbols:")
print(loc_db)
patches = asmblock.asm_resolve_final(mn_x86, asmcfg)

# Translate to IR
ir_arch = ir_a_x86_32(loc_db)
ircfg = ir_arch.new_ircfg_from_asmcfg(asmcfg)
deadrm = DeadRemoval(ir_arch)


# Display IR
for lbl, irblock in viewitems(ircfg.blocks):
    print(irblock)

# Dead propagation
open('graph.dot', 'w').write(ircfg.dot())
print('*' * 80)
deadrm(ircfg)
open('graph2.dot', 'w').write(ircfg.dot())

# Display new IR
print('new ir blocks')
for lbl, irblock in viewitems(ircfg.blocks):
    print(irblock)