Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

VM instruction implementation without huge switch()?

Name: Anonymous 2014-06-09 23:26

How would one go about making a virtual machine without using a huge and clumsy switch( opcode ) statement?

The architecture I'm implementing (DCPU-16) has about 35-40 different instructions.

Name: Anonymous 2014-06-11 5:39

>>1
You can do function pointers or threaded code

Threaded code basically jumps to the code that implements the next instruction directly without a dispatch.

prog = { op_add, op_add, op_sub, ... };
op_add:
reg_a = reg_b + reg_a;
goto *ins_stream++;

op_sub:
reg_a = reg_a - reg_b;
goto *ins_stream++;

etc...

requires gcc's labels as values extensions to implement in C. You can use function pointers for the same idea, but it might be slower.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List