Re: Run time code execution
- From: "Malcolm Toon" <no@xxxxxx>
- Date: Wed, 10 Aug 2005 12:41:37 -0400
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.
>
>
.
- References:
- Run time code execution
- From: Malcolm Toon
- Re: Run time code execution
- From: Avatar Zondertau
- Re: Run time code execution
- From: Malcolm Toon
- Re: Run time code execution
- From: Les Pawelczyk
- Re: Run time code execution
- From: Per Larsen
- Re: Run time code execution
- From: Les Pawelczyk
- Run time code execution
- Prev by Date: Re: Run time code execution
- Next by Date: Re: Fastcode LowerCase B&V 3.0
- Previous by thread: Re: Run time code execution
- Next by thread: Re: Run time code execution
- Index(es):
Relevant Pages
|