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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
#!/usr/bin/env python3
###
# LLM generated
###
"""
Assembler for nandgame, matching the custom disassembler above.
Syntax (as produced by print_decoded):
mnemonic[.jump] DEST, OP1[, OP2]
Examples:
mov A, #123
add.jgt D, D, A
sub _, D, M
not D, D
inc.jeq D, D
and _, D, M
xor M, D, M
DEST:
A, D, M, any combination like AD, AM, DM, ADM, or "_" for no destination.
OP1 / OP2:
D, A, M, #0 (for OP1 only), or #<number> for mov A,#imm (A-instruction).
Jumps:
jlt, jle, jeq, jne, jgt, jge, jmp, or none.
"""
import sys
ZERO = "#0"
DEST_NONE = "_"
JUMP_NONE = ""
ENDIANNESS = "little"
# mapping from mnemonic to (opcode, two_op)
MNEMONICS = {
"and": (0b000, True),
"or": (0b001, True),
"xor": (0b010, True),
"not": (0b011, False),
"add": (0b100, True),
"inc": (0b101, False),
"sub": (0b110, True),
"dec": (0b111, False),
}
# jump mnemonic -> bits 0..2
JUMP_ENCODE = {
"": 0b000,
"jgt": 0b001,
"jeq": 0b010,
"jge": 0b011,
"jlt": 0b100,
"jne": 0b101,
"jle": 0b110,
"jmp": 0b111,
}
def encode_dest(dest: str) -> int:
"""
dest is something like "A", "D", "M", "AD", "ADM", or "_" for none.
Returns bits for A,D,M in positions 5,4,3.
"""
dest = dest.strip()
if dest == DEST_NONE:
return 0
bits = 0
if "A" in dest:
bits |= (1 << 5)
if "D" in dest:
bits |= (1 << 4)
if "M" in dest:
bits |= (1 << 3)
return bits
def encode_jump(jump: str) -> int:
jump = jump.strip()
if jump not in JUMP_ENCODE:
raise ValueError(f"Unknown jump condition: {jump}")
return JUMP_ENCODE[jump]
def encode_args_two_op(op1: str, op2: str) -> int:
"""
For two-operand instructions, find zx, sw, use_mem bits that reproduce
the given op1/op2 under decode_arg1/decode_arg2.
op1 in {D, A, M, #0}
op2 in {D, A, M}
"""
op1 = op1.strip()
op2 = op2.strip()
# brute-force all combinations of zx, sw, use_mem and pick the one that matches
for zx in (0, 1):
for sw in (0, 1):
for use_mem in (0, 1):
# simulate decode_arg1/2
if zx:
dec_op1 = ZERO
else:
if not sw:
dec_op1 = "D"
else:
dec_op1 = "M" if use_mem else "A"
if sw:
dec_op2 = "D"
else:
dec_op2 = "M" if use_mem else "A"
if dec_op1 == op1 and dec_op2 == op2:
bits = 0
if use_mem:
bits |= (1 << 12)
if zx:
bits |= (1 << 7)
if sw:
bits |= (1 << 6)
return bits
raise ValueError(f"Unsupported operand combination for two-op: {op1}, {op2}")
def encode_args_one_op(op1: str) -> int:
"""
For one-operand instructions, only decode_arg1 matters.
We choose canonical encodings:
D -> zx=0, sw=0
A -> zx=0, sw=1, use_mem=0
M -> zx=0, sw=1, use_mem=1
#0 -> zx=1
"""
op1 = op1.strip()
bits = 0
if op1 == ZERO:
bits |= (1 << 7) # zx
# sw/use_mem don't matter for arg1 when zx=1, but keep them 0
return bits
if op1 == "D":
# zx=0, sw=0, use_mem=0
return bits
if op1 == "A":
bits |= (1 << 6) # sw=1
# use_mem=0
return bits
if op1 == "M":
bits |= (1 << 6) # sw=1
bits |= (1 << 12) # use_mem=1
return bits
raise ValueError(f"Unsupported operand for one-op: {op1}")
def encode_instruction(mnemonic: str, dest: str, op1: str, op2: str, jump: str) -> int:
"""
Encode a single instruction into a 16-bit integer.
"""
mnemonic = mnemonic.strip()
if mnemonic == "hlt":
return (0xFFFF & ~0x4000)
dest = dest.strip()
op1 = op1.strip()
op2 = op2.strip()
jump = jump.strip()
# A-instruction: mov A, #imm
if mnemonic == "mov":
if dest == "A" and op1.startswith("#") and not op2 and not jump:
imm_str = op1[1:]
if imm_str.startswith("0x") or imm_str.startswith("0X"):
value = int(imm_str, 16)
else:
value = int(imm_str, 10)
if not (0 <= value < 0x8000):
raise ValueError(f"Immediate out of range (0..32767): {value}")
return value & 0x7FFF
else:
raise ValueError(f"Invalid args to mov.")
# C-instruction
if mnemonic not in MNEMONICS:
raise ValueError(f"Unknown mnemonic: {mnemonic}")
opcode, two_op = MNEMONICS[mnemonic]
# bit 14, 15 = 1
ins = 0xC000
# opcode bits: low 2 bits in 8..9, high bit in 10 (ar_n_log)
low2 = opcode & 0b11
high1 = (opcode >> 2) & 0b1
ins |= (low2 << 8)
if high1:
ins |= (1 << 10)
# dest bits
ins |= encode_dest(dest)
# jump bits
ins |= encode_jump(jump)
# arg bits
if two_op:
if not op2:
raise ValueError(f"Two-op instruction {mnemonic} requires two operands")
ins |= encode_args_two_op(op1, op2)
else:
if not op1:
raise ValueError(f"One-op instruction {mnemonic} requires one operand")
ins |= encode_args_one_op(op1)
return ins
def parse_line(line: str):
"""
Parse a single assembly line into (mnemonic, dest, op1, op2, jump).
Returns None if the line is empty or comment.
"""
# strip comments starting with ';' or '#'
for sep in (";"):
idx = line.find(sep)
if idx != -1:
line = line[:idx]
line = line.strip()
if not line:
return None
# first token: mnemonic[.jump]
parts = line.split(None, 1)
if not parts:
return None
opcode_part = parts[0]
rest = parts[1] if len(parts) > 1 else ""
if "." in opcode_part:
mnemonic, jump = opcode_part.split(".", 1)
else:
mnemonic, jump = opcode_part, ""
# operands: dest, op1[, op2]
dest = ""
op1 = ""
op2 = ""
if rest:
ops = [o.strip() for o in rest.split(",")]
ops = [o for o in ops if o] # remove empty
if len(ops) >= 1:
dest = ops[0]
if len(ops) >= 2:
op1 = ops[1]
if len(ops) >= 3:
op2 = ops[2]
if len(ops) > 3:
raise ValueError(f"Too many operands: {rest}")
# normalize no-dest
if dest == "":
dest = DEST_NONE
return mnemonic, dest, op1, op2, jump
def assemble_file(in_filename: str, out_filename: str):
with open(in_filename, "r") as fin, open(out_filename, "wb") as fout:
lineno = 0
for line in fin:
lineno += 1
try:
parsed = parse_line(line)
if parsed is None:
continue
mnemonic, dest, op1, op2, jump = parsed
ins = encode_instruction(mnemonic, dest, op1, op2, jump)
fout.write(ins.to_bytes(2, byteorder=ENDIANNESS))
except Exception as e:
raise SystemExit(f"{in_filename}:{lineno}: {e}")
def main():
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} input.asm output.bin")
sys.exit(1)
assemble_file(sys.argv[1], sys.argv[2])
if __name__ == "__main__":
main()
|