forked from Aneurysm9/vm_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.c
138 lines (134 loc) · 2.97 KB
/
decoder.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include "constants.h"
void print_value(int n, int base) {
if (n < REG_START) {
if (base == 0x10) printf("0x%X", n);
else if (base == 0) {
if (isprint(n)) printf("'%c'", n);
else printf("'\\x%02X'", n);
}
else printf("%d", n);
} else {
printf("r%d", n % REG_START);
}
putchar(' ');
}
int decode_instruction(uint16_t ip, uint16_t *prog) {
uint16_t instr = prog[ip];
char *op;
int argc;
int base = 0x10;
switch (instr) {
case 0: // halt
op = "halt";
argc = 0;
break;
case 1: // set
op = "set";
argc = 2;
break;
case 2: // push
op = "push";
argc = 1;
base = 10;
break;
case 3: // pop
op = "pop";
argc = 1;
break;
case 4: // eq
op = "eq";
argc = 3;
base = 10;
break;
case 5: // gt
op = "gt";
argc = 3;
base = 10;
break;
case 6: // jmp
op = "jmp";
argc = 1;
break;
case 7: // jt
op = "jt";
argc = 2;
break;
case 8: //jf
op = "jf";
argc = 2;
break;
case 9: // add
op = "add";
argc = 3;
base = 10;
break;
case 10: // mult
op = "mult";
argc = 3;
base = 10;
break;
case 11: // mod
op = "mod";
argc = 3;
base = 10;
break;
case 12: // and
op = "and";
argc = 3;
break;
case 13: // or
op = "or";
argc = 3;
break;
case 14: // not
op = "not";
argc = 2;
break;
case 15: // rmem
op = "rmem";
argc = 2;
break;
case 16: // wmem
op = "wmem";
argc = 2;
break;
case 17: // call
op = "call";
argc = 1;
break;
case 18: // ret
op = "ret";
argc = 0;
break;
case 19: // out
op = "out";
argc = 1;
base = 0;
break;
case 20: // in
op = "in";
argc = 1;
break;
case 21: // noop
op = "noop";
argc = 0;
break;
default:
return 0;
}
printf("0x%04X:\t\t%s ", ip, op);
if (argc >= 1) {
print_value(prog[ip+1], base);
if (argc >= 2) {
print_value(prog[ip+2], base);
if (argc >= 3) {
print_value(prog[ip+3], base);
}
}
}
putchar('\n');
return argc;
}