-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptr.c
209 lines (170 loc) · 3.92 KB
/
iptr.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define UNUSED(x) ((void)(x))
#define REGISTERS_NUM 6
int reg[REGISTERS_NUM];
void opcode_addr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] + reg[argB];
}
void opcode_addi(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] + argB;
}
void opcode_mulr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] * reg[argB];
}
void opcode_muli(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] * argB;
}
void opcode_banr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] & reg[argB];
}
void opcode_bani(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] & argB;
}
void opcode_borr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] | reg[argB];
}
void opcode_bori(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] | argB;
}
void opcode_setr(const int argA, const int argB, const int argC) {
UNUSED(argB);
reg[argC] = reg[argA];
}
void opcode_seti(const int argA, const int argB, const int argC) {
UNUSED(argB);
reg[argC] = argA;
}
void opcode_gtir(const int argA, const int argB, const int argC) {
reg[argC] = argA > reg[argB];
}
void opcode_gtri(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] > argB;
}
void opcode_gtrr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] > reg[argB];
}
void opcode_eqir(const int argA, const int argB, const int argC) {
reg[argC] = argA == reg[argB];
}
void opcode_eqri(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] == argB;
}
void opcode_eqrr(const int argA, const int argB, const int argC) {
reg[argC] = reg[argA] == reg[argB];
}
enum Opcode {
OP_ADDR,
OP_ADDI,
OP_MULR,
OP_MULI,
OP_BANR,
OP_BANI,
OP_BORR,
OP_BORI,
OP_SETR,
OP_SETI,
OP_GTIR,
OP_GTRI,
OP_GTRR,
OP_EQIR,
OP_EQRI,
OP_EQRR,
_OP_COUNT_,
};
typedef void (*OpFunc)(const int argA, const int argB, const int argC);
OpFunc opfunc[_OP_COUNT_] = {
&opcode_addr,
&opcode_addi,
&opcode_mulr,
&opcode_muli,
&opcode_banr,
&opcode_bani,
&opcode_borr,
&opcode_setr,
&opcode_seti,
&opcode_gtir,
&opcode_gtri,
&opcode_gtrr,
&opcode_eqir,
&opcode_eqri,
&opcode_eqrr,
};
const char* opname[_OP_COUNT_] = {
"addr",
"addi",
"mulr",
"muli",
"banr",
"bani",
"borr",
"setr",
"seti",
"gtir",
"gtri",
"gtrr",
"eqir",
"eqri",
"eqrr",
};
struct Instruction {
enum Opcode op;
int a, b, c;
};
#define INSTRUCTIONS_MAX 64
int instructionCount = 0;
struct Instruction instr[INSTRUCTIONS_MAX];
struct {
int value;
int alias;
} iptr;
void parse_input(void) {
char buffer[128];
while(fgets(buffer, sizeof(buffer), stdin) != NULL) {
if(buffer[0] == '#') {
sscanf(buffer+3, "%d", &iptr.alias);
fprintf(stderr, "--> iptr alias: %d\n", iptr.alias);
continue;
}
buffer[4] = '\0';
int op;
for(op = 0; op < _OP_COUNT_; ++op) {
if(strcmp(buffer, opname[op]) == 0) break;
}
if(op == _OP_COUNT_) {
fprintf(stderr, "Unrecognized instruction: %c%c%c%c\n", buffer[0], buffer[1], buffer[2], buffer[3]);
exit(1);
}
int argA, argB, argC;
sscanf(buffer+5, "%d %d %d", &argA, &argB, &argC);
instr[instructionCount++] = (struct Instruction){
.op = op,
.a = argA,
.b = argB,
.c = argC
};
}
fprintf(stderr, "--> instruction count: %d\n", instructionCount);
}
int execute(void) {
while((iptr.value >= 0) && (iptr.value < instructionCount)) {
struct Instruction *i = &instr[iptr.value];
reg[iptr.alias] = iptr.value;
opfunc[i->op](i->a, i->b, i->c);
iptr.value = reg[iptr.alias];
iptr.value += 1;
}
}
int reset(void) {
iptr.value = iptr.alias = 0;
for(int r = 0; r < REGISTERS_NUM; ++r) {
reg[r] = 0;
}
}
int main(int argc, char **argv) {
reset();
parse_input();
execute();
printf("ip: %d; reg: [%d, %d, %d, %d, %d, %d]\n", iptr.value, reg[0], reg[1], reg[2], reg[3], reg[4], reg[5]);
}