[C++] 纯文本查看复制代码typedef enum{ NOP = 0, ADD = 1, SUB = 2, AND = 3, OR = 4, XOR = 5, IN = 6, NOT = 7,} CODES;void vm_exec(VM *vm, int start){ int sp; int ip; int a; int b; a = b = 0; int addr; ip = start; sp = -1; int opcode = vm->code[ip]; while(opcode != HALT && ip < vm->code_size){ ip++; switch(opcode){ case ADD: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a + b; break; case SUB: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a - b; break; case MUL: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a * b; break; case AND: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a & b; break; case OR: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a | b; break; case XOR: b = vm->stack[sp--]; a = vm->stack[sp--]; vm->stack[++sp] = a ^ b; break; case NOT: a = vm->stack[sp--]; vm->stack[++sp] = a ? false : true; break; default: printf("Invalid opcode"); exit(1); } opcode = vm->code[ip]; }}