I was intrigued by this paper
The Cuneiform Tablets of 2015 - Viewpoints Research Institute by
Long Tien Nguyen and
Alan Kay. This page is a javascript program that implements (part of) their simplified virtual machine and runs some example programs in their proposed bytecode. Their big goal is to archive programs in an opaque format where the decoder is a simple project of an afternoon. As below is the core of my decoder, I think they succeeded in that respect.
var opcode = program_memory[program_counter];
var A = program_memory[program_counter + 1];
var B = program_memory[program_counter + 2];
var C = program_memory[program_counter + 3];
var error = false;
switch(opcode) {
case OP_JMP: program_counter = program_memory[A] break;
case OP_JEZ:
if(program_memory[B] == 0) {
program_counter = program_memory[A];
} else {
program_counter = program_counter + 4;
}
break;
case OP_MOV: program_memory[A] = program_memory[B]; break;
case OP_GET: program_memory[A] = program_memory[program_memory[B]]; break;
case OP_SET: program_memory[program_memory[B]] = program_memory[A]; break;
case OP_ADD: program_memory[A] = program_memory[B] + program_memory[C]; break;
case OP_SUB: program_memory[A] = program_memory[B] - program_memory[C]; break;
case OP_MUL: program_memory[A] = program_memory[B] * program_memory[C]; break;
case OP_DIV: program_memory[A] = program_memory[B] / program_memory[C]; break;
case OP_MOD: program_memory[A] = program_memory[B] % program_memory[C]; break;
case OP_LT: program_memory[A] = (program_memory[B] < program_memory[C]) ? 1 : 0; break;
case OP_NAND: program_memory[A] = !(program_memory[B] && program_memory[C]); break;
default:
error = true;
output("Invalid opcode={}",opcode);
break;
}