1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| int eval() { int op, *tmp; cycle = 0; while (1) { cycle ++; op = *pc++;
if (debug) printf("%d> %.4s", cycle, & "LEA ,IMM ,JMP ,CALL,JZ ,JNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PUSH," "OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ," "OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT"[op * 5]); if (op <= ADJ) printf(" %d\n", *pc); else printf("\n"); } if (op == IMM) {ax = *pc++;} else if (op == LC) {ax = *(char *)ax;} else if (op == LI) {ax = *(int *)ax;} else if (op == SC) {ax = *(char *)*sp++ = ax;} else if (op == SI) {*(int *)*sp++ = ax;} else if (op == PUSH) {*--sp = ax;} else if (op == JMP) {pc = (int *)*pc;} else if (op == JZ) {pc = ax ? pc + 1 : (int *)*pc;} else if (op == JNZ) {pc = ax ? (int *)*pc : pc + 1;} else if (op == CALL) {*--sp = (int)(pc+1); pc = (int *)*pc;} else if (op == ENT) {*--sp = (int)bp; bp = sp; sp = sp - *pc++;} else if (op == ADJ) {sp = sp + *pc++;} else if (op == LEV) {sp = bp; bp = (int *)*sp++; pc = (int *)*sp++;} else if (op == LEA) {ax = (int)(bp + *pc++);}
else if (op == OR) ax = *sp++ | ax; else if (op == XOR) ax = *sp++ ^ ax; else if (op == AND) ax = *sp++ & ax; else if (op == EQ) ax = *sp++ == ax; else if (op == NE) ax = *sp++ != ax; else if (op == LT) ax = *sp++ < ax; else if (op == LE) ax = *sp++ <= ax; else if (op == GT) ax = *sp++ > ax; else if (op == GE) ax = *sp++ >= ax; else if (op == SHL) ax = *sp++ << ax; else if (op == SHR) ax = *sp++ >> ax; else if (op == ADD) ax = *sp++ + ax; else if (op == SUB) ax = *sp++ - ax; else if (op == MUL) ax = *sp++ * ax; else if (op == DIV) ax = *sp++ / ax; else if (op == MOD) ax = *sp++ % ax;
else if (op == EXIT) { printf("exit(%d)", *sp); return *sp;} else if (op == OPEN) { ax = open((char *)sp[1], sp[0]); } else if (op == CLOS) { ax = close(*sp);} else if (op == READ) { ax = read(sp[2], (char *)sp[1], *sp); } else if (op == PRTF) { tmp = sp + pc[1]; ax = printf((char *)tmp[-1], tmp[-2], tmp[-3], tmp[-4], tmp[-5], tmp[-6]); } else if (op == MALC) { ax = (int)malloc(*sp);} else if (op == MSET) { ax = (int)memset((char *)sp[2], sp[1], *sp);} else if (op == MCMP) { ax = memcmp((char *)sp[2], (char *)sp[1], *sp);} else { printf("unknown instruction:%d\n", op); return -1; } } }
|