This commit is contained in:
QkoSad
2025-07-16 13:00:37 +03:00
commit 7894b48931
806 changed files with 162532 additions and 0 deletions
+232
View File
@@ -0,0 +1,232 @@
@10
D=A
@SP
A=M
M=D
@SP
M=M+1
@0
D=A
@LCL
M=M+D
@SP
M=M-1
A=M
D=M
@LCL
A=M
M=D
@0
D=A
@LCL
M=M-D
@21
D=A
@SP
A=M
M=D
@SP
M=M+1
@22
D=A
@SP
A=M
M=D
@SP
M=M+1
@2
D=A
@ARG
M=M+D
@SP
M=M-1
A=M
D=M
@ARG
A=M
M=D
@2
D=A
@ARG
M=M-D
@1
D=A
@ARG
M=M+D
@SP
M=M-1
A=M
D=M
@ARG
A=M
M=D
@1
D=A
@ARG
M=M-D
@36
D=A
@SP
A=M
M=D
@SP
M=M+1
@6
D=A
@THIS
M=M+D
@SP
M=M-1
A=M
D=M
@THIS
A=M
M=D
@6
D=A
@THIS
M=M-D
@42
D=A
@SP
A=M
M=D
@SP
M=M+1
@45
D=A
@SP
A=M
M=D
@SP
M=M+1
@5
D=A
@THAT
M=M+D
@SP
M=M-1
A=M
D=M
@THAT
A=M
M=D
@5
D=A
@THAT
M=M-D
@2
D=A
@THAT
M=M+D
@SP
M=M-1
A=M
D=M
@THAT
A=M
M=D
@2
D=A
@THAT
M=M-D
@510
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
@11
M=D
@0
D=A
@LCL
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@5
D=A
@THAT
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
@1
D=A
@ARG
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M-D
@6
D=A
@THIS
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@6
D=A
@THIS
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
@SP
M=M-1
A=M
D=M
A=A-1
M=M-D
@11
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
+2
View File
@@ -0,0 +1,2 @@
|RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] |
| 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 |
+2
View File
@@ -0,0 +1,2 @@
|RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] |
| 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 |
+25
View File
@@ -0,0 +1,25 @@
// 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/07/MemoryAccess/BasicTest/BasicTest.tst
load BasicTest.asm,
output-file BasicTest.out,
compare-to BasicTest.cmp,
output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1
RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1
RAM[3015]%D1.6.1 RAM[11]%D1.6.1;
set RAM[0] 256, // stack pointer
set RAM[1] 300, // base address of the local segment
set RAM[2] 400, // base address of the argument segment
set RAM[3] 3000, // base address of the this segment
set RAM[4] 3010, // base address of the that segment
repeat 600 { // enough cycles to complete the execution
ticktock;
}
// Outputs the stack base and some values
// from the tested memory segments
output;
+31
View File
@@ -0,0 +1,31 @@
// 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/07/MemoryAccess/BasicTest/BasicTest.vm
// Executes pop and push commands using the virtual memory segments.
push constant 10
pop local 0
push constant 21
push constant 22
pop argument 2
pop argument 1
push constant 36
pop this 6
push constant 42
push constant 45
pop that 5
pop that 2
push constant 510
pop temp 6
push local 0
push that 5
add
push argument 1
sub
push this 6
push this 6
add
sub
push temp 6
add
+25
View File
@@ -0,0 +1,25 @@
// 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/07/MemoryAccess/BasicTest/BasicTestVME.tst
load BasicTest.vm,
output-file BasicTest.out,
compare-to BasicTest.cmp,
output-list RAM[256]%D1.6.1 RAM[300]%D1.6.1 RAM[401]%D1.6.1
RAM[402]%D1.6.1 RAM[3006]%D1.6.1 RAM[3012]%D1.6.1
RAM[3015]%D1.6.1 RAM[11]%D1.6.1;
set sp 256, // stack pointer
set local 300, // base address of the local segment
set argument 400, // base address of the argument segment
set this 3000, // base address of the this segment
set that 3010, // base address of the that segment
repeat 25 { // BasicTest.vm has 25 instructions
vmstep;
}
// Outputs the stack base and some values
// from the tested memory segments
output;
+122
View File
@@ -0,0 +1,122 @@
@3030
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
@3
M=D
@3040
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
@4
M=D
@32
D=A
@SP
A=M
M=D
@SP
M=M+1
@2
D=A
@THIS
M=M+D
@SP
M=M-1
A=M
D=M
@THIS
A=M
M=D
@2
D=A
@THIS
M=M-D
@46
D=A
@SP
A=M
M=D
@SP
M=M+1
@6
D=A
@THAT
M=M+D
@SP
M=M-1
A=M
D=M
@THAT
A=M
M=D
@6
D=A
@THAT
M=M-D
@3
D=M
@SP
A=M
M=D
@SP
M=M+1
@4
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
@2
D=A
@THIS
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M-D
@6
D=A
@THAT
A=M+D
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
+2
View File
@@ -0,0 +1,2 @@
|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046|
| 6084 | 3030 | 3040 | 32 | 46 |
+20
View File
@@ -0,0 +1,20 @@
// 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/07/MemoryAccess/PointerTest/PointerTest.tst
load PointerTest.asm,
output-file PointerTest.out,
compare-to PointerTest.cmp,
output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1
RAM[4]%D1.6.1 RAM[3032]%D1.6.1 RAM[3046]%D1.6.1;
set RAM[0] 256, // initializes the stack pointer
repeat 450 { // enough cycles to complete the execution
ticktock;
}
// outputs the stack base, this, that, and
// some values from the the this and that segments
output;
+22
View File
@@ -0,0 +1,22 @@
// 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/07/MemoryAccess/PointerTest/PointerTest.vm
// Executes pop and push commands using the
// pointer, this, and that segments.
push constant 3030
pop pointer 0
push constant 3040
pop pointer 1
push constant 32
pop this 2
push constant 46
pop that 6
push pointer 0
push pointer 1
add
push this 2
sub
push that 6
add
+20
View File
@@ -0,0 +1,20 @@
// 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/07/MemoryAccess/PointerTest/PointerTestVME.tst
load PointerTest.vm,
output-file PointerTest.out,
compare-to PointerTest.cmp,
output-list RAM[256]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1
RAM[3032]%D1.6.1 RAM[3046]%D1.6.1;
set RAM[0] 256, // initializes the stack pointer
repeat 15 { // PointerTest.vm has 15 instructions
vmstep;
}
// outputs the stack base, this, that, and
// some values from the the this and that segments
output;
+72
View File
@@ -0,0 +1,72 @@
@111
D=A
@SP
A=M
M=D
@SP
M=M+1
@333
D=A
@SP
A=M
M=D
@SP
M=M+1
@888
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
@StaticTest.vm.8
M=D
@SP
M=M-1
A=M
D=M
@StaticTest.vm.3
M=D
@SP
M=M-1
A=M
D=M
@StaticTest.vm.1
M=D
@StaticTest.vm.3
D=M
@SP
A=M
M=D
@SP
M=M+1
@StaticTest.vm.1
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M-D
@StaticTest.vm.8
D=M
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
+2
View File
@@ -0,0 +1,2 @@
|RAM[256]|
| 1110 |
+17
View File
@@ -0,0 +1,17 @@
// 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/07/MemoryAccess/StaticTest/StaticTest.tst
load StaticTest.asm,
output-file StaticTest.out,
compare-to StaticTest.cmp,
output-list RAM[256]%D1.6.1;
set RAM[0] 256, // initializes the stack pointer
repeat 200 { // enough cycles to complete the execution
ticktock;
}
output; // the stack base
+17
View File
@@ -0,0 +1,17 @@
// 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/07/MemoryAccess/StaticTest/StaticTest.vm
// Executes pop and push commands using the static segment.
push constant 111
push constant 333
push constant 888
pop static 8
pop static 3
pop static 1
push static 3
push static 1
sub
push static 8
add
+17
View File
@@ -0,0 +1,17 @@
// 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/07/MemoryAccess/StaticTest/StaticTestVME.tst
load StaticTest.vm,
output-file StaticTest.out,
compare-to StaticTest.cmp,
output-list RAM[256]%D1.6.1;
set sp 256, // initializes the stack pointer
repeat 11 { // StaticTest.vm has 11 instructions
vmstep;
}
output; // the stack base
+20
View File
@@ -0,0 +1,20 @@
@7
D=A
@SP
A=M
M=D
@SP
M=M+1
@8
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
+2
View File
@@ -0,0 +1,2 @@
| RAM[0] | RAM[256] |
| 257 | 15 |
+17
View File
@@ -0,0 +1,17 @@
// 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/07/StackArithmetic/SimpleAdd/SimpleAdd.tst
load SimpleAdd.asm,
output-file SimpleAdd.out,
compare-to SimpleAdd.cmp,
output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2;
set RAM[0] 256, // initializes the stack pointer
repeat 60 { // enough cycles to complete the execution
ticktock;
}
output; // the stack pointer and the stack base
+9
View File
@@ -0,0 +1,9 @@
// 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/07/StackArithmetic/SimpleAdd/SimpleAdd.vm
// Pushes and adds two constants.
push constant 7
push constant 8
add
+17
View File
@@ -0,0 +1,17 @@
// 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/07/StackArithmetic/SimpleAdd/SimpleAddVME.tst
load SimpleAdd.vm,
output-file SimpleAdd.out,
compare-to SimpleAdd.cmp,
output-list RAM[0]%D2.6.2 RAM[256]%D2.6.2;
set RAM[0] 256, // initializes the stack pointer
repeat 3 { // SimpleAdd.vm has 3 instructions
vmstep;
}
output; // the stack pointer and the stack base
+373
View File
@@ -0,0 +1,373 @@
@17
D=A
@SP
A=M
M=D
@SP
M=M+1
@17
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE0
D;JEQ
@SP
A=M
A=A-1
M=0
@FALSE0
0;JMP
(TRUE0)
@SP
A=M
A=A-1
M=-1
(FALSE0)
@17
D=A
@SP
A=M
M=D
@SP
M=M+1
@16
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE1
D;JEQ
@SP
A=M
A=A-1
M=0
@FALSE1
0;JMP
(TRUE1)
@SP
A=M
A=A-1
M=-1
(FALSE1)
@16
D=A
@SP
A=M
M=D
@SP
M=M+1
@17
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE2
D;JEQ
@SP
A=M
A=A-1
M=0
@FALSE2
0;JMP
(TRUE2)
@SP
A=M
A=A-1
M=-1
(FALSE2)
@892
D=A
@SP
A=M
M=D
@SP
M=M+1
@891
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE3
D;JLT
@SP
A=M
A=A-1
M=0
@FALSE3
0;JMP
(TRUE3)
@SP
A=M
A=A-1
M=-1
(FALSE3)
@891
D=A
@SP
A=M
M=D
@SP
M=M+1
@892
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE4
D;JLT
@SP
A=M
A=A-1
M=0
@FALSE4
0;JMP
(TRUE4)
@SP
A=M
A=A-1
M=-1
(FALSE4)
@891
D=A
@SP
A=M
M=D
@SP
M=M+1
@891
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE5
D;JLT
@SP
A=M
A=A-1
M=0
@FALSE5
0;JMP
(TRUE5)
@SP
A=M
A=A-1
M=-1
(FALSE5)
@32767
D=A
@SP
A=M
M=D
@SP
M=M+1
@32766
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE6
D;JGT
@SP
A=M
A=A-1
M=0
@FALSE6
0;JMP
(TRUE6)
@SP
A=M
A=A-1
M=-1
(FALSE6)
@32766
D=A
@SP
A=M
M=D
@SP
M=M+1
@32767
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE7
D;JGT
@SP
A=M
A=A-1
M=0
@FALSE7
0;JMP
(TRUE7)
@SP
A=M
A=A-1
M=-1
(FALSE7)
@32766
D=A
@SP
A=M
M=D
@SP
M=M+1
@32766
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
D=M-D
@TRUE8
D;JGT
@SP
A=M
A=A-1
M=0
@FALSE8
0;JMP
(TRUE8)
@SP
A=M
A=A-1
M=-1
(FALSE8)
@57
D=A
@SP
A=M
M=D
@SP
M=M+1
@31
D=A
@SP
A=M
M=D
@SP
M=M+1
@53
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M+D
@112
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M-D
@SP
A=M
A=A-1
M=-M
@SP
M=M-1
A=M
D=M
A=A-1
M=M&D
@82
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
A=A-1
M=M|D
@SP
A=M
A=A-1
M=!M
+4
View File
@@ -0,0 +1,4 @@
| RAM[0] | RAM[256] | RAM[257] | RAM[258] | RAM[259] | RAM[260] |
| 266 | -1 | 0 | 0 | 0 | -1 |
| RAM[261] | RAM[262] | RAM[263] | RAM[264] | RAM[265] |
| 0 | -1 | 0 | 0 | -91 |
+22
View File
@@ -0,0 +1,22 @@
// 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/07/StackArithmetic/StackTest/StackTest.tst
load StackTest.asm,
output-file StackTest.out,
compare-to StackTest.cmp,
output-list RAM[0]%D2.6.2
RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2;
set RAM[0] 256, // initializes the stack pointer
repeat 1000 { // enough cycles to complete the execution
ticktock;
}
// outputs the stack pointer (RAM[0]) and
// the stack contents: RAM[256]-RAM[265]
output;
output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2;
output;
+45
View File
@@ -0,0 +1,45 @@
// 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/07/StackArithmetic/StackTest/StackTest.vm
// Executes a sequence of arithmetic and logical operations
// on the stack.
push constant 17
push constant 17
eq
push constant 17
push constant 16
eq
push constant 16
push constant 17
eq
push constant 892
push constant 891
lt
push constant 891
push constant 892
lt
push constant 891
push constant 891
lt
push constant 32767
push constant 32766
gt
push constant 32766
push constant 32767
gt
push constant 32766
push constant 32766
gt
push constant 57
push constant 31
push constant 53
add
push constant 112
sub
neg
and
push constant 82
or
not
+22
View File
@@ -0,0 +1,22 @@
// 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/07/StackArithmetic/StackTest/StackTestVME.tst
load StackTest.vm,
output-file StackTest.out,
compare-to StackTest.cmp,
output-list RAM[0]%D2.6.2
RAM[256]%D2.6.2 RAM[257]%D2.6.2 RAM[258]%D2.6.2 RAM[259]%D2.6.2 RAM[260]%D2.6.2;
set RAM[0] 256, // initializes the stack pointer
repeat 38 { // StackTest.vm consists of 38 instructions
vmstep;
}
// outputs the stack pointer (RAM[0]) and
// the stack contents: RAM[256]-RAM[265]
output;
output-list RAM[261]%D2.6.2 RAM[262]%D2.6.2 RAM[263]%D2.6.2 RAM[264]%D2.6.2 RAM[265]%D2.6.2;
output;
+160
View File
@@ -0,0 +1,160 @@
import os
import sys
from pathlib import Path
trueCounter = 0
falseCounter = 0
staticCounter = 16
def clear_file(temp_string_cf, asmfile): # 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 push(line):
global tempString1
if line[4:8] == 'this':
number = int(line[8:])
line = '@' + str(number) + '\nD=A\n@THIS\nA=M+D\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:8] == 'that':
number = int(line[8:])
line = '@' + str(number) + '\nD=A\n@THAT\nA=M+D\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:8] == 'temp':
number = int(line[8:])
number += 5
line = '@' + str(number) + '\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:9] == 'local':
number = int(line[9:])
line = '@' + str(number) + '\nD=A\n@LCL\nA=M+D\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:10] == 'static':
number = int(line[10:])
line = '@' + name1 + '.' + str(number) + '\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:11] == 'pointer':
number = int(line[11:])
number += 3
line = '@' + str(number) + '\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:12] == 'argument':
number = int(line[12:])
line = '@' + str(number) + '\nD=A\n@ARG\nA=M+D\nD=M\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
elif line[4:12] == 'constant':
number = line[12:]
line = '@' + str(number) + '\nD=A\n@SP\nA=M\nM=D\n@SP\nM=M+1\n'
tempString1 += line
def pop(line):
global SP
global tempString1
if line[3:7] == 'this':
number = int(line[7:])
line = '@' + str(number) + '\nD=A\n@THIS\nM=M+D\n@SP\nM=M-1\nA=M\nD=M\n@THIS\nA=M\nM=D\n' + '@' + str(
number) + '\nD=A\n@THIS\nM=M-D\n'
elif line[3:7] == 'that':
number = int(line[7:])
line = '@' + str(number) + '\nD=A\n@THAT\nM=M+D\n@SP\nM=M-1\nA=M\nD=M\n@THAT\nA=M\nM=D\n' + '@' + str(
number) + '\nD=A\n@THAT\nM=M-D\n'
elif line[3:7] == 'temp':
number = int(line[7:])
number = number + 5
line = '@SP\nM=M-1\nA=M\nD=M\n@' + str(number) + '\nM=D\n'
elif line[3:8] == 'local':
number = int(line[8:])
line = '@' + str(number) + '\nD=A\n@LCL\nM=M+D\n@SP\nM=M-1\nA=M\nD=M\n@LCL\nA=M\nM=D\n' + '@' + str(
number) + '\nD=A\n@LCL\nM=M-D\n'
elif line[3:9] == 'static':
number = int(line[9:])
line = '@SP\nM=M-1\nA=M\nD=M\n@' + name1 + '.' + str(number) + '\nM=D\n'
elif line[3:10] == 'pointer':
number = int(line[10:])
number = number + 3
line = '@SP\nM=M-1\nA=M\nD=M\n@' + str(number) + '\nM=D\n'
elif line[3:11] == 'argument':
number = int(line[11:])
line = '@' + str(number) + '\nD=A\n@ARG\nM=M+D\n@SP\nM=M-1\nA=M\nD=M\n@ARG\nA=M\nM=D\n' + '@' + str(
number) + '\nD=A\n@ARG\nM=M-D\n'
tempString1 += line
def aritmetic(line):
global trueCounter
global falseCounter
global tempString1
if line == 'add':
line = "@SP\nM=M-1\nA=M\nD=M\nA=A-1\nM=M+D\n"
elif line == 'sub':
line = "@SP\nM=M-1\nA=M\nD=M\nA=A-1\nM=M-D\n"
elif line == 'neg':
line = "@SP\nA=M\nA=A-1\nM=-M\n"
elif line == 'eq':
line = '@SP\nM=M-1\nA=M\nD=M\nA=A-1\nD=M-D\n@TRUE' + str(
trueCounter) + '\nD;JEQ\n@SP\nA=M\nA=A-1\nM=0\n@FALSE' + str(
falseCounter) + '\n0;JMP\n(TRUE' + str(trueCounter) + ')\n@SP\nA=M\nA=A-1\nM=-1\n(FALSE' + str(
falseCounter) + ')\n'
trueCounter += 1
falseCounter += 1
elif line == 'gt':
line = '@SP\nM=M-1\nA=M\nD=M\nA=A-1\nD=M-D\n@TRUE' + str(
trueCounter) + '\nD;JGT\n@SP\nA=M\nA=A-1\nM=0\n@FALSE' + str(
falseCounter) + '\n0;JMP\n(TRUE' + str(trueCounter) + ')\n@SP\nA=M\nA=A-1\nM=-1\n(FALSE' + str(
falseCounter) + ')\n'
trueCounter += 1
falseCounter += 1
elif line == 'lt':
line = '@SP\nM=M-1\nA=M\nD=M\nA=A-1\nD=M-D\n@TRUE' + str(
trueCounter) + '\nD;JLT\n@SP\nA=M\nA=A-1\nM=0\n@FALSE' + str(
falseCounter) + '\n0;JMP\n(TRUE' + str(trueCounter) + ')\n@SP\nA=M\nA=A-1\nM=-1\n(FALSE' + str(
falseCounter) + ')\n'
trueCounter += 1
falseCounter += 1
elif line == 'and':
line = "@SP\nM=M-1\nA=M\nD=M\nA=A-1\nM=M&D\n"
elif line == 'or':
line = "@SP\nM=M-1\nA=M\nD=M\nA=A-1\nM=M|D\n"
elif line == 'not':
line = "@SP\nA=M\nA=A-1\nM=!M\n"
tempString1 += line
name1 = None
line, tempString = '', ''
tempString1 = ''
aritmeticList3 = ['add', 'sub', 'neg', 'and', 'not']
aritmeticList2 = ['or', 'gt', 'lt', 'eq']
yourpath = os.getcwd()
for root, dirs, files in os.walk(yourpath, topdown=False):
nam1 = ''
for name in files:
if name[-3:] == '.vm':
name1 = name
tempString1 = ''
tempString = ''
with open(Path(root, name), "r+") as vmfile:
tempString = clear_file(tempString, vmfile)
for j in tempString: # zapisva liniqta v string koito posle se obrabotva
if j != '\n':
line += j
else:
if line[:4] == 'push':
push(line)
line = ''
elif line[:3] == 'pop':
pop(line)
line = ''
elif line[:2] in aritmeticList2 or line[:3] in aritmeticList3:
aritmetic(line)
line = ''
if name1 == name:
with open(Path(root, f'{name1[:-3]}.asm'), 'w+') as newfile:
newfile.write(tempString1)
+1
View File
@@ -0,0 +1 @@
python3 debug