11 typedef struct state state
;
19 char *program
[50]; // sized based on pre-inspecting file
23 get (state
*s
, const char *value
)
26 return s
->regs
[*value
- 'a'];
31 set (state
*s
, const char *reg
, long long value
)
33 s
->regs
[*reg
- 'a'] = value
;
35 printf (" %s=%lld\n", reg
, value
);
39 run (state
*s
, int id
)
41 while (s
->pc
< instr
) {
42 char arg1
[10], arg2
[10];
44 printf ("p%d instr %d at pc %d: %s\n", id
, s
->count
, s
->pc
,
46 if (!(s
->count
% 10000))
47 printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%10d h=%lld", s
->count
,
50 printf ("\nstarting computation at count %d,", s
->count
);
51 for (int i
= 'a'; i
<= 'h'; i
++)
52 printf (" %c=%lld", i
, s
->regs
[i
- 'a']);
55 sscanf (program
[s
->pc
], "%*s %9s %9s", arg1
, arg2
);
56 switch ((program
[s
->pc
][0] << 8) + program
[s
->pc
][1]) {
57 case ('s' << 8) + 'e': // set X Y
58 set (s
, arg1
, get (s
, arg2
));
60 case ('s' << 8) + 'u': // sub X Y
61 set (s
, arg1
, get (s
, arg1
) - get (s
, arg2
));
63 case ('a' << 8) + 'd': // add X Y
64 set (s
, arg1
, get (s
, arg1
) + get (s
, arg2
));
66 case ('m' << 8) + 'u': // mul X Y
68 set (s
, arg1
, get (s
, arg1
) * get (s
, arg2
));
70 case ('m' << 8) + 'o': // mod X Y
71 set (s
, arg1
, get (s
, arg1
) % get (s
, arg2
));
73 case ('j' << 8) + 'n': // jnz X Y
75 s
->pc
+= get (s
, arg2
) - 1;
86 int main(int argc
, char **argv
)
88 char buf
[400]; // sized based on pre-inspecting file
89 int nread
= fread (buf
, 1, sizeof buf
, stdin
);
91 while (p
< buf
+ nread
) {
96 printf ("program consists of %d instructions\n", instr
);
99 states
.regs
[0] = atoi (argv
[1]);
101 states
.regs
[1] = atoi (argv
[2]);
105 states
.regs
[2] = atoi (argv
[3]);
107 printf ("\nafter %d operations, reg h is %lld\n", states
.count
,
108 states
.regs
['h' - 'a']);
114 int main(int argc
, char **argv
)
116 int a
= 1, b
= 0, c
= 0, d
= 0, e
= 0, f
= 0, g
= 0, h
= 0;
162 printf ("b=%d c=%d d=%d e=%d f=%d g=%d h=%d\n", b
, c
, d
, e
, f
, g
, h
);
186 printf (" b=%d c=%d d=%d e=%d f=%d g=%d h=%d\n", b
, c
, d
, e
, f
, g
, h
);
202 printf ("final value of h: %d\n", h
);