Jump to content
  • Advertisement

Archived

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

baskuenen

Self modifying code

This topic is 6224 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey fellow proggers, I''ve got a question concerning self modifying code in MSVC. When I modify the index in an instruction width inline assembly, I get an Access Violation. How do I fix this? Bas.

Share this post


Link to post
Share on other sites
Advertisement
I sure as hell am not telling you, you''re programming viruses again aren''t you


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
NO, NEVER, NIE, NOP!!!!

I want to use it for optimizing the inner loop of my 3D Engine.
I''m a little short on regs and want to use it like this:

add eax, xxxxxxxx
add ebx, xxxxxxxx

Setting interpolation values in them.
Speed is what I''m interrested in!
Bas.


P.S.: I must confess in the old days I wrote a virus once. But it was .com files only. Only to prove I could (virus wasn''t harmfull)
Today I despise these people: "building something is hard, to destroy something is easy."

Share this post


Link to post
Share on other sites
Its probably because the memory that the actual code sits in is protected, ie you''re not meant to be able to write to it...

You should be able to define a new Data? section and tell the linker to put the code in there, rather than in the protected section....or, apparently you can tell the linker in Visual C to make a specific section readable....


but IMHO you ought to spend your time speeding the code up in other ways such as trying to improve the algorithm, which code produce a dramatic improvement in speed, rather than trying to get a small improvement by making self-modifying code, which will then be very hard to maintain.


Also just noted, one of the options is:

"P Pageable Marks the section as not pageable "

Hmm, a way to stop windows from putting the exe to swap disk? Does anybody have a ver large excutable ( and not much memory ) they can test this with?

if you define /SECTION:.text,PER (non-pageable, executable, and allow read operations) it should stop the actual executable from being swapped out....


Game production:
Good, quick, cheap: Choose two.

Share this post


Link to post
Share on other sites
How is writing to the code going to be any faster than writing to a data variable?

Even if you can fake the compiler/linker into letting you write to this ''code'' segment, it pretty much won''t work anymore. I''m almost certain the code cache is not writable. So either your change won''t get out to memory, or the previously loaded code is going to be executed again anyways because its still in the cache. It may not be impossible, but you aren''t going to get a speed increase. Those days are long gone.

Rock

Share this post


Link to post
Share on other sites
oh dear.. protected mode SMC. you don''t wanna go there. you could force cpl0 (doesnt need to be a vxd, there''s info floating around on how to do it), but isn''t that a bit of an overkill?

-goltrpoat


--
Float like a butterfly, bite like a crocodile.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Probably VirtualAlloc(), VirtualProtect() or APIs related to them provide the way to permit the code you create dynamically in run time to be executed.

Of course, I also guess we should improve its algorithm to speed program up first of all, in nine cases out of ten. Don''t ask me details. In fact, I have never written SMC under Win32 actually, though I had done that in X68000(MC68000) years ago.

Kwanji - a game developer in Japan

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
VirtualProtect and FlushInstructionCache is the way to go.

Look here for some SMC tips:

http://www.geocities.com/pcgpe/selfmod.html

Share this post


Link to post
Share on other sites
This optimization is highly unlikely to speed anything up, since you''re now doing memory writes to change the code. You might as well have saved your registers into memory in the first place (which would probably be better for cache coherence).

But if you really wanna do it, I think the easiest way (though it''s been a while since I''ve needed to do this) is to put the code into a variable in your data space, and just do a jump into the variable.

I wanna reiterate, though. There are good reasons to write self-modifying code, but this probably isn''t one of them.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!