应一个做硬件的同事的要求,他利用其他软件可以得到十六进制的机器码,希望做一个简单的软件,可以将机器
码反编译成汇编指令。本来网上应该有很多这方面的软件。但他说这个很特别,找不到,于是给他做了一个小软件
现在将代码帖出来,供网友参考
source
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.StringTokenizer;
public class HexToAssemble {
Properties orderProp = new Properties();
private String instructionPath = "e:/zip/order.properties";
String binaryCodePath = "e:/zip/input.txt";
String outputPath = "e:/zip/result.txt";
private String content;
private String baseDir = "000";
private boolean outPutFlag =true;
private int baseInt;
private void loadProp() {
InputStream inStream = null;
try {
inStream = new FileInputStream(instructionPath);
orderProp.load(inStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void readTxt() {
StringBuffer buf = new StringBuffer();
File file = new File(binaryCodePath);
try {
BufferedReader bufread = new BufferedReader(new FileReader(file));
String tmp = bufread.readLine();
while (tmp != null) {
buf.append(tmp);
tmp = bufread.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
content = buf.toString();
}
private void dealContent() {
try {
PrintWriter pw = new PrintWriter(new File(outputPath));
StringTokenizer token = new StringTokenizer(content, " ");
int tokenNumber = token.countTokens();
String[] orders = new String[tokenNumber];
int index = 0;
while (token.hasMoreElements()) {
orders[index] = token.nextToken();
index = index + 1;
}
String key = null;
String value = null;
String xValue = null;
String absoluteAddress = null;
String originalInstruction = null;
for (int i = 0; i < orders.length; i++) {
key = orders[i].toUpperCase();
xValue = null;
absoluteAddress = getAddress();
originalInstruction = key;
if ( Integer.parseInt(key, 16)>=128 )
{
xValue = key.substring(1);
key = key.substring(0, 1) +"X";
absoluteAddress = ""+absoluteAddress +": ";
}
else
absoluteAddress = absoluteAddress +": ";
value = orderProp.getProperty(key);
if( value != null)
{
StringTokenizer vs = new StringTokenizer(value,";");
String v1 = vs.nextToken();
String v2 = vs.nextToken();
if ( v2.equals("1"))
{
pw.append(absoluteAddress);
if (outPutFlag)
pw.append(" " +originalInstruction +" ");
pw.append(v1 ) .append("/r/n");
setNextAddress(1);
}
else if ( v2.equals("2"))
{
if ( i+1 <orders.length )
{
i = i+1;
originalInstruction = originalInstruction + " " + orders[i]+" ";
String muli = null;
if ( xValue != null )
{
muli = xValue + orders[i];
muli = changeHig(muli,v1);
}
else
muli = orders[i];
muli =muli+"H";
if ( v1.indexOf("expr") !=-1)
{
v1 = v1.replaceAll("expr", muli);
}
else
v1 = v1+" " + muli;
pw.append(absoluteAddress);
if (outPutFlag)
pw.append(" " + originalInstruction+" ");
pw.append( v1 ) .append("/r/n");
setNextAddress(2);
}
else
{
System.out.println("input binary code has error");
}
}
else if ( v2.equals("3"))
{
String str7c7d = orders[i];
if ( i+1 <orders.length )
{
i = i+1;
originalInstruction = originalInstruction + " " + orders[i];
String first = orders[i];
if ( v1.indexOf("expr") !=-1)
{
v1 = v1.replaceFirst("expr", first+"H");
}
if ( i+1 <orders.length )
{
i = i+1;
originalInstruction = originalInstruction + " " + orders[i];
String second = orders[i];
if ( v1.indexOf("expr") !=-1)
{
v1 = v1.replaceFirst("expr", second+"H");
}
pw.append(absoluteAddress);
if (outPutFlag)
pw.append(" " + originalInstruction+" ");
if ( str7c7d.equals("7C") || str7c7d.equals("7D"))
{
pw.append( v1 ).append(" ").append(""+first+second+"H").append("/r/n");
str7c7d = "";
}
else
pw.append( v1 ).append("/r/n");
setNextAddress(3);
}
else
{
System.out.println("input binary code has error");
}
}
else
{
System.out.println("input binary code has error");
}
}
}
else
{
System.out.println(" input binary code has error ,pls check code in 0 ..255" );
continue;
}
}
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String getAddress()
{
String returnStr = Integer.toHexString(baseInt);
return returnStr;
}
private void setNextAddress(int byteNumber)
{
baseInt = baseInt + byteNumber;
}
private String changeHig(String value,String operator)
{
//value=FFA,JZ
String returnStr = "";
int number =Integer.parseInt(value,16);
int baseNumber = Integer.parseInt(baseDir, 16);
if ( number>2047 )//80
{
value = "F"+value;
number =Integer.parseInt(value,16);
}
if (
// operator.indexOf( "JNC") ==-1 && operator.indexOf( "jnc") ==-1 &&
operator.indexOf( "INDEX") ==-1 && operator.indexOf( "index") ==-1 &&
operator.indexOf( "CALL") ==-1 && operator.indexOf( "call") ==-1
)
{
number =number+1;
}
else
{
number =number+2;
}
returnStr = Integer.toHexString( (number + baseInt) );
if (returnStr.length()==5)
returnStr =returnStr.substring(1);
return returnStr;
}
public static void main(String[] args) {
HexToAssemble hta = new HexToAssemble();
// hta.loadProp();
// hta.readTxt();
// hta.dealContent();
if ( args.length ==5)
{
hta.setBaseDir(args[0]);
hta.setInstructionPath(args[1]);
hta.setBinaryCodePath(args[2] );
hta.setOutputPath(args[3]);
hta.setOutPutFlag(args[4]);
hta.loadProp();
hta.readTxt();
hta.dealContent();
}
else
{
System.out.println(" ");
System.out.println("===============================================================================");
System.out.println("useage: java HexToAssemble baseAddress instrunction inputBinaryCode outputFile 1/0");
System.out.println("java HexToAssemble 600 instruction.txt input.txt result.txt 1 ");
System.out.println("===============================================================================");
}
}
public String getBaseDir() {
return baseDir;
}
public void setBaseDir(String baseDir) {
this.baseDir = baseDir;
baseInt = Integer.parseInt(baseDir, 16);
}
public String getInstructionPath() {
return instructionPath;
}
public void setInstructionPath(String instructionPath) {
this.instructionPath = instructionPath;
}
public String getBinaryCodePath() {
return binaryCodePath;
}
public void setBinaryCodePath(String binaryCodePath) {
this.binaryCodePath = binaryCodePath;
}
public String getOutputPath() {
return outputPath;
}
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
}
public boolean isOutPutFlag() {
return outPutFlag;
}
public void setOutPutFlag(String outPutFlag) {
if (outPutFlag.equals("1"))
this.outPutFlag = true;
else
this.outPutFlag = false;
}
}
========================================================================
instruction.txt
00=SSC;1
01=ADD A,expr;2
02=ADD A,[expr];2
03=ADD A,[X+expr];2
04=ADD [expr],A;2
05=ADD [X+expr],A;2
06=ADD [expr],expr;3
07=ADD [X+expr],expr;3
08=PUSH A;1
09=ADC A,expr;2
0A=ADC A,[expr];2
0B=ADC A,[X+expr];2
0C=ADC [expr],A;2
0D=ADC [X+expr],A;2
0E=ADC [expr],expr;3
0F=ADC [X+expr],expr;3
10=PUSH X;1
11=SUB A,expr;2
12=SUB A,[expr];2
13=SUB A,[X+expr];2
14=SUB [expr],A;2
15=SUB [X+expr],A;2
16=SUB [expr],expr;3
17=SUB [X+expr],expr;3
18=POP A;1
19=SBB A,expr;2
1A=SBB A,[expr];2
1B=SBB A,[X+expr];2
1C=SBB [expr],A;2
1D=SBB [X+expr],A;2
1E=SBB [expr],expr;3
1F=SBB [X+expr],expr;3
20=POP X;1
21=AND A,expr;2
22=AND A,[expr];2
23=AND A,[X+expr];2
24=AND [expr],A;2
25=AND [X+expr],A;2
26=AND [expr],expr;3
27=AND [X+expr],expr;3
28=ROMX;1
29=OR A,expr;2
2A=OR A,[expr];2
2B=OR A,[X+expr];2
2C=OR [expr],A;2
2D=OR [X+expr],A;2
2E=OR [expr],expr;3
2F=OR [X+expr],expr;3
30=HALT;1
31=XOR A,expr;2
32=XOR A,[expr];2
33=XOR A,[X+expr];2
34=XOR [expr],A;2
35=XOR [X+expr],A;2
36=XOR [expr],expr;3
37=XOR [X+expr],expr;3
38=ADD SP,expr;2
39=CMP A,expr;2
3A=CMP A,[expr];2
3B=CMP A,[X+exr];2
3C=CMP [expr],expr;3
3D=CMP [X+expr],expr;3
3E=MVI A,[expr];2
3F=MVI [expr],A;2
40=NOP;1
41=AND REG[expr],expr;3
42=AND REG[X+expr],expr;3
43=OR REG[expr],expr;3
44=OR REG[X+expr],expr;3
45=XOR REG[expr],expr;3
46=XOR REG[X+expr],expr;3
47=TST [expr],expr;3
48=TST [X+expr],expr;3
49=TST REG[expr],expr;3
4A=TST REG[X+expr],expr;3
4B=SWAP A,X;1
4C=SWAP A,[expr];2
4D=SWAP X,[expr];2
4E=SWAP A,SP;1
4F=MOV X,SP;1
50=MOV A,expr;2
51=MOV A,[expr];2
52=MOV A,[X+expr];2
53=MOV [expr],A;2
54=MOV [X+expr],A;2
55=MOV [expr],expr;3
56=MOV [X+expr],expr;3
57=MOV X,expr;2
58=MOV X,[expr];2
59=MOV X,[X+expr];2
5A=MOV [expr],X;2
5B=MOV A,X;1
5C=MOV X,A;1
5D=MOV A,REG[expr];2
5E=MOV A,REG[X+expr];2
5F=MOV [expr],[expr];3
60=MOV REG[expr],A;2
61=MOV REG[X+expr],A;2
62=MOV REG[expr],expr;3
63=MOV REG[X+expr],expr;3
64=ASL A;1
65=ASL [expr];2
66=ASL [X+expr];2
67=ASR A;1
68=ASR [expr];2
69=ASR [X+expr];2
6A=RLC A;1
6B=RLC [expr];2
6C=RLC [X+expr];2
6D=RRC A;1
6E=RRC [expr];2
6F=RRC [X+expr];2
70=AND F,expr;2
71=OR F,expr;2
72=XOR F,expr;2
73=CPL A;1
74=INC A;1
75=INC X;1
76=INC [expr];2
77=INC [X+expr];2
78=DEC A;1
79=DEC X;1
7A=DEC [expr];2
7B=DEC [X+expr];2
7C=LCALL;3
7D=LJMP;3
7E=RETI;1
7F=RET;1
8X=JMP;2
9X=CALL;2
AX=JZ;2
BX=JNZ;2
CX=JC;2
DX=JNC;2
EX=JACC;2
FX=INDEX;2
============================================================
input.txt
7c 22 8E
7C 0F EC
50 57
7C 03 C5
7C 29 06
50 4F
7C 03 C5
7C 29 06
50 57
7C 03 C5
7C 29 06
7C 03 D1
21 08
AF FA
7C 03 CE
53 91 7C 03 D1 21 08 AF
FA 7C 03 CE 5C 51 91 7C
04 A8 7C 0F EC 8F E0 7D
20 AF 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30