Let's Learn x86 - 64 Assembly! Part 2 - We're Writing a Virtual Machine

added 19.05.2020 00:00

Image of article 'Let's Learn x86    -    64 Assembly! Part 2     -     We're Writing a Virtual Machine'
Share

The instruction implementation is a tiny piece of code that modifies parts of QBX state (registers, memory, flags) according to the instruction's definition, and transfers execution back to the instruction fetch step.

The first one, given a list of instruction names, will define an instruction code constant for each of them: The next helper macro, given a list of instruction names, will define a jump table mapping instruction codes to their implementations: Reminder - if you're unfamiliar with the FASM macro system, I recommend reviewing part 1.

Another chunk of memory, which we'll call the "jump table", will store a mapping from QBX instruction codes to their corresponding implementations.

If the contents of QBX registers were stored in memory, we indeed could do that, and simply pass the memory address corresponding to the virtual register into the instruction implementation.

It's fairly small, but first we need to get some bootstrapping out of the way, like including files with our helper macros and defining import tables: As a next step, let's define the constants for all instruction codes by applying the macro to the macro: Next comes our data section: Here, we're first defining our jump table using the helper macro from earlier.