Re: Run time code execution



any respect for this being my first real ASM endeavor? *grin*

Here's an all-asm version. I'm not sure how to do the try...finally in ASM.
Can you guys shed some light on that?

procedure TForm1.DoAsm3;
var
FMemorySize: integer;
FValue: integer;
FOldProtect: pointer;
FCodeMemory: PChar;
FHandle: integer;
begin
DoStart;
try
asm
MOV FMemorySize, 11


// Call VirtualAlloc, which returns the pointer to the base of the
allocated
// memory in EAX
PUSH PAGE_EXECUTE_READWRITE
PUSH MEM_COMMIT
MOV EAX, [FMemorySize]
PUSH EAX
PUSH 0
CALL VirtualAlloc
MOV FCodeMemory, EAX
// Call virtual lock so nothing else can update the value

PUSH FMemorySize
PUSH FCodeMemory
CALL VirtualLock

// Load up the "dynamic code"
MOV EAX, FCodeMemory
MOV BYTE PTR [EAX], $B8
INC EAX
MOV BYTE PTR [EAX], $80
INC EAX
MOV BYTE PTR [EAX], $96
INC EAX
MOV BYTE PTR [EAX], $98
INC EAX
MOV BYTE PTR [EAX], $00 // MOV EAX, $00989680
INC EAX
MOV BYTE PTR [EAX], $FF
INC EAX
MOV BYTE PTR [EAX], $03 // FF03 inc dword ptr [ebx]
INC EAX
MOV BYTE PTR [EAX], $48 // DEC EAX
INC EAX
MOV BYTE PTR [EAX], $75
INC EAX
MOV BYTE PTR [EAX], $FB // JNZ -$05
INC EAX
MOV BYTE PTR [EAX], $C3 // RET

// Change the permissions on the memory to only PAGE_EXECUTE

PUSH [FOldProtect]
PUSH PAGE_EXECUTE
PUSH FMemorySize
PUSH FCodeMemory
CALL VirtualProtect

// Call GetCurrentProcess to get the current process
CALL GetCurrentProcess
MOV FHandle, EAX

// Call FlushInstructionCache
PUSH 0
PUSH 0
PUSH FHandle
CALL FlushInstructionCache

MOV FValue, 0

PUSH EBX
LEA EBX, [FValue]
CALL DWORD PTR FCodeMemory
POP EBX

end;
finally
if FCodeMemory <> nil then
VirtualFree(FCodeMemory, FMemorySize, MEM_RELEASE);
end;
DoEnd;
Memo1.LInes.Add(IntToStr(FValue));
end;


"Les Pawelczyk" <les_at_pixelpointpos_dot_com> wrote in message
news:42fa2b38$1@xxxxxxxxxxxxxxxxxxxxxxxxx
>> With all due respect, your code is garbage :)
>
> Hey, I'll take all the respect you can muster. :) The code is based on
> original post by Malcolm in "...delphi.general". Everything else seemed to
> be working so I was only paying attention to the 'asm' block.
>
>
>> You're committing whatever memory FMemory happens to point to on entry
>> and
>> using that. Typically, it will point to a stack location, and my guess is
>> that this is what messes up the caching logic on the chip and hence
>> causes
>> the slowdown.
>
> That's exactly what it does. The code and the data are sharing the same
> cache line. A no-no.
>
>
> Les.
>
>


.



Relevant Pages

  • Re: Macro2D
    ... õ1, eax", 0 ... db "push striIIi", NL ... db ".2: cmp edi, ebp", NL ... db ".4: mov eax, edi", NL ...
    (alt.lang.asm)
  • One RosAsm Pre-parser
    ... cmp eax 0 | ja L0<< ... mov ecx, D ...
    (alt.lang.asm)
  • Re: Macro2D
    ... mov, eax ... cmp eax, -1 ... push STD_INPUT_HANDLE ...
    (alt.lang.asm)
  • ascii to st0
    ... push IDC_ARROW ... cmp eax, 0 ... mov ebp, esp ... cmp dword @Message, WM_CLOSE ...
    (alt.lang.asm)
  • Re: Insert a byte
    ... push ebx ... mov ebx, dword ... cmp dword, 0 ... ..e0: xor eax, eax ...
    (alt.lang.asm)