diff --git a/README.md b/README.md index 93ec5ea..2a904e5 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,6 @@ wc -l file.hack ### Memory Access Commands -- [x] BasicTest.vm -- [ ] PointerTest.vm +- [x] BasicTest.vm (228) +- [x] PointerTest.vm (127) - [ ] StackTest.vm diff --git a/projects/07/MemoryAccess/PointerTest/PointerTest.asm b/projects/07/MemoryAccess/PointerTest/PointerTest.asm new file mode 100644 index 0000000..420b30f --- /dev/null +++ b/projects/07/MemoryAccess/PointerTest/PointerTest.asm @@ -0,0 +1,127 @@ +@3030 // push constant 3030 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 3030 (L0) +@SP // pop +AM=M-1 +D=M +@THIS +M=D // (L1) +@3040 // push constant 3040 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 3040 (L2) +@SP // pop +AM=M-1 +D=M +@THAT +M=D // (L3) +@32 // push constant 32 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 32 (L4) +@THIS // this 2 +D=M +@2 // write 2 to A +D=D+A // D = segment+index +@R13 // save it to R13 +M=D // write @THIS+2 to R13 +@SP // pop +AM=M-1 +D=M +@R13 +A=M // Read @R13 to A (for this 2) +M=D // end pop this 2 (L5) +@46 // push constant 46 +D=A +@SP +A=M +M=D +@SP +M=M+1 // end push constant 46 (L6) +@THAT // that 6 +D=M +@6 // write 6 to A +D=D+A // D = segment+index +@R13 // save it to R13 +M=D // write @THAT+6 to R13 +@SP // pop +AM=M-1 +D=M +@R13 +A=M // Read @R13 to A (for that 6) +M=D // end pop that 6 (L7) +@THIS // pointer 0 +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push pointer 0 (L8) +@THAT // pointer 1 +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push pointer 1 (L9) +@SP // ==== add ==== +A=M-1 +D=M +A=A-1 +M=D+M +@SP +M=M-1 // end add (L10) +@THIS // this 2 +D=M +@2 // write 2 to A +D=D+A // D = segment+index +@R13 // save it to R13 +M=D // write @THIS+2 to R13 +@R13 +A=M +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push this 2 (L11) +@SP // ==== sub ==== +A=M-1 +D=M +A=A-1 +M=M-D +@SP +M=M-1 // end sub (L12) +@THAT // that 6 +D=M +@6 // write 6 to A +D=D+A // D = segment+index +@R13 // save it to R13 +M=D // write @THAT+6 to R13 +@R13 +A=M +D=M +@SP +A=M +M=D +@SP +M=M+1 // end push that 6 (L13) +@SP // ==== add ==== +A=M-1 +D=M +A=A-1 +M=D+M +@SP +M=M-1 // end add (L14) +@126 +0;JMP diff --git a/projects/07/MemoryAccess/PointerTest/PointerTest.out b/projects/07/MemoryAccess/PointerTest/PointerTest.out new file mode 100644 index 0000000..5d62de8 --- /dev/null +++ b/projects/07/MemoryAccess/PointerTest/PointerTest.out @@ -0,0 +1,2 @@ +|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046| +| 6084 | 3030 | 3040 | 32 | 46 | diff --git a/vm/CodeWriter.php b/vm/CodeWriter.php index 13aac78..7044e6c 100644 --- a/vm/CodeWriter.php +++ b/vm/CodeWriter.php @@ -185,6 +185,14 @@ class CodeWriter { ]); break; + case 'pointer': + $register = $this->resolvePointer($index); + $this->write([ + "$register // pointer $index", + "D=M" + ]); + break; + case 'temp': $register = $this->resolveTemp($index); $this->write([ @@ -255,6 +263,18 @@ class CodeWriter { ]); break; + case 'pointer': + // pointer points to a 2 register segment holding [this, that] + $register = $this->resolvePointer($index); + $this->write([ + "@SP // pop", + "AM=M-1", + "D=M", + $register, + "M=D // (L{$this->sourceLine})" + ]); + break; + case 'temp': $tempRegister = $this->resolveTemp($index); $this->write([ @@ -271,6 +291,10 @@ class CodeWriter { } } + private function resolvePointer(Int $index) { + return $index == 0 ? '@THIS' : '@THAT'; + } + // Keeping this because book asked me to function writePushPop(Int $command, String $segment , Int $index) { switch ($command) {