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-10 0:13

Store an array for function pointers indexed by the opcode which calls them.
#define EAT 0x00
#define FUCK 0x01
#define DIE 0x02
#define INSTRUCTIONS 0x03
void eat(){}
void fuck(){}
void die(){}
int main(int a, char ** b){
void (*i[3])();
i[EAT]=eat; i[FUCK]=fuck; i[DIE]=die;
while(peakNextInstruction() < INSTRUCTIONS){
i[popNextInstruction()]();
}
return -1;
}

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