The Runtime Environment 421
This surprisingly simple model is really all that’s necessary to execute the compiled code of our scripts. The IP starts at zero, is evaluated at every iteration of the main loop to execute the current opcode, and is then incremented so that the next itera-tion will execute the next instruction. Finally, we also check to make sure we haven’t passed the last opcode in the stream; if we have, we take this as a sign to terminate the script (as if the Exit instruction were encountered.)
I don’t have the room to cover the implementation of all 18 opcodes in our script-ing system, so I’ll instead just cover a few. Figure 12.14 provides a visual interpreta-tion of their basic functionality as well. Fortunately, the functionality of almost all of these opcodes is relatively simple, so you shouldn’t have much trouble filling in the rest on your own. To get started, let’s take a look at what is probably the most common and fundamental instruction: Mov.
Mov works by moving the value of a source operand into a destination operand. The easy part about this is that the destination operand is always a memory reference.
Various opcodes, expressed visually
(Nothing else would make sense. Immediate values are constants, and constants, by their name alone, can’t be changed.) This means that once we know where the des-tination operand is pointing, we just need to determine exactly what the value of the source operand is and move it there.