Skip to content

Commit

Permalink
Project 7 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
luismomm2110 committed Apr 14, 2021
1 parent 0c25b6d commit 4989c4f
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 10 deletions.
2 changes: 0 additions & 2 deletions projects/07/MemoryAccess/PointerTest/PointerTest.out
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046|
| 6084 | 3030 | 3040 | 32 | 46 |
Binary file not shown.
Binary file not shown.
87 changes: 87 additions & 0 deletions projects/07/MemoryAccess/StaticTest/StaticTest.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//1 constant 111
@111
D=A
@SP
A=M
M=D
@SP
M=M+1
//1 constant 333
@333
D=A
@SP
A=M
M=D
@SP
M=M+1
//1 constant 888
@888
D=A
@SP
A=M
M=D
@SP
M=M+1
//2 static 8
@SP
AM=M-1
D=M
@StaticTest.8
M=D
//2 static 3
@SP
AM=M-1
D=M
@StaticTest.3
M=D
//2 static 1
@SP
AM=M-1
D=M
@StaticTest.1
M=D
//1 static 3
@StaticTest.3
D=M
@SP
A=M
M=D
@SP
M=M+1
//1 static 1
@StaticTest.1
D=M
@SP
A=M
M=D
@SP
M=M+1
//sub
@SP
AM=M-1
D=M
@SP
AM=M-1
M=M-D
@SP
M=M+1
//1 static 8
@StaticTest.8
D=M
@SP
A=M
M=D
@SP
M=M+1
//add
@SP
AM=M-1
M=D
@SP
AM=M-1
M=M+D
@SP
M=M+1
(END)
@END
0;JMP
2 changes: 2 additions & 0 deletions projects/07/MemoryAccess/StaticTest/StaticTest.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
|RAM[256]|
| 1110 |
Binary file modified projects/07/src/CodeWriter.class
Binary file not shown.
31 changes: 25 additions & 6 deletions projects/07/src/CodeWriter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import java.io.*;

public class CodeWriter {


private static Integer numJump;
private String pathAssembly;
private File assemblyTranslate;
Expand Down Expand Up @@ -69,28 +67,44 @@ public void writePushPop(int command, String segment, int c_index) throws IOExce
//push constant
if (segment.equals("constant")) {
this.assemblyWriter.write("@" + Integer.toString(c_index) + "\n" + "D=A\n" + "@SP\n" + "A=M\n" + "M=D\n" + advSP());

//push local, static, this, that
} else if (!chooseSeg(segment, c_index).isBlank()) {
this.assemblyWriter.write(chooseSeg(segment, c_index) + "A=A+D\n" + "D=M\n" + "@SP\n" + "A=M\n" + "M=D\n" + advSP());

//push temp
} else if (segment.equals("temp")) {
this.assemblyWriter.write("@R" + Integer.toString(5 + c_index) + "\n" + "D=M\n" + "@SP\n" + "A=M\n" + "M=D\n" + advSP());

//push pointer
} else if (segment.equals("pointer")) {
this.assemblyWriter.write("@" + Integer.toString(3 + c_index) + "\n" + "D=M\n" + "@SP\n" + "A=M\n" + "M=D\n" + advSP());

//push static
} else if (segment.equals("static")){
this.assemblyWriter.write(staticName(c_index) + "D=M\n" + "@SP\n" + "A=M\n" + "M=D\n" + advSP());

} else {
this.assemblyWriter.write("ERROR IN COMMAND PUSH");
}
} else if (command == 2) {
//pop temp
if (segment.equals("temp")) {
this.assemblyWriter.write(backSP() + "D=M\n" + "@R" + Integer.toString(5 + c_index) + "\n" + "M=D\n");
} //pop local, static, this, that
else if (!chooseSeg(segment, c_index).isBlank()) {
this.assemblyWriter.write(chooseSeg(segment, c_index) + "D=A+D\n" + "@adress\n" + "M=D\n" + backSP() + "D=M\n" +
"@adress\n" + "A=M\n" + "M=D\n");

//pop local, static, this, that
} else if (!chooseSeg(segment, c_index).isBlank()) {
this.assemblyWriter.write(chooseSeg(segment, c_index) + "D=A+D\n" + "@adress\n" + "M=D\n" + backSP() + "D=M\n"
+ "@adress\n" + "A=M\n" + "M=D\n");

//pop pointer
} else if (segment.equals("pointer")) {
this.assemblyWriter.write(backSP() + "D=M\n" + "@" + Integer.toString(3 + c_index) + "\n" + "M=D\n");

//pop static
} else if (segment.equals("static")) {
this.assemblyWriter.write(backSP() + "D=M\n" + staticName(c_index) + "M=D\n");

} else {
this.assemblyWriter.write("ERROR IN COMMAND POP");
}
Expand Down Expand Up @@ -140,6 +154,11 @@ private String compCommands(String type) {
+ "\n" + "D;JMP\n" + "(FALSE" + numJump + ")\n" + "@SP\n" + "A=M\n" + "M=0\n" + "(CONTINUE" + numJump + ")\n" + advSP();
}

private String staticName(int index) {
String name = this.assemblyTranslate.getName().replace(".asm", ("." + Integer.toString(index)));
return "@" + name + "\n";
}

private String backSP () {
return "@SP\n" + "AM=M-1\n";
}
Expand Down
2 changes: 1 addition & 1 deletion tools/bin/CPU Emulator.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:\Users\luism\Desktop\nand2Tetris\nand2tetris.part2--java\projects\07\MemoryAccess\PointerTest
C:\Users\luism\Desktop\nand2Tetris\nand2tetris.part2--java\projects\07\MemoryAccess\StaticTest
2 changes: 1 addition & 1 deletion tools/bin/Virtual Machine Emulator.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:\Users\luism\Desktop\nand2Tetris\nand2tetris\projects\07\MemoryAccess\PointerTest
C:\Users\luism\Desktop\nand2Tetris\nand2tetris\projects\07\MemoryAccess\StaticTest

0 comments on commit 4989c4f

Please sign in to comment.