1 | // opcodes |
2 | |
3 | static final int |
4 | op_return = 0, |
5 | op_nop = 1, |
6 | op_jump = 2, |
7 | op_jump_rel = 16, |
8 | op_branchif = 3, |
9 | op_branchifnot = 4, |
10 | op_less = 5, |
11 | op_less_im = 17, // highest |
12 | op_equal = 6, |
13 | op_more = 7, |
14 | op_set = 8, |
15 | op_set_ind = 9, |
16 | op_copy = 10, |
17 | op_copy_ind = 11, |
18 | op_add = 12, |
19 | op_add_im = 13, |
20 | op_not = 14, |
21 | op_neg = 15, |
22 | op_illegal = 999; |
23 | |
24 | static int runProgram_maxSteps = 1000*1000; |
25 | |
26 | static void runProgram(Grid g, int pc) { |
27 | new Runner(g, pc, runProgram_maxSteps); |
28 | } |
29 | |
30 | sclass Runner { |
31 | Grid g; |
32 | int pc; |
33 | |
34 | *(Grid *g, int startAddress, int steps) { |
35 | pc = startAddress; |
36 | bool flag = false; |
37 | for (int step = 0; step < steps; step++) { |
38 | int opcode = g.get(pc++); |
39 | int a, b; |
40 | switch (opcode) { |
41 | case op_return: ret; |
42 | case op_nop: |
43 | break; case op_jump: pc = g.get(pc); |
44 | break; case op_jump_rel: pc += g.get(pc)-1; |
45 | break; case op_branchif: if (flag) pc = g.get(pc); else ++pc; |
46 | break; case op_branchifnot: if (!flag) pc = g.get(pc); else ++pc; |
47 | break; case op_less: a = y(); b = y(); flag = a < b; |
48 | break; case op_less_im: a = y(); b = x(); flag = a < b; |
49 | break; case op_equal: a = y(); b = y(); flag = a == b; |
50 | break; case op_more: a = y(); b = y(); flag = a > b; |
51 | break; case op_set: a = x(); set(x(), a); |
52 | break; case op_set_ind: a = x(); set(y(), a); |
53 | break; case op_copy: a = y(); set(x(), a); |
54 | break; case op_copy_ind: a = y(); set(y(), a); |
55 | break; case op_add: a = x(); b = x(); set(a, get(a)+get(b)); |
56 | break; case op_add_im: a = x(); b = x(); set(a, get(a)+b); |
57 | break; case op_not: a = x(); set(a, ~get(a)); |
58 | break; case op_neg: a = x(); set(a, -get(a)); |
59 | default: fail("Illegal opcode " + opcode + " at " + (pc-1)); |
60 | } |
61 | } |
62 | } |
63 | |
64 | int x() { ret get(pc++); } |
65 | int y() { ret get(get(pc++)); } |
66 | int get(int addr) { ret g.get(addr); } |
67 | void set(int addr, int x) { g.set(addr, x); } |
68 | } |
69 | |
70 | static void testProgram(Grid g, int startAddress, int... values) { |
71 | runProgram(g, startAddress); |
72 | for (int i = 0; i < l(values); i += 2) |
73 | assertEquals(values[i+1], g.get(values[i])); |
74 | //print("Program OK (" + n(l(program), "int") + ", " + n(l(values)/2, "value") + ")"); |
75 | print("Program OK (" + n(l(values)/2, "value") + ")"); |
76 | } |
77 | |
78 | static void disassemble(Grid g, int pc) { |
79 | print(new Disassembler(g, pc).buf); |
80 | } |
81 | |
82 | sclass Disassembler { |
83 | Grid g; |
84 | int pc; |
85 | new StringBuffer buf; |
86 | |
87 | *(Grid *g, int startAddress) { |
88 | pc = startAddress; |
89 | int end = g.lastNonZero(); |
90 | while (pc < end) { |
91 | int opcode = g.get(pc++); |
92 | int a, b; |
93 | S s = "?"; |
94 | switch (opcode) { |
95 | case op_return: s = "return"; |
96 | break; case op_nop: s = "nop"; |
97 | break; case op_jump: s = "jump " + x(); |
98 | break; case op_jump_rel: s = "jump rel " + x(); |
99 | break; case op_branchif: s = "branch if " + x(); |
100 | break; case op_branchifnot: s = "branch if not " + x(); |
101 | break; case op_less: a = x(); b = x(); s = "less " + a + " " + b; |
102 | break; case op_less_im: a = x(); b = x(); s = "less im " + a + " " + b; |
103 | break; case op_equal: a = x(); b = x(); s = "equal " + a + " " + b; |
104 | break; case op_more: a = x(); b = x(); s = "more " + a + " " + b; |
105 | break; case op_set: a = x(); b = x(); s = "set " + a + " " + b; |
106 | break; case op_set_ind: a = x(); b = x(); s = "set ind " + a + " " + b; |
107 | break; case op_copy: a = x(); b = x(); s = "copy " + a + " " + b; |
108 | break; case op_copy_ind: a = x(); b = x(); s = "copy ind " + a + " " + b; |
109 | break; case op_add: a = x(); b = x(); s = "add " + a + " " + b; |
110 | break; case op_add_im: a = x(); b = x(); s = "add im " + a + " " + b; |
111 | break; case op_not: s = "not " + x(); |
112 | break; case op_neg: s = "neg " + x(); |
113 | default: s = "Unknown opcode " + opcode; |
114 | } |
115 | buf.append(s).append("\n"); |
116 | } |
117 | } |
118 | |
119 | int x() { ret g.get(pc++); } |
120 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003139 |
Snippet name: | Assembly Machine (include) |
Eternal ID of this version: | #1003139/1 |
Text MD5: | cc6065310d6bffd96689b0cf95628593 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-23 14:27:00 |
Source code size: | 4109 bytes / 120 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 597 / 1366 |
Referenced in: | [show references] |