forked from denhood/CS271_VM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_to_hack1.py
More file actions
361 lines (344 loc) · 10.4 KB
/
vm_to_hack1.py
File metadata and controls
361 lines (344 loc) · 10.4 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
################################################################################
# GLOBAL VARIABLE ALERT! #
labelCount = 0 #need this global variable to handle address labels in eq
################################################################################
def open_file(fileName):
fileObj = open(fileName,'w')
return fileObj
def write_to_file(string, fileObj):
fileObj.write(string)
return
def close_file(fileName): #create an infinite assembly loop and close file
fileName.write("\n(END)\n@END\n0;JMP")
fileName.close()
return
def setup(fileName): #setup puts starting address of stack in SP
fileName.write("@256\n"+\
"D=A\n"+\
"@SP\n"+\
"M=D")
return
def push(memSeg, offset): #NOTE: only complete segment is constant
if memSeg == 'constant': #write the value of offset to the stack
#offset is a value to put on stack
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'static': #base address IS RAM[16]
#get the value at static
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'that': #base address of area in heap stored in RAM[4]
#get the value at THAT
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeAg == 'this': #base address of area in heap stored in RAM[3]
#get the value at THIS
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'argument': #base address of area in heap stored in RAM[2]
#get the value at ARG
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'local': #base address of area in heap stored in RAM[1]
#get the value of LCL
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'pointer': #base address of area in heap stored in RAM[?]
#get the value of LCL
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
elif memSeg == 'temp': #Hold the contents of temp segment in RAM[5]
#get the value of LCL
return('\n@'+offset+\
'\nD=A'+\
'\n@SP'+\
'\nA=M'+\
'\nM=D'+\
'\n@SP'+\
'\nM=M+1')
else:
return ('seg not found')
def pop():
pass
def add():
#add the last two values on the stack
string = ""
#decrement the stack pointer to last value that was pushed on
string += "\n@SP"+\
"\nM=M-1"
#load that value in D register
string += "\nA=M"+\
"\nD=M"
#decrement the stack pointer to next to last value
string += "\n@SP"+\
"\nM=M-1"
#load that value in the A register
string += "\nA=M"+\
"\nA=M"
#add two values and put in D register
string += "\nD=D+A"
#write D register contents to memory and advance stack pointer
string += "\n@SP"+\
"\nA=M"+\
"\nM=D"+\
"\n@SP"+\
"\nM=M+1"
return string
def sub():
string = ""
#load the stack pointer and decrement to last value pushed and put in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#load next to last value and put in A register
string += "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#subtract D register from A register and store in D register
string += "\nD=A-D"
#put result in location of memory addressed by SP
string += "\n@SP"+\
"\nA=M"+\
"\nM=D"+\
"\n@SP"+\
"\nM=M+1"
return string
def neg():
string = ""
#load the SP and decrement to last value
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"
#negate value
string += "\nM=-M"
#advance SP
string += "\n@SP"+\
"\nM=M+1"
return string
def eq():
global labelCount #forgive me Batman for I have sinned
labelCount += 1 #NEED TO HAVE AN INCREMENTING VARIABLE AND CONCATENATE WITH ADDRESS LABELS ex:(AROUND1)
string = ""
#load last two values off stack into D and A registers
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"+\
"\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#subtract the two registers, if D=A then D-A=0
string += "\nD=D-A"
#check to see if this is zero if it is, jump to (AROUND), else set memory to FALSE (0)
string += "\n@AROUND"+str(labelCount)+\
"\nD;JEQ"+\
"\n@SP"+\
"\nA=M"+\
"\nM=0"+\
"\n@END_COMP"+str(labelCount)+\
"\n0;JMP"
#set value in memory to TRUE (-1)
string += "\n(AROUND"+str(labelCount)+")"+\
"\n@SP"+\
"\nA=M"+\
"\nM=-1"
#after setting memory to TRUE or FALSE, JUMP here to increment SP
string += "\n(END_COMP"+str(labelCount)+")"+\
"\n@SP"+\
"\nM=M+1"
return string
def gt():
global labelCount #forgive me Batman for I have sinned
labelCount += 1#NEED TO HAVE AN INCREMENTING VARIABLE AND CONCATENATE WITH ADDRESS LABELS ex:(AROUND1)
string = ""
#load last value of stack in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#load second to last value in A register
string += "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#if A>D then A-D must be positive
string += "\nD=A-D"
#if A-D is NOT greater than 0, return FALSE (0)
string += "\n@AROUND"+str(labelCount)+\
"\nD;JGT"+\
"\n@SP"+\
"\nA=M"+\
"\nM=0"+\
"\n@END_COMP"+str(labelCount)+\
"\n0;JMP"
#if A-D is positive, return TRUE (-1)
string += "\n(AROUND"+str(labelCount)+")"+\
"\n@SP"+\
"\nA=M"+\
"\nM=-1"
#increment SP
string += "\n(END_COMP"+str(labelCount)+")"+\
"\n@SP"+\
"\nM=M+1"
return string
def lt():
global labelCount #forgive me Batman for I have sinned
labelCount += 1#NEED TO HAVE AN INCREMENTING VARIABLE AND CONCATENATE WITH ADDRESS LABELS ex:(AROUND1)
string = ""
#load last value of stack in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#load second to last value in A register
string += "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#if A<D then A-D must be negative
string += "\nD=A-D"
#if A-D is NOT less than 0, return FALSE (0)
string += "\n@AROUND"+str(labelCount)+\
"\nD;JLT"+\
"\n@SP"+\
"\nA=M"+\
"\nM=0"+\
"\n@END_COMP"+str(labelCount)+\
"\n0;JMP"
#if A-D is negative, return TRUE (-1)
string = string + "\n(AROUND"+str(labelCount)+")"+\
"\n@SP"+\
"\nA=M"+\
"\nM=-1"
#increment SP
string += "\n(END_COMP"+str(labelCount)+")"+\
"\n@SP"+\
"\nM=M+1"
return string
def And():
string = ""
#load last value of stack in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#load next to last value of stack in A register
string += "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#store result of D&A in D register
string += "\nD=D&A"
#load last address of SP and put TRUE or FALSE there
string += "\n@SP"+\
"\nA=M"+\
"\nM=D"
#increment SP
string += "\n@SP"+\
"\nM=M+1"
return string
def Or():
string = ""
#load last value of stack in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#load next to last value of stack in A register
string += "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nA=M"
#store result of D&A in D register
string += "\nD=D|A"
#load last address of SP and put TRUE or FALSE there
string += "\n@SP"+\
"\nA=M"+\
"\nM=D"
#increment SP
string += "\n@SP"+\
"\nM=M+1"
return string
def Not():
string = ""
#load last value of stack in D register
string = "\n@SP"+\
"\nM=M-1"+\
"\nA=M"+\
"\nD=M"
#NOT the result in D register
string += "\nD=!M"
#write NOT to memory
string += "\nM=D"
#increment SP
string += "\n@SP"+\
"\nM=M+1"
return string
def VM_command_to_HACK(instruction,file):
templst = instruction.split()
if templst[0] == 'push':
write_to_file(push(templst[1],templst[2]),file)
elif templst[0] == 'add':
write_to_file(add(),file)
elif templst[0] == 'sub':
write_to_file(sub(),file)
elif templst[0] == 'neg':
write_to_file(neg(),file)
elif templst[0] == 'eq':
write_to_file(eq(),file)
elif templst[0] == 'gt':
write_to_file(gt(),file)
elif templst[0] == 'lt':
write_to_file(lt(),file)
elif templst[0] == 'and':
write_to_file(And(),file)
elif templst[0] == 'or':
write_to_file(Or(),file)
elif templst[0] == 'not':
write_to_file(Not(),file)
else:
print("command not found")
def main():
fptr = open_file("vmout.asm")
setup(fptr) #setup address of stack
#parse file
close_file(fptr)
print("file written")