16汇编学习:数据传送指令的反汇编

数据传送指令大概是pdf的第一页

这里有些疑惑的是xchg的第二种类型,和累加寄存器交换,如果携程xchg ax,ax会被xp debug当成nop(可能是交换无意义吧,毕竟都没变)

MyTransfer.h

#pragma once
#include<string>
#include <iostream>
#include <iomanip>
#include<map>
#include<share.h>
#include<sstream>
#include<vector>
//#define BIT(a,start,end) ((a)&0xff)>>((end-start))
int GetBit(unsigned char num, int n, int m);//第n位开始的m个位数
#define PMT std::shared_ptr<Type>
#define PTR std::shared_ptr<Transfer>
enum RegType
{
	/*八大常用寄存器*/
	AX=0,AL=0,AH=4,
	CX=1,CL=1,CH=5,
	DX=2,DL=2,DH=6,
	BX=3,BL=3,BH=7,
	SP=4,
	BP=5,
	SI=6,
	DI=7,
	/*段寄存器*/
	ES=0,
	CS = 1,
	SS=2,
	DS=3,
	/*基址和变址寄存器*/
	BXSI=0,
	BXDI=1,
	BPSI=2,
	BPDI=3,
	IRSI=4,
	IRDI=5,
	BRBP=6,
	BRBX=7
};
class Type
{
public:
	virtual bool cmp(unsigned char Key) = 0;//如果键值匹配返回true
	virtual std::string Decode(const unsigned char* ptr, int& offset) = 0;
	static std::string getSmallReg(unsigned char reg );//小的寄存器 ah,al...
	static std::string getGeneralReg(unsigned char reg);//八大常用寄存器
	static std::string getSegementReg(unsigned char reg);//段寄存器
	static std::string getBrIrReg(unsigned char reg);//基址变址寄存器
	static std::string getHex(unsigned char data);//获取一字节16进制数据
	static std::string getAddress(unsigned char mod,unsigned char r, unsigned char* ptr=nullptr,unsigned char w=1);//获取寄存器是off1当做w 1返回大寄存器 0返回小的
	static std::string getReg(unsigned char w, unsigned char reg);
	static std:: string getRegMem( unsigned char rm);
	static std::string getRegMem1( unsigned char rm, unsigned char off1);
	static std::string getRegMem2( unsigned char rm,unsigned char off1,unsigned char off2);
	static int getOffset(unsigned char mod, unsigned char rm);
private:

};
class MovType1:public Type
{
	unsigned short rm : 3;
	unsigned short reg : 3;
	unsigned short mod : 2;
	unsigned short w : 1;
	unsigned short d : 1;
	unsigned short key : 6;
public:
	
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType2 :public Type
{
	unsigned short rm : 3;
	unsigned short z3:3;
	unsigned short mod : 2;
	unsigned short w : 1;
	unsigned  short key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType3:public Type
{
	unsigned char reg : 3;
	unsigned char w : 1;
	unsigned char key : 4;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType4 :public Type
{
	unsigned char w : 1;
	unsigned char key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType5 :public Type
{
	unsigned char w : 1;
	unsigned char key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType6 :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 2;
	unsigned short z1 : 1;
	unsigned short mod : 2;
	unsigned short key : 8;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class MovType7 :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 2;
	unsigned short z1 : 1;
	unsigned short mod : 2;
	unsigned short key : 8;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//push [地址]
class PushType1:public Type
{
	unsigned short rm:3;
	unsigned short n110 : 3;
	unsigned short mod : 2;
	unsigned short key:8;
public:
	PushType1() { n110 = 6; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
//push 常用寄存器
class PushType2:public Type
{
	unsigned char reg : 3;
	unsigned char n01010 : 5;
public:
	PushType2() { n01010 = 0b01010; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
//push 段寄存器
class PushType3 :public Type
{
	unsigned char n110 : 3;
	unsigned char sreg : 2;
	unsigned char n000 : 3;
public:
	PushType3() { n000 = 0b000;
	n110 = 0b110;
	}
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
class PopType1 :public Type
{
	unsigned short rm : 3;
	unsigned short n000 : 2;
	unsigned short mod : 2;
	unsigned short key : 8;
public :
	PopType1() { n000 = 0b000; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class PopType2 :public Type
{
	unsigned char reg : 3;
	unsigned char n01011 : 5;
public:
	PopType2() { n01011 = 0b01011; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class PopType3 :public Type
{
	unsigned char n111 : 3;
	unsigned char sreg : 2;
	unsigned char n000 : 3;
public:
	PopType3() {
		n000 = 0b000;
		n111 = 0b111;
	}
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
//1.寄存器与寄存器之间可以对换数据
//2.寄存器与存储器之间可以对换数据
//3.不能在存储器与存储器之间对换数据
class XchgType1 :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 3;
	unsigned short mod : 2;
	unsigned short w : 1;
	unsigned short key : 7;

public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
class XchgType2 :public Type
{
	unsigned char reg : 3;
	unsigned char key : 5;
	
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};

class XlatType :public Type
{

public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//in al,port in ax port
class InType1:public Type
{
	unsigned short  port:8;
	unsigned short w : 1;
	unsigned short key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
class InType2 :public Type
{
	
	unsigned char w : 1;
	unsigned char key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);

};
class OutType1:public Type
{
	unsigned short  port : 8;
	unsigned short w : 1;
	unsigned short key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class OutType2 :public Type
{
	unsigned char w : 1;
	unsigned char key : 7;
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class LeaType :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 3;
	unsigned short mod : 2;
	unsigned short key : 8;
public:
	LeaType() { key = 0x8d; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//LDS r16, mem
//r16←mem
//DS←mem + 2
class LdsType :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 3;
	unsigned short mod : 2;
	unsigned short key : 8;
public:
	LdsType() { key = 0xc5; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class LesType :public Type
{
	unsigned short rm : 3;
	unsigned short reg : 3;
	unsigned short mod : 2;
	unsigned short key : 8;
public:
	LesType() { key = 0xc4; }
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//LAHF指令将标志寄存器的低字节送寄存器AH flag->ah
class LahfType :public Type
{
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//SAHF将AH寄存器内容送FLAGS的低字节 ah->flag
class SahfType :public Type
{
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//PUSHF指令将标志寄存器的内容压入堆栈,同时栈顶指针SP减2
class PushfType :public Type
{
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
//POPF指令将栈顶字单元内容送标志寄存器,同时栈顶指针SP加2
class PopfType :public Type
{
public:
	virtual bool cmp(unsigned char Key);
	virtual std::string Decode(const unsigned char* ptr, int& offset);
};
class Transfer
{
public :
	Transfer();
	virtual std::string Decode(int index, unsigned char*ptr,int &offset);
	virtual int cmp(unsigned char Key);
private:
	std::vector<PMT>mVec;
};
class Disassembly
{
public:
	static  std::string Decode(unsigned char *ptr,int& offset);//解析字符串  返回
public:
	static std::vector<PTR>mVec;
};

#include "MyTransfer.h"
std::vector<PTR> Disassembly::mVec = {PTR(new Transfer()) };
std::string Disassembly::Decode(unsigned char* ptr, int& offset)
{
	int ret = -1;
	for (int i = 0; i < mVec.size(); i++)
	{
		ret = mVec[i]->cmp(ptr[0]);
		if ( ret!= -1)
		{
			return mVec[i]->Decode(ret, ptr, offset);
		}
	}
	
	offset = 1;
	return "unkown";
}

std::string Type::getSmallReg(unsigned char reg)
{
	switch (reg)
	{
	case AL:
		return "AL";
	case CL:
		return "CL";
	case DL:
		return "DL";
	case BL:
		return "BL";
	case AH:
		return "AH";
	case CH:
		return "CH";
	case DH:
		return "DH";
	case BH:
		return "BH";
	}
	return std::string();
}

std::string Type::getGeneralReg(unsigned char reg)
{
	switch (reg)
	{
	case AX:
		return "AX";
	case CX:
		return "CX";
	case DX:
		return "DX";
	case BX:
		return "BX";
	case SP:
		return "SP";
	case BP:
		return "BP";
	case SI:
		return "SI";
	case DI:
		return "DI";
	}
	return "unkown GeneralReg";
}

std::string Type::getSegementReg(unsigned char reg)
{
	switch (reg)
	{
	case ES:
		return "ES";
	case CS:
		return "CS";
	case SS:
		return "SS";
	case DS:
		return "DS";
	}
	return "unkown SegementReg";
}

std::string Type::getBrIrReg(unsigned char reg)
{
	switch (reg)
	{
	case BXSI:
		return "BX+SI";
	case BXDI:
		return "BX+DI";
	case BPSI:
		return "BP+SI";
	case BPDI:
		return "BP+DI";
	case IRSI:
		return "SI";
	case IRDI:
		return "DI";
	case BRBX:
		return "BX";
	case BRBP:
		return "BP";
	}
	return "unkown BrIrReg";
}

std::string Type::getHex(unsigned char data)
{
	static unsigned char a[] = { "0123456789ABCDEF" };
	std::string ret;
	do
	{
		ret .insert(ret.begin(), a[data % 16]);
		data /= 16;
	} while (data > 0);
	if (ret.size() == 1)
		ret .insert(ret.begin(), '0');
	return ret;
}

std::string Type::getAddress(unsigned char mod, unsigned char r,unsigned char*ptr, unsigned char w)
{
	switch (mod)
	{
	case 3:
		return getReg( w, r);
	case 2:
		return getRegMem2(r, ptr[1], ptr[2]);
	case 1:
		return getRegMem1( r, ptr[1]);
	case 0:
	{
		if (r != BRBP)
			return getRegMem(r);
		else
		{
			return std::string("[") + getHex(ptr[2]) + getHex(ptr[1]) + "]";
		}
	}
		
	}
	return std::string("unkown mod");
}

std::string Type::getReg(unsigned char w,unsigned char reg)
{
	return w == 1 ? getGeneralReg(reg) : getSmallReg(reg);
}

std::string Type::getRegMem( unsigned char rm)
{
	std::string ret;
	ret = "[" + getBrIrReg(rm)+"]";
	return ret;
}

std::string Type::getRegMem1(unsigned char rm, unsigned char off1)
{
	std::string ret;
	ret = "[" + getBrIrReg(rm) + "+" + getHex(off1) + "]";
	return ret;
}

std::string Type::getRegMem2( unsigned char rm, unsigned char off1, unsigned char off2)
{
	std::string ret;
	ret = "[" + getBrIrReg(rm) + "+" + getHex(off2)+getHex(off1) + "]";
	return ret;
}

int Type::getOffset(unsigned char mod, unsigned char rm)
{
	int offset = 1;
	if (mod == 0b11)// reg,reg
	{
		offset = 0;
	}
	else if (mod == 0b10)
		offset = 2;
	else if (mod == 0b01)
		offset = 1;
	else if (mod == 0b00)
	{
		if (rm == BRBP)
			offset = 2;
		else
			offset = 0;
	}
	return offset;
}



bool MovType1::cmp(unsigned char Key)
{
	if (Key >= 0x88 && Key <= 0x8B)
	{
		return true;
	}
	
	return false;
}
/**/
//mod 2位
//00 表示基址变址
//01相对基址变址+1字节偏移
//10相对基址变址+2字节偏移
//11寄存器到寄存器

//w
//1 2字节寄存器
//0 1字节寄存器
//d
//0 到[reg]
//1到reg
std::string MovType1::Decode(const unsigned char* ptr, int& offset)
{
	//unsigned char 

	std::string hex_str;
	std::string ret="mov ";
	key = GetBit(ptr[0], 2, 6);
	d = GetBit(ptr[0], 1, 1);
	w = GetBit(ptr[0], 0, 1);
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1],3,3 );
	rm = GetBit(ptr[1], 0, 3);
	std::string Rm= getAddress(mod, rm, (unsigned char*)ptr, w);;
	std::string Reg= getReg(w, reg);;
	switch (mod)
	{
	case 3:
		offset = 2;
		break;
	case 2:
		offset = 4;
		break;
	case 1:
		offset = 3;
		break;
	case 0:
		offset = rm == BRBP ? 4 : 2;
		break;
	}
	if (d)
	{
		ret += Reg + "," + Rm;
	}
	else
	{
		ret += Rm + "," + Reg;
	}
	return ret;
}

bool MovType2::cmp(unsigned char Key)
{
	if (Key == 0xc7 || Key == 0xc6)
		return true;
	return false;
}
// w 1两字节偏移 0一字节偏移
std::string MovType2::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	rm = GetBit(ptr[1], 0, 3);
	mod = GetBit(ptr[1], 6, 2);
	std::string ret = "mov ";
	std::string hex_str;
	std::string Rm= getAddress(mod,rm,(unsigned char*)(ptr+1),w);
	std::string Reg;
	hex_str = (w == 1 ? getHex(ptr[3]) : "") + getHex(ptr[2]);
	ret += w == 1 ? "word ptr" : "byte ptr";
	
	if (mod == 0)
	{
		ret += Rm+","+hex_str;
		//基址变址 变址表 1/2字节数据
		if (rm!= BRBP)
		{
			offset = w == 1 ? 4 : 3;
		}
		// mov [1234],1234
		else
		{
			offset = w == 1 ? 6: 5;
		}
	}
	else if (mod == 1)
	{
		ret += Rm + "," + hex_str;
		offset = w == 1 ? 5 : 4;
	}
	else if (mod == 2)
	{
		ret += Rm + "," + hex_str;
		offset = w == 1 ? 6 : 5;
	}
	//mov reg ,imm
	else
	{
		Reg = getReg(w, rm);
		ret += Reg + ",";
		ret += hex_str;
		offset = w == 1 ? 4 : 3;
	}

	return ret;
}

bool MovType3::cmp(unsigned char Key)
{
	if (Key >= 0xB0 && Key <= 0xBF)
		return true;
	
	return false;
}
//w 1 2字节立即数 0 1字节立即数 
std::string MovType3::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 3, 1);
	reg = GetBit(ptr[0], 0, 3);
	std::string ret="mov ";
	if (w == 1)
	{
		ret += getGeneralReg(reg)+","+getHex(ptr[2])+getHex(ptr[1]);
		offset = 3;
	}
	else
	{
		ret += getSmallReg(reg) + "," + getHex(ptr[1]) ;
		offset = 2;
	}
	return ret;
}
//内存到累加器
//w =1 ax
//w =0 al
bool MovType4::cmp(unsigned char Key)
{
	if (Key == 0xa1||Key==0xa0)
		return true;
	return false;
}

std::string MovType4::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	std::string ret = "mov ";
	std::string Reg= getReg(w, AX);
	ret += Reg;
	ret += " ,[" + getHex(ptr[2]) + getHex(ptr[1]) + ']';
	offset = 3;
	return ret;
}

bool MovType5::cmp(unsigned char Key)
{
	if (Key == 0xa2 || Key == 0xa3)
		return true;
	return false;
}

std::string MovType5::Decode(const unsigned char* ptr, int& offset)
{
	std::string ret = "mov ";
	w = GetBit(ptr[0], 0, 1);
	std::string Reg= getReg(w, AX);
	if (w == 1)
	{
		ret += '[' + getHex(ptr[2]) + getHex(ptr[1])+"],"+Reg;
		offset = 3;
	}
	else
	{
		ret += '[' + getHex(ptr[1]) + "],al" + Reg;
		offset = 2;
	}
	return ret;
}

bool MovType6::cmp(unsigned char Key)
{
	if (Key == 0x8E)
		return true;
	return false;
}

std::string MovType6::Decode(const unsigned char* ptr, int& offset)
{
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1], 3, 2);
	rm = GetBit(ptr[1], 0, 3);
	std::string ret = "mov ";
	std::string Reg = getSegementReg(reg);
	std::string Rm = getAddress(mod,rm,(unsigned char*)ptr+1);//= getBrIrReg(rm);
	std::string hex_str;
	ret += Reg + ",";
	ret += Rm;
	offset=2+ getOffset(mod,rm);
	return ret;
}

bool MovType7::cmp(unsigned char Key)
{
	if (Key == 0x8c)
		return true;
	return false;
}

std::string MovType7::Decode(const unsigned char* ptr, int& offset)
{
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1], 3, 2);
	rm = GetBit(ptr[1], 0, 3);
	std::string ret = "mov ";
	std::string Reg = getSegementReg(reg);
	std::string Rm = getAddress(mod, rm, (unsigned char*)ptr + 1);
	std::string hex_str;
	ret+= Rm + ",";
	offset = getOffset(mod, rm) + 2;
	ret += Reg;
	return ret;	
}
int GetBit(unsigned char num, int n, int m)
{
	return (num >> n) & ((1 << m) - 1);
}
bool XchgType2::cmp(unsigned char Key)
{
	if (Key >= 0x90 && Key <= 0x97)
		return true;
	return false;
}

std::string XchgType2::Decode(const unsigned char* ptr, int& offset)
{
	offset = 1;
	if (ptr[0] == 0x90)
		return "nop";
	reg = GetBit(ptr[0], 0, 3);
	std::string Reg = getGeneralReg(reg);
	std::string ret = "xchg "+Reg+",ax";
	return ret;
}

bool XchgType1::cmp(unsigned char Key)
{
	if (Key >= 0x86 && Key <= 0x87)
		return true;
	return false;
}

std::string XchgType1::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	rm = GetBit(ptr[1], 0, 3);
	reg = GetBit(ptr[1], 3, 3);
	mod = GetBit(ptr[1], 6, 2);
	std::string Reg=getReg(w,reg);
	std::string Rm=getAddress(mod,rm,(unsigned char*)ptr);
	std::string ret = "xchg "+Reg+","+Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;
}

Transfer::Transfer()
{
	mVec = {
PMT(new MovType1()),PMT(new MovType2()),PMT(new MovType3()),
PMT(new MovType4()),PMT(new MovType5()),PMT(new MovType6()),PMT(new MovType7()),
PMT(new XchgType1()),PMT(new XchgType2()),PMT(new PushType1()),PMT(new PushType2()),
PMT(new PushType3()),PMT(new PopType1()),PMT(new PopType2()),PMT(new PopType3()),
PMT(new LeaType()),PMT(new LesType()),PMT(new LdsType()),PMT(new LahfType()),
PMT(new SahfType()),PMT(new InType1()),PMT(new InType2()),PMT(new OutType1()),PMT(new  OutType2()),
PMT(new XlatType()),
	};
}

std::string Transfer::Decode(int index, unsigned char* ptr, int& offset)
{
	int len = mVec.size();
	if (index >= len || index < 0)
	{
		offset = 1;
		return "unkown index";
	}
	return mVec[index]->Decode(ptr, offset);
}

int Transfer::cmp(unsigned char Key)
{
	int len = mVec.size();
	for (int i = 0; i < len; i++)
	{
		if (mVec[i].get()->cmp(Key))
		{
			return i;
		}
	}
	return -1;
}
bool PushType1::cmp(unsigned char Key)
{
	if (Key == 0xff)
		return true;
	return false;
}

std::string PushType1::Decode(const unsigned char* ptr, int& offset)
{
	if (GetBit(ptr[1], 3, 3) != n110)
	{
		offset = 1;
		return "unkown";
	}
	rm = GetBit(ptr[1], 0, 3);
	mod = GetBit(ptr[1], 6, 2);
	std::string Rm = getAddress(mod, rm, (unsigned char*)(ptr + 1));
	std::string ret = "push " + Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;
}

bool PushType2::cmp(unsigned char Key)
{
	if (GetBit(Key,3, 5) == n01010)
		return true;
	return false;
}

std::string PushType2::Decode(const unsigned char* ptr, int& offset)
{
	reg = GetBit(ptr[0], 0, 3);
	std::string ret = "push " + getGeneralReg(reg);
	offset = 1;
	return ret;
}

bool PushType3::cmp(unsigned char Key)
{
	if (GetBit(Key, 0, 3) == n110 && GetBit(Key, 5, 3) == n000)
		return true;
	return false;
}

std::string PushType3::Decode(const unsigned char* ptr, int& offset)
{
	sreg = GetBit(ptr[0], 3, 2);
	std::string ret = "push " + getSegementReg(sreg);
	offset = 1;
	return ret;
}
bool PopType1::cmp(unsigned char Key)
{
	if (Key == 0x8f)
		return true;
	return false;
}

std::string PopType1::Decode(const unsigned char* ptr, int& offset)
{
	if (GetBit(ptr[1], 3, 3) != n000)
	{
		offset = 1;
		return "unkown";
	}
	rm = GetBit(ptr[1], 0, 3);
	mod = GetBit(ptr[1], 6, 2);
	std::string Rm = getAddress(mod, rm, (unsigned char*)(ptr + 1));
	std::string ret = "pop " + Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;
}

bool PopType2::cmp(unsigned char Key)
{
	if (GetBit(Key, 3, 5) == n01011)
		return true;
	return false;
}

std::string PopType2::Decode(const unsigned char* ptr, int& offset)
{
	reg = GetBit(ptr[0], 0, 3);
	std::string ret = "pop " + getGeneralReg(reg);
	offset = 1;
	return ret;
}

bool PopType3::cmp(unsigned char Key)
{
	if (GetBit(Key, 0, 3) == n111 && GetBit(Key, 5, 3) == n000)
		return true;
	return false;
}

std::string PopType3::Decode(const unsigned char* ptr, int& offset)
{
	sreg = GetBit(ptr[0], 3, 2);
	std::string ret = "pop " + getSegementReg(sreg);
	offset = 1;
	return ret;
}

bool XlatType::cmp(unsigned char Key)
{
	if (Key == 0xd7)
		return true;
	return false;
}

std::string XlatType::Decode(const unsigned char* ptr, int& offset)
{
	std::string ret = "xlat";
	offset = 1;
	return ret;
}

bool InType1::cmp(unsigned char Key)
{
	return Key == 0xE4 || Key == 0xE5;
}

std::string InType1::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	std::string ret = "in ";
	ret+=(w == 1 ? "ax," : "al,");
	port = ptr[1];
	ret += getHex(port);
	offset = 2;
	return ret;
}

bool InType2::cmp(unsigned char Key)
{
	return Key == 0xEC || Key == 0xED;
}

std::string InType2::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0],0,1);
	std::string ret = (w == 1 ? "in ax," : "in al,");
	ret += "dx";
	offset = 1;
	return ret;
}

bool OutType1::cmp(unsigned char Key)
{
	return Key == 0xe7 || Key == 0xe6;
}

std::string OutType1::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	std::string ret = "out ";
	port = ptr[1];
	ret += getHex(port);
	ret += (w == 1 ? ",ax" : ",al");
	offset = 2;
	return ret;
}

bool OutType2::cmp(unsigned char Key)
{
	return Key == 0xee || Key == 0xef;
}

std::string OutType2::Decode(const unsigned char* ptr, int& offset)
{
	w = GetBit(ptr[0], 0, 1);
	std::string ret =  "out dx,";
	ret += (w==1?"ax":"al");
	offset = 1;
	return ret;
}

bool LeaType::cmp(unsigned char Key)
{
	return Key == key;
}

std::string LeaType::Decode(const unsigned char* ptr, int& offset)
{
	std::string ret = "lea ";
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1], 3, 3);
	rm = GetBit(ptr[1], 0, 3);
	std::string Reg = getGeneralReg(reg);
	std::string Rm = getAddress(mod, rm, (unsigned char*)ptr+1);
	ret += Reg + "," + Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;

}

bool LdsType::cmp(unsigned char Key)
{

	return Key== 0xc5;
}

std::string LdsType::Decode(const unsigned char* ptr, int& offset)
{
	std::string ret = "lds ";
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1], 3, 3);
	rm = GetBit(ptr[1], 0, 3);
	std::string Reg = getGeneralReg(reg);
	std::string Rm = getAddress(mod, rm, (unsigned char*)ptr);
	ret += Reg + "," + Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;
}

bool LesType::cmp(unsigned char Key)
{
	
	return Key==key;
}

std::string LesType::Decode(const unsigned char* ptr, int& offset)
{
	std::string ret = "les ";
	mod = GetBit(ptr[1], 6, 2);
	reg = GetBit(ptr[1], 3, 3);
	rm = GetBit(ptr[1], 0, 3);
	std::string Reg = getGeneralReg(reg);
	std::string Rm = getAddress(mod, rm, (unsigned char*)ptr);
	ret += Reg + "," + Rm;
	offset = 2 + getOffset(mod, rm);
	return ret;
}

bool LahfType::cmp(unsigned char Key)
{
	return Key==0x9f;
}

std::string LahfType::Decode(const unsigned char* ptr, int& offset)
{
	offset = 1;
	return "lahf";
}

bool SahfType::cmp(unsigned char Key)
{

	return Key==0x9e;
}

std::string SahfType::Decode(const unsigned char* ptr, int& offset)
{
	offset = 1;
	return" sahf";
}

bool PushfType::cmp(unsigned char Key)
{
	return Key==0x9c;
}

std::string PushfType::Decode(const unsigned char* ptr, int& offset)
{
	offset = 1;
	return "pushf";
}

bool PopfType::cmp(unsigned char Key)
{
	return Key == 0x9d;
}

std::string PopfType::Decode(const unsigned char* ptr, int& offset)
{
	offset = 1;
	return "popf";
}

MyTransfer.cpp

代码还是比较冗余,尽管优化了一点,但还是有很多地方显得重复,代码测试较少,有bug正常

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值