News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Self Modify vs Jump Table

Started by theunknownguy, November 02, 2010, 07:59:21 PM

Previous topic - Next topic

theunknownguy

Hey all, i got a question, but first let me explain the context so you guys wont come with your usually questions: "Why would you want self modify code?" (Virus maker rolf)...

I am coding an intel sandbox emulation for the full architecture (regs, opcodes, etc). I already got it done but i am trying to optimize it, now the problem comes with this:

ADD   OR ADC SBB AND SUB XOR CMP ROL ROR RCL RCR SHL SHR SAL  SAR

This opcodes i can bind them into the same procedure that emulate the opcodes. But in order to do so i would have to make self modify code, so for example i can do:

XX DWORD PTR DS:[ADDR], REG32

Where XX can be: ADD, OR, ADC, SBB, AND, XOR & CMP

I can reduce alot the code size with this, instead of making the jump table go to each opcode emulation independent.

The penalty of self modify code is high on new processor has ive read, but in this case, wich method would be better? Treath opcodes in a bind procedure with self modify or using the jump table and treath them as individual?.

Thanks.  :U

redskull

Every single instruction you execute will be a cache miss and a pipeline flush.  It's the anti-optimization.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

theunknownguy

Quote from: redskull on November 02, 2010, 08:06:46 PM
Every single instruction you execute will be a cache miss and a pipeline flush.  It's the anti-optimization.

-r

So i should treath them has individual and copy / paste the whole procedure 7 times? for the:

ADD, OR, ADC, SBB, AND, XOR & CMP

Thanks

PS: The question would be better like: "Is there a penalty for a long badass jump table?"

redskull

If you are using a jump table, just have identical entries for each command all go to a single generalized function that is "smart" enough; e.g. decode and setup the arguments the same way, but then have some kind of switch block to perform the actual specific instruction.  Or just copy and paste.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

theunknownguy

Quote from: redskull on November 02, 2010, 08:19:38 PM

but then have some kind of switch block to perform the actual specific instruction.  Or just copy and paste.

-r

This was actually what i have now... Is this the best way to do it? (Cant think of other method)

Since i have the "smart fuction" that process everything about the opcode, but the switch table is only for "execute" the opcode.