From 4e49913808da8593c332b230f0bc292537ac40d7 Mon Sep 17 00:00:00 2001 From: Nemo Date: Tue, 2 Jun 2020 17:28:20 +0530 Subject: [PATCH] [07/Static] Implements static push/pop memory segment --- .../07/MemoryAccess/StaticTest/StaticTest.asm | 73 +++++++++++++++++++ .../07/MemoryAccess/StaticTest/StaticTest.out | 2 + vm/CodeWriter.php | 27 +++++++ vm/VMTranslator.php | 1 + 4 files changed, 103 insertions(+) create mode 100644 projects/07/MemoryAccess/StaticTest/StaticTest.asm create mode 100644 projects/07/MemoryAccess/StaticTest/StaticTest.out diff --git a/projects/07/MemoryAccess/StaticTest/StaticTest.asm b/projects/07/MemoryAccess/StaticTest/StaticTest.asm new file mode 100644 index 0000000..754a1a1 --- /dev/null +++ b/projects/07/MemoryAccess/StaticTest/StaticTest.asm @@ -0,0 +1,73 @@ +@111 // push constant 111 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 111 (L0) +@333 // push constant 333 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 333 (L1) +@888 // push constant 888 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 888 (L2) +@SP //pop static 8 +AM=M-1 +D=M +@StaticTest.8 +M=D // end pop static 8 (L3) +@SP //pop static 3 +AM=M-1 +D=M +@StaticTest.3 +M=D // end pop static 3 (L4) +@SP //pop static 1 +AM=M-1 +D=M +@StaticTest.1 +M=D // end pop static 1 (L5) +@StaticTest.3 +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push static 3 (L6) +@StaticTest.1 +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push static 1 (L7) +@SP // ==== sub ==== +A=M-1 +D=M +A=A-1 +M=M-D +@SP +M=M-1 // end sub (L8) +@StaticTest.8 +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push static 8 (L9) +@SP // ==== add ==== +A=M-1 +D=M +A=A-1 +M=D+M +@SP +M=M-1 // end add (L10) +@72 +0;JMP diff --git a/projects/07/MemoryAccess/StaticTest/StaticTest.out b/projects/07/MemoryAccess/StaticTest/StaticTest.out new file mode 100644 index 0000000..2bc908b --- /dev/null +++ b/projects/07/MemoryAccess/StaticTest/StaticTest.out @@ -0,0 +1,2 @@ +|RAM[256]| +| 1110 | diff --git a/vm/CodeWriter.php b/vm/CodeWriter.php index 7044e6c..0558947 100644 --- a/vm/CodeWriter.php +++ b/vm/CodeWriter.php @@ -9,6 +9,10 @@ class CodeWriter { $this->file = fopen($outputFile, "w"); } + function setInputFileName($inputFileName) { + $this->vm = basename($inputFileName, ".vm"); + } + function nextSourceLine() { $this->sourceLine += 1; } @@ -185,6 +189,14 @@ class CodeWriter { ]); break; + case 'static': + $symbol = $this->resolveStatic($index); + $this->write([ + $symbol, + "D=M" + ]); + break; + case 'pointer': $register = $this->resolvePointer($index); $this->write([ @@ -239,6 +251,10 @@ class CodeWriter { ]); } + private function resolveStatic(Int $index) { + return "@{$this->vm}.$index"; + } + private function writePop(String $segment, Int $index) { switch ($segment) { // The address is given by LCL+INDEX @@ -263,6 +279,17 @@ class CodeWriter { ]); break; + case 'static': + $symbol = $this->resolveStatic($index); + $this->write([ + "@SP //pop $segment $index", + "AM=M-1", + "D=M", + $symbol, + "M=D // end pop $segment $index (L{$this->sourceLine})" + ]); + break; + case 'pointer': // pointer points to a 2 register segment holding [this, that] $register = $this->resolvePointer($index); diff --git a/vm/VMTranslator.php b/vm/VMTranslator.php index 68b9e68..eea9d51 100644 --- a/vm/VMTranslator.php +++ b/vm/VMTranslator.php @@ -24,6 +24,7 @@ class VMTranslator { function translate() { foreach ($this->files as $file) { $parser = new Parser($file); + $this->writer->setInputFileName($file); foreach ($parser->commands() as $command) { $commandType = CommandType::fromName($command);