Fetch - Execute
|Instructions appear in memory in sequences that may or may not be contiguous. All instructions share common formatting and behaviour. Therefore:|
|Write one sequence of code to fetch and decode instructions. Dispatch to handlers for each instruction after the common code had completed. These execute the variable part of the instructions and return to the fetch when complete. Increment the program counter at the end of the fetch sequence so that, for example, a jump instruction handler can simply replace the anticipated next instruction address with the correct one.|
|Computer hardware often follows the same pattern with fetch and pc
increment occuring on one phase of a cycle and instruction execution occuring
Typically more effort goes into optimizing the fetch sequence of simulators because improvements there effect every instruction. One variation is to duplicate the fetch cycle after every instruction handler and to fall into it without a jump. This may or may not be a good strategy depending on cache size and behavior.