一个这样的例子是诸如的列表
line1 = [[ld a8,0x8910] [mul a3,a2,8] [shl a3,a3,4] [add a3,a8]]
这里最后一个加法指令取决于左移(shl)的结果,而该结果又取决于mul intsruction的结果.
我希望我的代码输出依赖为{mul – > sh – >加}
另一个例子:line2 = [[add a3,a1] [sub a4,a5,a6] [add a9,a4]]
输出:{add – >添加} {sub – >加}
我想删除指令 *** 作码以获取line1 = [[a8,0x8910] [a3,8] [a3,4] [a3,a8]]
然后将目标 *** 作数提取到dst_List = [a8,a3}并将源 *** 作数提取到另一个列表中,如src_List = [0x8910,[a2,8],[a3,4],a8]].我正在考虑从src_List中获取第n个项目,并在匹配时与0到n-1项dst_List和输出索引进行比较.最后使用一些字典输出与我的索引相对应的指令.我的方法是否正确?任何人都可以帮助我如何在python中实现这一目标?
到目前为止,我已经尝试过:
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 hazardsfor 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中有更多更漂亮的方法可以做到这一点,但我认为在这种情况下更容易阅读/解析的东西会更好.
总结以上是内存溢出为你收集整理的Python:用于检测数据危害的脚本全部内容,希望文章能够帮你解决Python:用于检测数据危害的脚本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)