Python:从汇编代码中删除控制指令并查找依赖关系

时间:2014-07-24 23:38:17

标签: python python-2.7

根据评论编辑问题:

我有一个汇编代码文件,其说明如下:

Line[1]:       | [wrr a10,a9] [add a8,a10,a3] [ld.n a8,a8,16]                                                              
Line[2]:       | [add a8,a10,a3] [ld.n a8,a8,16] [call a8]                                                                 
Line[3]:       | [ld.n a8,a8,16] [call a8] [entry a1,32]                                                                      
Line[4]:       | [call a8] [entry a1,32] [ld a8,0x1fff8910]                                                                   
Line[5]:       | [entry a1,32] [ld a8,0x1fff8910] [ext a3,a2,0,8]                                                             
Line[6]:       | [st.n a15,a1,12] [st.n a12,a1,16] [st.n a9,a1,8]                                                            
Line[7]:       | [ext a3,a2,0,8] [sll a3,a3,4] [add.n a3,a3,a8]                                                                
Line[8]:       | [sll a3,a3,4] [add.n a3,a3,a8] [ld a11,a3,128]    

我想删除以下列表中描述的控制指令以及说明中出现的任何两个后续加载存储:

control_instructions = ['call', 'entry', 'j']

2_consecutive_remove = ['st', 'ld]

(如果ld ld或st st出现在一行的2个连续列表项中,我必须删除该行)

由于呼叫和输入出现在第2-5行,而第6行连续有两条st指令,我的输出应仅打印第1,7,8行

我尝试使用以下代码删除控制指令,但它似乎不起作用。任何帮助将不胜感激

new_lines = [line for line in old_lines if not any(x in y for x in control_instructions for y in line )]

1 个答案:

答案 0 :(得分:0)

您的文件不是列表,所以:

control_instructions = ['call', 'entry', 'j']
cleaned = []
consecutive_remove = ['st.n', 'ld']
with open("your_log.txt") as f:
    for line in f:
        if not any(x in line for x in control_instructions) and sum(line.count(x) for x in consecutive_remove)  <= 1 :
            cleaned.append(line)
            print line

Line[1]: | [wrr a10,a9] [add a8,a10,a3] [ld.n a8,a8,16]

Line[7]: | [ext a3,a2,0,8] [sll a3,a3,4] [add.n a3,a3,a8]

Line[8]: | [sll a3,a3,4] [add.n a3,a3,a8] [ld a11,a3,128]