From e21c6d3183c1ddeb6adf65f76b9c1edc8b44a7de Mon Sep 17 00:00:00 2001 From: Damian Recoskie Date: Tue, 21 Oct 2014 01:46:43 -0700 Subject: [PATCH] Version 1.1 Just cleaned up the decode algorithm and simplified it. --- DisassembleX86-64.js | 267 +++++++------------------------------------ 1 file changed, 40 insertions(+), 227 deletions(-) diff --git a/DisassembleX86-64.js b/DisassembleX86-64.js index 57d09e2..28a1f63 100644 --- a/DisassembleX86-64.js +++ b/DisassembleX86-64.js @@ -9,11 +9,12 @@ var t=binary.split(",");for(var i=0;i>3)&0x07; - -//if right shift 2 and 0x01=00000001 then bit tow is read for settings - -Input=(v>>2)&0x01; - -//reads reverse bit - -Flip=(v>>1)&0x01; - -//reads the force "8 bit" bit - -RegSize=v&0x01; - -//moves one position though code - -Pos+=1; - -//return the parts of the byte for the operation - -return([OPType,OP,Input,Flip,RegSize]); -} - -//else useing operations I did not cover yet - -else -{ -Pos+=1; -return([-1]); -} +if((v%8)<=5&v<=0x40){OPType=0;OP=(v>>3)&0x07;Input=(v>>2)&0x01;Flip=(v>>1)&0x01; +Force=v&0x01;Pos+=1;return([OPType,OP,Input,Flip,Force]);} +else{Pos+=1;return([-1]);} //unrecognized operation code } //********************************decode the Mod_R_M byte******************************** -function ModRM(v) -{ -Mode=(v>>6)&0x3; -R=(v>>3)&0x07; -M=v&0x07; - -Pos+=1; - -return([Mode,R,M]); -} +function ModRM(v){Mode=(v>>6)&0x3;R=(v>>3)&0x07;M=v&0x07;Pos+=1;return([Mode,R,M]);} //********************************Decode an operation******************************** @@ -163,160 +85,51 @@ function Decode(Data) { var out=""; +var OpSize=2; //set Operation size default 32 + var OPC=DeOP(Data[Pos]); -//useing operatios not covered yet +//using operations not covered yet if(OPC==-1){return("???\r\n");} //chech operation code type if "ModR_M" -if(OPC[0]==0) -{ -//decode operation code - -out=OP0[OPC[1]]+" "; - -//check if stright input operation - -if(OPC[2]==1) -{ -out+=REG[OPC[4]][0]+","+ReadInput(OPC[4]+1,0); -} - -//else the operation is not an straight input then it is MOD_R_M - -else -{ -//decode the Mod R M byte - -var ModR_M=ModRM(Data[Pos]); - -//check if mode is register with register - -if(ModR_M[0]==3) -{ -//check the reverse bit - -if(OPC[3]==0) +else if(OPC[0]==0) { -out+=REG[OPC[4]][ModR_M[2]]+","+REG[OPC[4]][ModR_M[1]]; +//check if force 8 bit is 0 -} -else -{ - -out+=REG[OPC[4]][ModR_M[1]]+","+REG[OPC[4]][ModR_M[2]]; - -} - -} - -//else it is an old Reg and Memory operation - -else -{ - -//if mode is 0 and Memory Reg is RBP straight input ram address number Displacement - -if(ModR_M[0]==0&ModR_M[2]==5) -{ - -//check reverse bit - -if(OPC[3]==0) -{ -out+=RAMS[OPC[4]*2]+ReadInput(2,0)+"],"+REG[OPC[4]][ModR_M[1]]; -} -else -{ -out+=REG[OPC[4]][ModR_M[1]]+","+RAMS[OPC[4]*2]+ReadInput(2,0)+"]"; -} - -} - -//check if RSP if so decode the next "ModRM" - -if(ModR_M[2]==4) -{ +if(OPC[4]==0){OpSize=0;} -MulM_M=ModRM(Data[Pos]); - -//check reverse bit - -if(OPC[3]==0) -{ - -//check if MEM 2 is RSP for RSP Displacement - -if(MulM_M[1]==5) -{ - -out+=RAMS[OPC[4]*2]+REG[2][MulM_M[2]]+"+"+REG[2][MulM_M[1]]+Shift[MulM_M[0]]+ReadInput(ModR_M[0],1)+"],"+REG[OPC[4]][ModR_M[1]]; - -} +//get operation code -else -{ - -out+=RAMS[OPC[4]*2]+REG[2][MulM_M[2]]+ReadInput(ModR_M[0],1)+"],"+REG[OPC[4]][ModR_M[1]]; - -} - -} - -//else it is reversed +out=OP0[OPC[1]]+" "; -else -{ +//check if straight input -if(MulM_M[1]==5) +if(OPC[2]==1) { - -out+=REG[OPC[4]][ModR_M[1]]+","+RAMS[OPC[4]*2]+REG[2][MulM_M[2]]+"+"+REG[2][MulM_M[1]]+Shift[MulM_M[0]]+ReadInput(ModR_M[0],1)+"]"; - +out+=REG[OpSize][0]+","; +if(OPC[4]==0){out+=ReadInput(1,0);}else{out+=ReadInput(2,0);} } else { -out+=REG[OPC[4]][ModR_M[1]]+","+RAMS[OPC[4]*2]+REG[2][MulM_M[2]]+ReadInput(ModR_M[0],1)+"]"; +//get the decoding of the operands -} - -} +oprands=DecodeModRM(Data,OpSize); -} - -//else there is no RSP register displacement or any displacements - -else -{ - -//check if it is not Reversed - -if(OPC[3]==0) -{ -out+=RAMS[OPC[4]*2]+REG[2][ModR_M[2]]+ReadInput(ModR_M[0],1)+"],"+REG[OPC[4]][ModR_M[1]]; -} - -//else it is reversed - -else -{ -out+=REG[OPC[4]][ModR_M[1]]+","+RAMS[OPC[4]*2]+REG[2][ModR_M[2]]+ReadInput(ModR_M[0],1)+"]"; -} - -} - -} +//flip the oprands if flip bit is set +if(OPC[3]==1){out+=oprands[1]+","+oprands[0];} +else{out+=oprands[0]+","+oprands[1];} } } -//finally return the decoded instruction +//return the decoded instruction return(out+"\r\n"); }