Python:用于检测数据危害的脚本
我正在研究一个学校项目,编写一个程序来检测汇编指令中的Read-After-Write数据依赖性.我有一个包含说明的列表.
一个这样的例子是诸如的列表 这里最后一个加法指令取决于左移(shl)的结果,而该结果又取决于mul intsruction的结果. 我希望我的代码输出依赖为{mul – > sh – >加} 另一个例子:line2 = [[add a3,a1] [sub a4,a5,a6] [add a9,a4]] 我想删除指令操作码以获取line1 = [[a8,0x8910] [a3,8] [a3,4] [a3,a8]] 到目前为止,我已经尝试过: dest = re.findall( r'[(?=([a-z0-9.]+))',str(line)) src = re.findall( r',(?=([a-z0-9]+))',str(line)) for i in dest: dst_list.append([i]) for j in src: src_list.append(j) #psuedo code to find hazards for nth src_item in src_list: for 0 to n-1 dst_items in dst_list: if src_list[src_item] == dst_list[dst_item] OUTPUT dst_item -> src_item 上面的re.findall为我提供了一个包含所有目标操作数的列表和另一个包含源操作数的列表(我需要列表,列表中每个指令的2个src参数在一个列表中). 我该如何实现这一目标? 解决方法
因此,我们首先假设您的汇编指令将具有一致的格式,并且它们位于字符串列表中,因为否则您将不得不进行一系列预处理,并且应该处理操作代码和指令解析.
[op_code dest,src1,src2,...,srcn] 例如: line1 = ["ld a8,0x8910","mul a3,8","shl a3,4","add a3,a8"] 有了它,我们可以做一些python魔术,甚至不担心正则表达式. def instruction_reader(op_string): opcode,values = op_string.split(" ") values_list = values.split(',') dest = values_list[0] src = values_list[1:] return (opcode,dest,src) 有了它,您现在可以通过一些列表迭代器将数据放入正确的桶中 list_of_all = [instruction_reader(item) for item in line1] opcodes = [op[0] for op in list_of_all] dests = [dest[1] for dest in list_of_all] srcs = [src[2] for src in list_of_all] 现在你只需要在srcs和dests之间进行比较以找到依赖关系. dep_list = [] for i,dest in enumerate(dests): for src in srcs: if dest in src: dep_list.append(opcodes[i]) 这可以简化为列表理解 dep_list = [opcodes[i] for i,dest in enumerate(dests) for src in srcs if dest in src] 旁白:是的,在python中有更多更漂亮的方法可以做到这一点,但我认为在这种情况下更容易阅读/解析的东西会更好. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 100行python代码实现跳一跳辅助程序
- python基础学习5----字典
- Python资源安装过程出现Retrying (Retry(total=4, connect=
- 如何在tweepy的流API中使用’count’参数?
- django – Rest Framework教程IntegrityError创建片段
- Python Ethical Hacking - BACKDOORS(1)
- python-2.7 – 将分组的zscore列添加到pandas数据帧
- python入门教程 python入门神图一张
- Python3和Python2 异常处理Exception的简单示例
- 如何在python中创建文件的校验和