📄 cleanasm.py
字号:
if k == "__PIA": s = [ ["mov", "bx,1"], ["xor", "cx,cx"], ] dpos = omatch(i-1, -2, s) if dpos: orewrite_inst(i, "M_PIA1", "", dpos) continue if k == "__PTC": s = [ ["jne", "(.*)"], ] dpos = omatch(i+1, 1, s) if dpos: olines[i][1] = "M_PTC_JNE" k, v = parse_label("jne", dpos[0][2].group(1)) orewrite_call(i, k, v, dpos) continue if opts.loop_rewrite and inst in ["loop"]: s = [ ["mov", r"^c[lx],11$"], ["shr", "dx,1"], ["rcr", "ax,1"], ] dpos = omatch(i-1, -3, s) if dpos: orewrite_inst(i, "M_shrd_11", "", dpos) continue s = [ ["mov", r"^c[lx],8$"], ["shl", "ax,1"], ["rcl", "dx,1"], ] dpos = omatch(i-1, -3, s) if dpos: orewrite_inst(i, "M_shld_8", "", dpos) continue s1 = [ ["mov", r"^c[lx],8$"], ["shl", "si,1"], ["rcl", "di,1"], ] s2 = [ ["les", r"^bx,dword ptr \[bp([+-](\d+))\]$"], ] dpos1 = omatch(i-1, -3, s1) dpos2 = omatch(i+1, 1, s2) if 1 and dpos1 and dpos2: # bx and cx are free for use orewrite_inst(i, "M_shld_disi_8_bxcx", "", dpos1) continue s1 = [ ["mov", "ax,si"], ["mov", r"^c[lx],8$"], ["shl", "ax,1"], ["rcl", "di,1"], ] s2 = [ ["mov", "si,ax"], ["les", r"^bx,dword ptr \[bp([+-](\d+))\]$"], ] dpos1 = omatch(i-1, -4, s1) dpos2 = omatch(i+1, 2, s2) if 1 and dpos1 and dpos2: # bx and cx are free for use orewrite_inst(i, "M_shld_diax_8_bxcx", "", dpos1[-3:]) continue s1 = [ ["mov", r"^c[lx],8$"], ["shl", r"^word ptr \[bp([+-](\d+))\],1$"], ["rcl", r"^word ptr \[bp([+-](\d+))\],1$"], ] s2 = [ ["mov", r"^dx,word ptr"], ["mov", r"^ax,word ptr"], ] s3 = [ ["mov", r"^ax,word ptr"], ["mov", r"^dx,word ptr"], ] dpos1 = omatch(i-1, -3, s1) dpos2 = omatch(i+1, 2, s2) dpos3 = omatch(i+1, 2, s3) if dpos1 and (dpos2 or dos3): bp_dx, bp_ax = dpos1[-1][2].group(1), dpos1[-2][2].group(1) m = "M_shld_8_bp %s %s" % (bp_dx, bp_ax) orewrite_inst(i, m, "", dpos1) continue s1 = [ ["mov", r"^word ptr \[bp([+-](\d+))\],si$"], ["mov", r"^word ptr \[bp([+-](\d+))\],di$"], ["mov", r"^c[lx],11$"], ["shr", r"^word ptr \[bp([+-](\d+))\],1$"], ["rcr", r"^word ptr \[bp([+-](\d+))\],1$"], ] s2 = [ ["mov", r"^bx,word ptr"], ["mov", r"^bx,word ptr"], ["mov", r"^ax,word ptr \[bp([+-](\d+))\]$"], ["mov", r"^dx,word ptr \[bp([+-](\d+))\]$"], ] dpos1 = omatch(i-1, -5, s1) dpos2 = omatch(i+1, 4, s2) if dpos1 and dpos2: bp_dx, bp_ax = dpos1[-2][2].group(1), dpos1[-1][2].group(1) bp_di, bp_si = dpos1[-4][2].group(1), dpos1[-5][2].group(1) assert bp_dx == dpos2[-1][2].group(1) assert bp_ax == dpos2[-2][2].group(1) assert bp_dx == bp_di assert bp_ax == bp_si m = "M_shrd_11_disi_bp %s %s" % (bp_dx, bp_ax) orewrite_inst(i, m, "", dpos1 + dpos2[-2:]) continue if opts.mov_rewrite and inst in ["mov"]: s = [ ["mov", r"^al,byte ptr \[(di|si)\]$"], ["xor", r"^ah,ah$"], ["mov", r"^word ptr \[bp([+-](\d+))\],ax$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^word ptr \[bp([+-](\d+))\],(0|1)$"], ["mov", r"^bx,word ptr \[bp([+-](\d+))\]$"], ["mov", r"^word ptr \[bx\],(0)$"], ["mov", r"^word ptr \[bx([+-](\d+))\],(0)$"], ["mov", r"^bx,word ptr \[bp([+-](\d+))\]$"], ["mov", r"^word ptr \[bx\],(0)$"], ["mov", r"^word ptr \[bx([+-](\d+))\],(0)$"], ["mov", r"^dl,byte ptr \[(di|si)([+-](\d+))\]$"], ["xor", r"^dh,dh$"], ["mov", r"^cx,ax$"], ] dpos = omatch(i, -len(s), s) if dpos: ipos, n_del = 16, 0 pos0 = dpos[0][0] r = [] for pos, m0, m1 in dpos: assert pos == pos0 + len(r) r.append([olines[pos][1], olines[pos][2]]) z0 = r[0]; z1 = r[2]; del r[:3] r.insert(0, ["xor", "ax,ax"]) r.insert(ipos, z0); r.insert(ipos + 1, z1) i = 0 while i < len(r): inst, args = r[i] if inst == "mov" and args.endswith(",0"): r[i] = [inst, args[:-1] + "ax"] elif inst == "mov" and args.endswith(",1"): assert i < ipos r.insert(ipos, [inst, args[:-1] + "ax"]) del r[i]; i -= 1; n_del += 1 i += 1 assert len(r) == len(dpos) pos = pos0 for inst, args in r: ##print pos-pos0, inst, args olines[pos][1] = inst olines[pos][2] = args pos += 1 if n_del: olines.insert(pos0 + ipos - n_del, [None, "inc", "ax", None]) continue # if inst_has_label(inst): k, v = parse_label(inst, args) olines[i][2] = None olines[i][3] = add_label(k, v) # # pass 3 digits, i = 1, len(labels) while i >= 10: digits += 1 i /= 10 format = "%s0%dd" % ("%", digits) counter = 0 for i in range(len(olines)): label, inst, args, args_label = olines[i] # rewrite local labels v = labels.get(label) if v is not None: assert v[:3] == [0, 0, label], (label, v) v[2] = opts.label_prefix + format % counter counter += 1 # handle inlining if opts.auto_inline and inst == "call": v = labels[args_label] if v[:2] == [1, 2]: # external 2-byte x = inline_map.get(v[2]) if x and v[3] <= x[1]: # max. number of calls ##print "inline", v, x if x: olines[i][1] = x[0] olines[i][2] = "/* inlined */" olines[i][2] = "" olines[i][3] = None # # write ofile ofp = open(ofile, "wb") current_label = None for label, inst, args, args_label in olines: if labels.has_key(label): current_label = labels[label][2] if opts.verbose: ofp.write("%s: /* %d */\n" % (labels[label][2], labels[label][3])) else: ofp.write("%s:\n" % (labels[label][2])) if inst == "*DEL*": continue if 1 and current_label in [".Lf122", ".Lf123", ".Lf124", ".Ls122", ".Ls123", ".Ls124"]: continue if args_label: if opts.verbose: args = "%s /* %d */" % (labels[args_label][2], labels[args_label][3]) else: args = labels[args_label][2] if 0: # remove unneeded "byte/word/dword ptr" # [this works, but disabled for now as we gain nothing] if re.search(r"\bbyte ptr ", args): if re.search(r"^[abcd][hl],", args): args = args.replace("byte ptr ", "") if re.search(r",[abcd][hl]$", args): args = args.replace("byte ptr ", "") if re.search(r"\bword ptr ", args): if re.search(r"^[abcds][ix],", args): args = args.replace("word ptr ", "") if re.search(r",[abcds][ix]$", args): args = args.replace("word ptr ", "") if re.search(r"\bdword ptr ", args): if re.search(r"^[abcd][x],", args): args = args.replace("dword ptr ", "") l = "%8s%-7s %s" % ("", inst, args) ofp.write(l.rstrip() + "\n") ofp.close() ##print olinesif __name__ == "__main__": sys.exit(main(sys.argv))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -