If you do, I'd be grateful if you could let us know how & why this works:
Program SelfModify;
Const
AnzNOPs = 10;
Type
TTestProc = Procedure;
Var
code : Pointer;
Run : TTestProc;
pb : ^Byte;
pw : ^Word Absolute pb;
i : LongInt;
begin
GetMem(code, AnzNOPs + 7); {7 Bytes For proc header & end}
pb := code; {pb = ^start of routine to build}
pb^ := $55;
INC(pb); {push bp}
pw^ := $E589;
INC(pw); {mov bp,sp}
For i := 1 to AnzNOPs DO
begin
pb^ := $90;
INC(pb); {nop's}
end;
pb^ := $5D;
INC(pb); {pop bp}
pb^ := $CA;
INC(pb);
pw^ := $0000; {retf 0}
{$IFDEF DPMI}
WriteLN('Protected Mode');
code:= Ptr(Seg(code) + SelectorInc, Ofs(code)); {alias-selector}
{$else}
WriteLN('Real Mode');
{$endIF}
Run := TTestProc(code); {that's a Type-cast!}
Run; {call routine}
FreeMem(code, AnzNOPs + 7);
WriteLN('Alive and kicking!');
end.
Program SelfModify;
Const
AnzNOPs = 10;
Type
TTestProc = Procedure;
Var
code : Pointer;
Run : TTestProc;
pb : ^Byte;
pw : ^Word Absolute pb;
i : LongInt;
begin
GetMem(code, AnzNOPs + 7); {7 Bytes For proc header & end}
pb := code; {pb = ^start of routine to build}
pb^ := $55;
INC(pb); {push bp}
pw^ := $E589;
INC(pw); {mov bp,sp}
For i := 1 to AnzNOPs DO
begin
pb^ := $90;
INC(pb); {nop's}
end;
pb^ := $5D;
INC(pb); {pop bp}
pb^ := $CA;
INC(pb);
pw^ := $0000; {retf 0}
{$IFDEF DPMI}
WriteLN('Protected Mode');
code:= Ptr(Seg(code) + SelectorInc, Ofs(code)); {alias-selector}
{$else}
WriteLN('Real Mode');
{$endIF}
Run := TTestProc(code); {that's a Type-cast!}
Run; {call routine}
FreeMem(code, AnzNOPs + 7);
WriteLN('Alive and kicking!');
end.
Comment