Can I compile code on the fly in Delphi and execute it?

Is it possible to create an array of bytes and then run Windows as regular code? Say we have an assembly code:

inc  ecx

which is part of the program. After compiling with Nasm, we get an EXE in which the above line is converted to something like this:

00000035 41 

Is it possible to create an array of bytes, fill it with the bytes indicated above and execute - so what is the increment actually happening?

I made my super-simple interpreted language, but since it is interpreted rather slowly. I do not want to write a real compiler for it, but I would like to speed up its compilation and launch on the fly.

+5
source share
3 answers

. , , , . VirtualProtect, . VirtualAlloc , . , , . VirtualProtect, , GetMem, , . , . , DEP , .

, , , , , .

+14
const
   size = 32768;
type
   TFuncInt = function(param: Integer): Integer; // EAX -> EAX
   TByteArray = array[0..size-1] of Byte;
   PByteArray = ^TByteArray;
var
   arr: PByteArray;
   func_param: Integer;
   func_result: Integer;
begin
   arr := VirtualAlloc(nil, size, $3000, $40);
   if arr <> nil then begin
     arr[0] := $40;  // inc EAX
     arr[1] := $C3;  // ret
     func_param := 77;
     func_result := TFuncInt(arr)(func_param);  // 78
     VirtualFree(arr, 0, $8000);
  end;
end;
+7

, (DEP) . , pascal.

+1

All Articles