Archived

This topic is now archived and is closed to further replies.

wrathgame

Dynmic code

Recommended Posts

Hi all, I need to insert a machine code procedure into my app at run time .. how do i do it ? BTW the reason for this is i want to have adaptable procedures for parts of my game.. so i can add to it with ease .. its complex but makes sence if i explained in full. ps. im using delphi but i can read c if they are the only examples ? anyone point me in a direction.. im lost thanks -Tim ps.. i my solution isnt a dll really .. i want to be able to compile small asm scripts for insertion

Share this post


Link to post
Share on other sites
I''m not sure what you mean by - insert a machine code procedure

But i belive a Delphi help about COM can help you out.
Try to get a book like Delphi 4 - Unleashed ( Part about COM )
or some other...

Share this post


Link to post
Share on other sites
You can write direcly to the code segment in unprotected OS enviroments, but this only works easy under flat-model memory, such as provided by a DOS extender, as CS and DS are both mapped to the same physical address space. Under Windows, you must create an alias to the code segment before writing to it.

To actually modify the code, you need to make sure the code segment has enough room for your procedures. If you will only be changing small parts of the code, this will work nicely:

  
// Assembly code

_asm
{
mov [DWORD PTR @@landmark-4],eax
; Code
mov ebx,12345678h
}
@@landmark:

We write to the code segment 4 bytes before the landmark, which would change the value that is set in ebx at runtime. It''s better to do it from the end of the count and go backwords because we can always specify a 4-byte value, while opcodes have different sizes.

Share this post


Link to post
Share on other sites
Self-modifying code is a nightmare. It''s nearly impossible to debug. The only times I ever saw it attempted was on embedded systems where data and program memory were severely limitted. I would highly suggest trying another solution to your problem.

Share this post


Link to post
Share on other sites
I''ve been thinking about this idea for a while. Seems to me that if you created a class to store instructions and had a method to return the code for certain parameters you could write it to a string and then just doa jmp to the address of the string. Maybe I just don''t know anything at all tho.

mov.GetInst(asm::EAX,asm::Mem(ptr),CurrPtr);

there some sample for how to move a value and write the instruction to CurrPtr. Just an idea...

This would be wonderful for artifical neural networks you know..

Share this post


Link to post
Share on other sites
The best solution seems to me IS a dll, you can code your dll with asm if you want it to be asm, then use LoadLibrary-GetProcAddress to run each of your small function-procedures, this is an easier option any way you see it, unless you want to insert the actual binary Opcodes at runtime, no one is crazy enought to do that unless you are writting a buffer/stack/heap overflow exploit.

Share this post


Link to post
Share on other sites
Self-modifying code... that''s usually bad news, haven''t done much of that since the Commodore 64 days. :-)

As previously suggested, a DLL is probably a good idea, or use virtual functions or function pointers to set the functions you want to use at run time.

If you really do want to write self-modifying code... I experienced some trouble with such things moving from 386 to 486 processors because modifying the code doesn''t invalidate the current instruction pipeline, so if the code is already in the pipe your changes will not be executed. In other words, whether or not it will work is somewhat hardware dependent - the P4''s pipeline is so long, it might work on a P3 but break on a P4. Or it might work, but break on the next generation. In short, just don''t go there.

Share this post


Link to post
Share on other sites
Hi all,

thanks for the input...

I have worked it out :-) for anyone interested i did it by..

Grab a block of memory large enough for the code to be inserted in, then read from a file the machine code and place in the newly allocated space.. then simply jump to the address of the memory block :-) using assembler, easy as that. and BTW the reason im doing this and not a dll is 1) i want to be able to call procedures/functions from this dynamic code.. i have even written a small compiler to convert my code into machine code files for loading in :-)

-Tim

Share this post


Link to post
Share on other sites