This commit is contained in:
2021-04-21 17:01:42 +03:00
commit 281a96945a
613 changed files with 202255 additions and 0 deletions

BIN
projects/06/06.zip Normal file

Binary file not shown.

6
projects/06/Add.hack Normal file
View File

@@ -0,0 +1,6 @@
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000

16
projects/06/Max.hack Normal file
View File

@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111

27483
projects/06/Pong.hack Normal file

File diff suppressed because it is too large Load Diff

25
projects/06/Rect.hack Normal file
View File

@@ -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

13
projects/06/add/Add.asm Normal file
View File

@@ -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

6
projects/06/add/Add.hack Normal file
View File

@@ -0,0 +1,6 @@
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000

202
projects/06/asm.py Normal file
View File

@@ -0,0 +1,202 @@
import sys
import os
import re
lut = {
"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,
"SCREEN": 16384,
"KBD": 24576,
"SP": 0,
"LCL": 1,
"ARG": 2,
"THIS": 3,
"THAT": 4,
}
currentAddr = 16
currentCmd = 0
def main():
global lut
global currentAddr
global currentCmd
global outfile
global infile
if len(sys.argv) < 2:
print("No file specified")
exit(-1)
elif not sys.argv[1].endswith(".asm"):
print("File is not assembly")
exit(-1)
infile = open(sys.argv[1], "r")
if os.path.isfile(sys.argv[1].replace(".asm", ".hack")):
outfile = open(sys.argv[1].replace(".asm", ".hack"), "w")
else:
outfile = open(sys.argv[1].replace(".asm", ".hack"), "x")
outfile.truncate(0)
labelre = re.compile("^\((.*?)\)")
# Parse all labels
for line in infile:
nocomment = line.split("//", 1)[0]
stripped = nocomment.strip()
if len(stripped) > 0:
match = labelre.match(stripped);
if match:
label = match.group(1)
lut[label] = currentCmd
else:
currentCmd += 1
infile.seek(0)
# Translate everything else
for line in infile:
nocomment = line.split("//", 1)[0]
stripped = nocomment.strip()
if len(stripped) > 0:
match = labelre.match(stripped)
if not match:
process_line(stripped)
infile.close()
outfile.close()
def getbin(num, n=0):
return format(num, 'b').zfill(n)
def getcomp(comp):
cmdluta = {
"0": "101010",
"1": "111111",
"-1": "111010",
"D": "001100",
"A": "110000",
"!D": "001101",
"!A": "110001",
"-D": "001111",
"-A": "110011",
"D+1": "011111",
"A+1": "110111",
"D-1": "001110",
"A-1": "110010",
"D+A": "000010",
"D-A": "010011",
"A-D": "000111",
"D&A": "000000",
"D|A": "010101",
}
cmd = ""
if comp.find("M") != -1:
cmd += "1"
else:
cmd += "0"
compa = comp.replace("M", "A")
cmd += cmdluta[compa]
return cmd
def getdest(dest):
cmd = ""
if dest.find("A") != -1:
cmd += "1"
else:
cmd += "0"
if dest.find("D") != -1:
cmd += "1"
else:
cmd += "0"
if dest.find("M") != -1:
cmd += "1"
else:
cmd += "0"
return cmd
def getjmp(jmp):
return {
"JGT": "001",
"JEQ": "010",
"JGE": "011",
"JLT": "100",
"JNE": "101",
"JLE": "110",
"JMP": "111",
}[jmp]
def process_line(line):
global lut
global currentAddr
global currentCmd
global outfile
global infile
if line[0] == "@":
addr = line[1:]
if not addr.isdigit():
if not addr in lut:
lut[addr] = currentAddr
currentAddr += 1
outfile.write("0" + getbin(lut[addr], 15) +"\n")
else:
outfile.write("0" + getbin(int(addr), 15) + "\n")
else:
if "=" in line:
dest=line.split("=",1)[0]
comp = line.split("=", 1)[1]
else:
dest = False
if ";" in line:
split = line.split(";")
jmp = split[len(split) - 1]
comp = line.split(";", 1)[0]
else:
jmp = False
if not comp:
comp = line
cmd = "111"
cmd += getcomp(comp)
if dest:
cmd += getdest(dest)
else:
cmd += "000"
if jmp:
cmd += getjmp(jmp)
else:
cmd += "000"
outfile.write(cmd + "\n")
if __name__ == '__main__':
main()

202
projects/06/asm.py.v1 Normal file
View File

