Generate Opcode for jumps

delphi package - full disassembler
Post Reply
shadow110
Posts: 8
Joined: Tue Apr 27, 2004 6:26 pm

Generate Opcode for jumps

Post by shadow110 »

Hello.

I would like to generate the opcode for jump instructions.

Something like

x := genopcode(0040000, Jmp 0050000);
// x = E9FBFF0000


x := genopcode(0001000, Jnp 0050000);
// x = 0F8BFAEF0400

Mayby it´s alread builtin?

Greetings
Alex
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

No, sorry, that's not built in.
neji
Posts: 155
Joined: Wed Mar 09, 2005 11:39 am
Contact:

Post by neji »

hallo madshi, if you want you can use this code to build this function

Code: Select all

var
JmpOPC:array[0..25]of string = ('jne' ,'je' ,'jmp','jo','jno','jnae','jb'  ,'jae' ,'jnb' ,'jz'  ,'jnz' ,'jna' ,'jbe' ,'ja'  ,'jnbe','js','jns','jnge','jl'  ,'jge' ,'jnl' ,'jng' ,'jle','jg','jnle','call');
JmpHex:array[0..25]of string = ('0F85','0F84','EB' ,'0F80','0F81' ,'0F82','0F82','0F83','0F83','0F84','0F85','0F86','0F86','0F87','0F87','0F88','0F89' ,'0F8C','0F8C','0F8D','0F8D','0F8E','0F8E','0F8F','0F8F','E8');
JmpHE2:array[0..25]of String = ('75'  ,'74'  ,'E9' ,'70','71' ,'72' ,'72'  , '73' ,'73'  ,'74'  ,'75'  ,'76'  ,'76'  ,'77'  ,'77' ,'78','79' ,'7C'  ,'7C','7D'  ,'7D'  ,'7E'  ,'7E','7F','7F','E8');


function TranslateJMP(StartAdress:Integer; Line:String):String;
function GetRelation(StartAdress, AimAdress, OpCodeLength, SecOpCodeLength:Integer; var TrySecOPC:boolean; ForceLong:boolean=false):string;
var tmp:String;
i:Integer;
begin
  result := '';
  tmp := IntToHex((AimAdress - StartAdress - (OpCodeLength+4)), 4);
  TrySecOPC := ((StartAdress - AimAdress) >= -129) and ((StartAdress - AimAdress) <= 126);
  if TrySecOPC then
  begin
    tmp := IntToHex((AimAdress - StartAdress - (SecOpCodeLength+1)), 1);
  end;
  if length(tmp) mod 2 <> 0 then tmp := '0'+tmp;
  for i:= round(length(tmp)/2) downto 1 do
    result := result + Tmp[2*i-1] + Tmp[2*i];
  if TrySecOPC then
    result := result[1] + result[2];
  while Length(result) < ifthen(TrySecOPC,2,8) do
    result := result + '0';
end;
var Tmp:string;
    Aim:string;
    secCode:boolean;
    jmpid,i:integer;
begin
  Tmp := LeftStr(Line,(Pos(' ',Line)-1));
  Tmp := LowerCase(tmp);
  For I := 0 to Length(jmpopc) - 1 do
    if Tmp = jmpopc[I] then
      jmpid := i;
  Aim := RightStr(Line,(Length(Line)-Pos(' ',Line)-2));
  Aim := GetRelation(StartAdress,StrToInt('$'+Aim),round(Length(JmpHex[jmpid])/2),round(Length(JmpHE2[jmpid])/2),Seccode,(tmp = 'call'));
  result := IfThen(SecCode,JmpHe2[jmpid],JmpHex[jmpid])+Aim ;
end;
Post Reply