前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >汇编优化工具「建议收藏」

汇编优化工具「建议收藏」

作者头像
全栈程序员站长
发布2022-07-07 17:00:37
2380
发布2022-07-07 17:00:37
举报

大家好,又见面了,我是全栈君。

代码语言:javascript
复制
/*
汇编优化工具
使汇编代码更具可读性,并提高汇编速度
优化15000行大约须要30秒
*/

import rfile.h

main
{
	name=getsl
	s=rfile.read_all_n(name)

	s=s.replace('\n','\r\n')
	s=s.replace('dword esp','esp')
	s=s.replace('dword ebp','ebp')
	s=s.replace('dword edi','edi')
	s=s.replace('dword esi','esi')
	s=s.replace('dword ebx','ebx')
	s=s.replace(',ecx',', ecx')
	s=s.replace('ebx,0','ebx , 0')
	s=s.replace('ecx,[','ecx , [')
	s=s.replace('mov dword [ edi ] , esi','mov [ edi ] , esi')
	s=opt_sym(s)
	s=line_del(s)
	s=opt_jmp(s)
	s=line_del(s)
	s=opt_two(s)
	s=line_del(s)
	s=sym_replace(s)
	s=line_del(s)

	rfile.write_all_n(name,s)	
}

rstr line_del(rstr& s)
{
	return s.replace('\r\n\r\n\r\n','\r\n\r\n')
}

//符号替换
rstr sym_replace(rstr s)
{
	rbuf<rstr> arr
	r_split_e<rstr>(s,'\r\n',arr)
	rstr ret
	for i in arr
		temp=arr[i]
		if temp.get_top==`:&&is_symbol(temp.sub(0,temp.count-1))
			ret+=sym_trans(temp.sub(0,temp.count-1))+':'
			ret+='\r\n'
			continue
		if (temp.sub(0,5)=='	jmp '||
			temp.sub(0,6)=='	call '||
			temp.sub(0,5)=='	jnz '||
			temp.sub(0,4)=='	jg '||
			temp.sub(0,4)=='	jl '||
			temp.sub(0,5)=='	jae '||
			temp.sub(0,4)=='	jz ')
			index=temp.find(' ')
			symbol=temp.sub(index+1)
			if is_symbol(symbol)
				ret+=temp.sub(0,index+1)+sym_trans(symbol)
				ret+='\r\n'
				continue
		ret+=temp
		ret+='\r\n'
	return ret
}

rstr sym_trans(rstr s)
{
	rstr ret
	for i in s
		two=s.sub(i,i+2)
		if two=='28'||two=='29'//()
			i++
			ret+='_'
		elif two=='2E'||two=='2C'||two=='5F'//.,_
			i++
			ret+='_'
		elif two=='26'
			i++
			ret+='q'
		elif two=='7E'
			i++
			ret+='d'
		elif two=='7C'
			i++
			ret+='h'
		elif two=='5B'
			i++
			ret+='i'
		elif two=='5D'
			i++
			ret+='j'
		elif two=='3D'//=
			i++
			ret+='a'
		elif two=='2B'
			i++
			ret+='b'
		elif two=='2D'//-
			i++
			ret+='s'
		elif two=='2A'//*
			i++
			ret+='t'
		elif two=='2F'
			i++
			ret+='f'
		elif two=='25'
			i++
			ret+='e'
		elif two=='21'//!
			i++
			ret+='o'
		elif two=='3E'//>
			i++
			ret+='x'
		elif two=='3C'//<
			i++
			ret+='y'
		else
			ret+=s[i]
	return seq_replace(ret)
}

rstr seq_replace(rstr s)
{
	//s=s.replace('__','_')
	if s.get_top==`_
		s.pop
	if s.get_top==`_
		s.pop
	return s
}

//无引用符号删除
rstr opt_sym(rstr s)
{
	rbuf<rstr> arr
	r_split_e<rstr>(s,'\r\n',arr)
	rstr ret
	for i in arr
		temp=arr[i]
		full=temp.sub(0,temp.count-1)
		if temp.get_top==`:&&is_symbol(full)
			symbol=get_symbol(full)
			ifn symbol.empty
				if get_count(arr,i,symbol,full)==1
					continue
		ret+=temp
		ret+='\r\n'
	return ret
}

//jmp后接一个跳转符号
rstr opt_jmp(rstr s)
{
	rbuf<rstr> arr
	r_split_e<rstr>(s,'\r\n',arr)
	rstr ret
	for i in arr
		temp=arr[i]
		a=arr.get(i+1)
		full=a.sub(0,a.count-1)
		if temp.sub(0,5)=='	jmp '&&temp.sub(5)==full&&a.get_top==`:
			symbol=get_symbol(full)
			ifn symbol.empty
				if get_count(arr,i,symbol,full)==2
					i++
					continue
				else
					continue
		ret+=temp
		ret+='\r\n'
	return ret
}

//连续2个jmp,后一个jmp不可能被运行
rstr opt_two(rstr s)
{
	rbuf<rstr> arr
	r_split_e<rstr>(s,'\r\n',arr)
	rstr ret
	for i in arr
		temp=arr[i]
		a=arr.get(i+1)
		if temp.sub(0,5)=='	jmp '&&a.sub(0,5)=='	jmp '
			ret+=temp
			ret+='\r\n'
			i++
			continue
		ret+=temp
		ret+='\r\n'
	return ret
}

int get_count(rbuf<rstr>& arr,int i,rstr& symbol,rstr& full)
{
	for j=i-1;j>=0;j--
		if arr[j].sub(0,arr[j].count-1)==symbol&&`:==arr[j].get_top
			break
	for k=i+1;k<arr.count;k++
		if arr[k].empty
			break
	return find_count(arr,j,k,full)
}

rstr get_symbol(rstr s)
{
	for i=s.count-1;i>=0;i--
		if s[i]==`_&&s.sub(i+1).is_number
			return s.sub(0,i)
	return ''
}

int find_count(rbuf<rstr>& arr,int left,int right,rstr& symbol)
{
	count=0
	for i=left;i<=right;i++
		count+=find_count(arr[i],symbol)
	return count
}

int find_count(rstr src,rstr dst)
{
	count=0
	for i in src
		if src.sub(i,i+dst.count)==dst
			count++
	return count	
}

bool is_symbol(rstr s)
{
	for i in s
		ifn is_symbol(s[i])
			return false
	return true
}

bool is_symbol(char ch)
{
	return ch>=`A&&ch<=`Z||ch>=`a&&ch<=`z||ch>=`0&&ch<=`9||ch==`_
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116563.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档