@@ -0,0 +1,202 @@
import sys
import os
import re
lut = {
"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,
"SCREEN": 16384,
"KBD": 24576,
"SP": 0,
"LCL": 1,
"ARG": 2,
"THIS": 3,
"THAT": 4,
}
currentAddr = 16
currentCmd = 0
def main():
global lut
global currentAddr
global currentCmd
global outfile
global infile
if len(sys.argv) < 2:
print("No file specified")
exit(-1)
elif not sys.argv[1].endswith(".asm"):
print("File is not assembly")
exit(-1)
infile = open(sys.argv[1], "r")
if os.path.isfile(sys.argv[1].replace(".asm", ".hack")):
outfile = open(sys.argv[1].replace(".asm", ".hack"), "w")
else:
outfile = open(sys.argv[1].replace(".asm", ".hack"), "x")
outfile.truncate(0)
labelre = re.compile("^\((.*?)\)")
# Parse all labels
for line in infile:
nocomment = line.split("//", 1)[0]
stripped = nocomment.strip()
if len(stripped) > 0:
match = labelre.match(stripped);
if match:
label = match.group(1)
lut[label] = currentCmd
else:
currentCmd += 1
infile.seek(0)
# Translate everything else
for line in infile:
nocomment = line.split("//", 1)[0]
stripped = nocomment.strip()
if len(stripped) > 0:
match = labelre.match(stripped)
if not match:
process_line(stripped)
infile.close()
outfile.close()
def getbin(num, n=0):
return format(num, 'b').zfill(n)
def getcomp(comp):
cmdluta = {
"0": "101010",
"1": "111111",
"-1": "111010",
"D": "001100",
"A": "110000",
"!D": "001101",
"!A": "110001",
"-D": "001111",
"-A": "110011",
"D+1": "011111",
"A+1": "110111",
"D-1": "001110",
"A-1": "110010",
"D+A": "000010",
"D-A": "010011",
"A-D": "000111",
"D&A": "000000",
"D|A": "010101",
}
cmd = ""
if comp.find("M") != -1:
cmd += "1"
else:
cmd += "0"
compa = comp.replace("M", "A")
cmd += cmdluta[compa]
return cmd
def getdest(dest):
cmd = ""
if dest.find("A") != -1:
cmd += "1"
else:
cmd += "0"
if dest.find("D") != -1:
cmd += "1"
else:
cmd += "0"
if dest.find("M") != -1:
cmd += "1"
else:
cmd += "0"
return cmd
def getjmp(jmp):
return {
"JGT": "001",
"JEQ": "010",
"JGE": "011",
"JLT": "100",
"JNE": "101",
"JLE": "110",
"JMP": "111",
}[jmp]
def process_line(line):
global lut
global currentAddr
global currentCmd
global outfile
global infile
if line[0] == "@":
addr = line[1:]
if not addr.isdigit():
if not addr in lut:
lut[addr] = currentAddr
currentAddr += 1
outfile.write("0" + getbin(lut[addr], 15) +"\n")
else:
outfile.write("0" + getbin(int(addr), 15) + "\n")
else:
if "=" in line:
dest=line.split("=",1)[0]
comp = line.split("=", 1)[1]
else:
dest = False
if ";" in line:
split = line.split(";")
jmp = split[len(split) - 1]
comp = line.split(";", 1)[0]
else:
jmp = False
if not comp:
comp = line
cmd = "111"
cmd += getcomp(comp)
if dest:
cmd += getdest(dest)
else:
cmd += "000"
if jmp:
cmd += getjmp(jmp)
else:
cmd += "000"
outfile.write(cmd + "\n")
if __name__ == '__main__':
main()

26
projects/06/max/Max.asm Normal file
View File

@@ -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

16
projects/06/max/Max.hack Normal file
View File

@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111

16
projects/06/max/MaxL.asm Normal file
View File

@@ -0,0 +1,16 @@
@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

16
projects/06/max/MaxL.hack Normal file
View File

@@ -0,0 +1,16 @@
0000000000000000
1110000000010000
0000000000000001
1110000000010000
0000000000001010
1110000000000001
0000000000000001
1110000000010000
0000000000001100
1110000000000111
0000000000000000
1110000000010000
0000000000000010
1110000000001000
0000000000001110
1110000000000111

26
projects/06/max/asdf.asm Normal file
View File

@@ -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

16
projects/06/max/asdf.hack Normal file
View File

@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111

28375
projects/06/pong/Pong.asm Normal file

File diff suppressed because it is too large Load Diff

27483
projects/06/pong/Pong.hack Normal file

File diff suppressed because it is too large Load Diff

27490
projects/06/pong/PongL.asm Normal file

File diff suppressed because it is too large Load Diff

1
projects/06/prog.txt Normal file
View File

@@ -0,0 +1 @@

35
projects/06/rect/Rect.asm Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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