Anand
Anand

Reputation: 729

Implementing a simulator for a subset of x86

I wish to implement a simulator for a subset of instructions for the x86 architecture. Given a binary, I wish to disassemble it and run a simulation on the instructions. For that, one would need to look at certain bits of an instruction to decide whether it is a control instruction, arithmetic instruction or a logical instruction and based on that, one must derive the parameters of the operation by looking at the remaining bits. One obvious yet painful way to implement this is by using nested if-else/switch-case statements. Can someone suggest a better methodology for implementing this?

Upvotes: 3

Views: 632

Answers (3)

Gabriel Southern
Gabriel Southern

Reputation: 10063

Doing a nested if/else type construct should be fine if you cache the output of the translation. If you are doing simulation you will have relatively few dynamic instructions out of all of the static instructions in the program. So the best performance optimization is to cache the output of the translation and then reuse it when the dynamic instruction executes. Eventually your cache will fill up and you will need to clear it for new entries. But it makes more sense to cache the translation somehow, rather than try to come up with a really fast method of doing the translation in the first place.

As an example QEMU is an emulator that supports a variety of targets that is optimized for performance. You can see how they translate x86 instructions here:

https://github.com/qemu/QEMU/blob/master/target-i386/translate.c#L4076

if QEMU did this for every instruction the performance would be very slow. But since the cache the results it does not matter too much that the first time an instruction is translated there is a complex case statement.

Upvotes: 1

StilesCrisis
StilesCrisis

Reputation: 16290

You can look at the source of an x86 emulator to find an implementation of this idea, already fully written and fleshed out.

Here's one you might try: http://www.dosbox.com/wiki/BuildingDOSBox#1._Grab_the_source

Let me know if this doesn't work out; there are lots to choose from.

In general, with an emulator, I would think that a switch on the opcode would be one way to go. Another good approach would be an 256-entry array of function pointers, corresponding to the first byte of the instruction. That gives a little more separation than a giant switch or if block. Of course you can reuse the functions as needed.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

Use a lookup table, perhaps in the form of a std::map.

Upvotes: 3

Related Questions