.
This commit is contained in:
Executable
+13
@@ -0,0 +1,13 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/06/add/Add.asm
|
||||
|
||||
// Computes R0 = 2 + 3 (R0 refers to RAM[0])
|
||||
|
||||
@2
|
||||
D = A
|
||||
@3
|
||||
D=D+A
|
||||
@0
|
||||
M=D
|
||||
Executable
+230
@@ -0,0 +1,230 @@
|
||||
def a_instruct(string): # covert the A instruction to bits
|
||||
string = string.strip('@\n')
|
||||
x = "{0:b}".format(int(string)) # convert the number to binary, problem if the number is too big
|
||||
while len(x) < 16: # need to be 16 bits and 1 for \n
|
||||
x = "0" + x
|
||||
return x # return th 16 bit value
|
||||
|
||||
|
||||
def c_instruct(string): # priema edna liniq koeto e edna instrukciq i q prevryshta v bit kod
|
||||
string = string + "\n"
|
||||
flag1, flag2, p = 0, 0, 0
|
||||
dest_s = ''
|
||||
comp_s = ''
|
||||
jump_s = ''
|
||||
for c in range(len(string)):
|
||||
if string[c] == '=': # ako ima ravno znachi predi nego ima destination
|
||||
i = 0
|
||||
flag1 = 1 # diga flag za destination
|
||||
while i < c:
|
||||
p = c
|
||||
dest_s += string[i]
|
||||
i += 1
|
||||
dest_s = destDic.get(dest_s) # convert t to Destination
|
||||
if string[c] == ';': # ako ima ; znachi predi nego ima computation
|
||||
flag2 = 1 # diga flag za Jump
|
||||
if flag1 == 0: # ako flaga za destination ne e dignat zapochvame ot nachaloto na liniqta
|
||||
p = 0
|
||||
else: # ako e zapochvame ot kydeto e ravnoto koeto se pazi vyv var i
|
||||
p = i
|
||||
while p < c:
|
||||
comp_s += string[p]
|
||||
p += 1
|
||||
comp_s = compDic.get(comp_s) # convert t1 to Computation
|
||||
|
||||
if string[c] == '\n':
|
||||
if flag2 == 1: # ako ima flag za jump to vzimame stoinosta na jumpa
|
||||
j = p + 1
|
||||
while j < c:
|
||||
jump_s += string[j]
|
||||
j += 1
|
||||
jump_s = jumpDic.get(jump_s)
|
||||
else: # ako nqma togava vzima stoinosta na computationa
|
||||
j = i + 1
|
||||
while j < c:
|
||||
comp_s += string[j]
|
||||
j += 1
|
||||
comp_s = compDic.get(comp_s)
|
||||
final_result = '111' + comp_s # dobavqme 111 koeto e standart za C instrukt i computation bez koeto nqma instrukciq
|
||||
if dest_s != '': # ako e imalo dest go dobavqme
|
||||
final_result = final_result + dest_s
|
||||
else: # inache dobavqme 3 nuli
|
||||
final_result = final_result + '000'
|
||||
if jump_s != '': # ako e imalo jump go dobavqme
|
||||
final_result = final_result + jump_s
|
||||
else: # inache dobavqme 3 nuli
|
||||
final_result = final_result + '000'
|
||||
return final_result
|
||||
|
||||
|
||||
def check_a_or_c(temp_string):
|
||||
line = ''
|
||||
temp_string1 = ''
|
||||
for j in temp_string: # zapisva liniqta v string koito posle se obrabotva
|
||||
if j != '\n':
|
||||
line += j
|
||||
else:
|
||||
for index in range(len(line)): # proverqva vseki simvol ot liniqta
|
||||
if line[index] == "@": # ako liniqta zapochva s @ znachi e a instrukciq
|
||||
temp_string1 += a_instruct(line) + '\n'
|
||||
line = ''
|
||||
break
|
||||
else: # ako ne zapochva s @ znachi e C instrukciq
|
||||
temp_string1 += c_instruct(line) + '\n'
|
||||
line = ''
|
||||
break
|
||||
return temp_string1
|
||||
|
||||
|
||||
def clear_file(temp_string_cf): # clears the empty lines, comments and spaces and writes them in a string
|
||||
for line in asmfile:
|
||||
f = 0
|
||||
if line == '\n': # removes empty lines
|
||||
continue
|
||||
for index in range(len(line)):
|
||||
if line[index] == "/" and line[index + 1] == '/': # checks if it is comment
|
||||
if f == 1: # maybe a problem if the last element of a line is / since it will stay
|
||||
temp_string_cf += '\n'
|
||||
break
|
||||
if line[index] != ' ': # removes all of the whitespaces except newlines
|
||||
temp_string_cf += line[index]
|
||||
f = 1
|
||||
return temp_string_cf
|
||||
|
||||
|
||||
def label(temp_string): # removes the labels and adds them to the dictionary writes all in a string
|
||||
line = ''
|
||||
temp_string_l = ''
|
||||
global rowasm
|
||||
for j in temp_string: # zapisva liniqta v string koito posle se obrabotva
|
||||
if j != '\n':
|
||||
line += j
|
||||
else:
|
||||
f = 0
|
||||
for index in range(len(line)): # loops through the lines char by char
|
||||
if line[index] == "(":
|
||||
# checks if it is a label, will be a problem if there is only "(" and no closing bracket alse need to check label syntax
|
||||
if bool(labelDic.get(line.strip('()'))) == False: # check if label is already in the Dictionary
|
||||
labelDic[line.strip("()")] = str(rowasm) # adds the label to the dictionary
|
||||
rowasm -= 1 # need to pay attention to the counting cause im not sure
|
||||
f = 1
|
||||
break # brake so i don't write this line in the string since it needs to be removed
|
||||
if f != 1:
|
||||
temp_string_l += line
|
||||
temp_string_l += '\n'
|
||||
rowasm += 1
|
||||
line = ''
|
||||
return temp_string_l
|
||||
|
||||
|
||||
def variable(temp_string): # replaces the variables with the correct address
|
||||
global variableCount
|
||||
line = ''
|
||||
temp_string_v = ''
|
||||
for j in temp_string: # zapisva liniqta v string koito posle se obrabotva
|
||||
if j != '\n':
|
||||
line += j
|
||||
else:
|
||||
for index in range(len(line)):
|
||||
if line[index] == '@' and numbers.find(line[index + 1]) < 0: # checks if the line holds a variable
|
||||
if bool(labelDic.get(line.strip('@'))): # check if variable is already in the Dictionary
|
||||
line = '@' + labelDic[line.strip('@')] # replaces the variable with address i hope
|
||||
else:
|
||||
labelDic[line.strip('@')] = str(variableCount) # if isn't it adds it
|
||||
line = '@' + str(variableCount) # replaces the variable with address i hope
|
||||
variableCount += 1 # increases the variable counter so i don't write them all in address 16
|
||||
break
|
||||
temp_string_v += line + '\n' # appends the line to the string
|
||||
line = ''
|
||||
return temp_string_v
|
||||
|
||||
|
||||
destDic = {'None': '000',
|
||||
'M': '001',
|
||||
'D': '010',
|
||||
'MD': '011',
|
||||
'A': '100',
|
||||
'AM': '101',
|
||||
'AD': '110',
|
||||
'AMD': '111', } # contains all destinations and their bit equivalent
|
||||
jumpDic = {'None': '000',
|
||||
'JGT': '001',
|
||||
'JQE': '010',
|
||||
'JGE': '011',
|
||||
'JLT': '100',
|
||||
'JNE': '101',
|
||||
'JLE': '110',
|
||||
'JMP': '111', } # contains all jumps and their bit equivalent
|
||||
compDic = {'0': '0101010',
|
||||
'1': '0111111',
|
||||
'-1': '0111010',
|
||||
'D': '0001100',
|
||||
'A': '0110000',
|
||||
'!D': '0001101',
|
||||
'!A': '0110001',
|
||||
'-D': '0001111',
|
||||
'-A': '0110011',
|
||||
'D+1': '0011111',
|
||||
'A+1': '0110111',
|
||||
'D-1': '0001110',
|
||||
'A-1': '0110010',
|
||||
'D+A': '0000010',
|
||||
'D-A': '0010011',
|
||||
'A-D': '0000111',
|
||||
'D&A': '0000000',
|
||||
'D|A': '0010101',
|
||||
'M': '1110000',
|
||||
'!M': '1110001',
|
||||
'-M': '1110011',
|
||||
'M+1': '1110111',
|
||||
'M-1': '1110010',
|
||||
'D+M': '1000010',
|
||||
'D-M': '1010011',
|
||||
'M-D': '1000111',
|
||||
'D&M': '1000000',
|
||||
'D|M': '1010101',
|
||||
} # contains all computations and their bit equivalent
|
||||
tempString = '' # create empty string
|
||||
rowasm = 0
|
||||
numbers = '0123456789' # used to check if given instruction calls to variable
|
||||
variableCount = 16 # keeps count of the variable address
|
||||
|
||||
labelDic = {'SP': '0',
|
||||
'LCL': '1',
|
||||
'ARG': '2',
|
||||
'THIS': '3',
|
||||
'THAT': '4',
|
||||
'SCREEN': '16384',
|
||||
'KBD': '24576',
|
||||
'R0': '0',
|
||||
'R1': '1',
|
||||
'R2': '2',
|
||||
'R3': '3',
|
||||
'R4': '4',
|
||||
'R5': '5',
|
||||
'R6': '6',
|
||||
'R7': '7',
|
||||
'R8': '8',
|
||||
'R9': '9',
|
||||
'R10': '10',
|
||||
'R11': '11',
|
||||
'R12': '12',
|
||||
'R13': '13',
|
||||
'R14': '14',
|
||||
'R15': '15',
|
||||
} # dictionary containing Labels and Variables in format: AddressNumber : LabelName/ VariableName
|
||||
with open("C:/Users/AGrudev/Desktop/n2t/nand2tetris/projects/06/pong/Pong.asm", "r+") as asmfile:
|
||||
tempString = clear_file(tempString)
|
||||
print(tempString + '----------')
|
||||
|
||||
tempString = label(tempString)
|
||||
print(tempString + '----------')
|
||||
|
||||
tempString = variable(tempString)
|
||||
print(tempString + '----------')
|
||||
|
||||
tempString = check_a_or_c(tempString)
|
||||
print(tempString + '----------')
|
||||
|
||||
with open("C:/Users/AGrudev/Desktop/n2t/nand2tetris/projects/06/pong/Pong.hack", "w+") as destfile:
|
||||
destfile.write(tempString)
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/06/max/Max.asm
|
||||
|
||||
// Computes R2 = max(R0, R1) (R0,R1,R2 refer to RAM[0],RAM[1],RAM[2])
|
||||
|
||||
@R0
|
||||
D=M // D = first number
|
||||
@R1
|
||||
D=D-M // D = first number - second number
|
||||
@OUTPUT_FIRST
|
||||
D;JGT // if D>0 (first is greater) goto output_first
|
||||
@R1
|
||||
D=M // D = second number
|
||||
@OUTPUT_D
|
||||
0;JMP // goto output_d
|
||||
(OUTPUT_FIRST)
|
||||
@R0
|
||||
D=M // D = first number
|
||||
(OUTPUT_D)
|
||||
@R2
|
||||
M=D // M[2] = D (greatest number)
|
||||
(INFINITE_LOOP)
|
||||
@INFINITE_LOOP
|
||||
0;JMP // infinite loop
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
0000000000000000
|
||||
1111110000010000
|
||||
0000000000000001
|
||||
1111010011010000
|
||||
0000000000001010
|
||||
1110001100000001
|
||||
0000000000000001
|
||||
1111110000010000
|
||||
0000000000001100
|
||||
1110101010000111
|
||||
0000000000000000
|
||||
1111110000010000
|
||||
0000000000000010
|
||||
1110001100001000
|
||||
0000000000001110
|
||||
1110101010000111
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/06/max/MaxL.asm
|
||||
|
||||
// Symbol-less version of the Max.asm program.
|
||||
|
||||
@0
|
||||
D=M
|
||||
@1
|
||||
D=D-M
|
||||
@10
|
||||
D;JGT
|
||||
@1
|
||||
D=M
|
||||
@12
|
||||
0;JMP
|
||||
@0
|
||||
D=M
|
||||
@2
|
||||
M=D
|
||||
@14
|
||||
0;JMP
|
||||
Executable
+28375
File diff suppressed because it is too large
Load Diff
Executable
+27483
File diff suppressed because it is too large
Load Diff
Executable
+27490
File diff suppressed because it is too large
Load Diff
Executable
+35
@@ -0,0 +1,35 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/06/rect/Rect.asm
|
||||
|
||||
// Draws a rectangle at the top-left corner of the screen.
|
||||
// The rectangle is 16 pixels wide and R0 pixels high.
|
||||
|
||||
@0
|
||||
D=M
|
||||
@INFINITE_LOOP
|
||||
D;JLE
|
||||
@counter
|
||||
M=D
|
||||
@SCREEN
|
||||
D=A
|
||||
@address
|
||||
M=D
|
||||
(LOOP)
|
||||
@address
|
||||
A=M
|
||||
M=-1
|
||||
@address
|
||||
D=M
|
||||
@32
|
||||
D=D+A
|
||||
@address
|
||||
M=D
|
||||
@counter
|
||||
MD=M-1
|
||||
@LOOP
|
||||
D;JGT
|
||||
(INFINITE_LOOP)
|
||||
@INFINITE_LOOP
|
||||
0;JMP
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
0000000000000000
|
||||
1111110000010000
|
||||
0000000000010111
|
||||
1110001100000110
|
||||
0000000000010000
|
||||
1110001100001000
|
||||
0100000000000000
|
||||
1110110000010000
|
||||
0000000000010001
|
||||
1110001100001000
|
||||
0000000000010001
|
||||
1111110000100000
|
||||
1110111010001000
|
||||
0000000000010001
|
||||
1111110000010000
|
||||
0000000000100000
|
||||
1110000010010000
|
||||
0000000000010001
|
||||
1110001100001000
|
||||
0000000000010000
|
||||
1111110010011000
|
||||
0000000000001010
|
||||
1110001100000001
|
||||
0000000000010111
|
||||
1110101010000111
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
// This file is part of www.nand2tetris.org
|
||||
// and the book "The Elements of Computing Systems"
|
||||
// by Nisan and Schocken, MIT Press.
|
||||
// File name: projects/06/rect/RectL.asm
|
||||
|
||||
// Symbol-less version of the Rect.asm program.
|
||||
|
||||
@0
|
||||
D=M
|
||||
@23
|
||||
D;JLE
|
||||
@16
|
||||
M=D
|
||||
@16384
|
||||
D=A
|
||||
@17
|
||||
M=D
|
||||
@17
|
||||
A=M
|
||||
M=-1
|
||||
@17
|
||||
D=M
|
||||
@32
|
||||
D=D+A
|
||||
@17
|
||||
M=D
|
||||
@16
|
||||
MD=M-1
|
||||
@10
|
||||
D;JGT
|
||||
@23
|
||||
0;JMP
|
||||
Reference in New Issue
Block a user