[07/Pointers] Implemented pointer segment push/pop
This commit is contained in:
parent
16e830851e
commit
67b4e2653e
|
@ -108,6 +108,6 @@ wc -l file.hack
|
||||||
|
|
||||||
### Memory Access Commands
|
### Memory Access Commands
|
||||||
|
|
||||||
- [x] BasicTest.vm
|
- [x] BasicTest.vm (228)
|
||||||
- [ ] PointerTest.vm
|
- [x] PointerTest.vm (127)
|
||||||
- [ ] StackTest.vm
|
- [ ] StackTest.vm
|
||||||
|
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
||||||
|
|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046|
|
||||||
|
| 6084 | 3030 | 3040 | 32 | 46 |
|
|
@ -185,6 +185,14 @@ class CodeWriter {
|
||||||
]);
|
]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'pointer':
|
||||||
|
$register = $this->resolvePointer($index);
|
||||||
|
$this->write([
|
||||||
|
"$register // pointer $index",
|
||||||
|
"D=M"
|
||||||
|
]);
|
||||||
|
break;
|
||||||
|
|
||||||
case 'temp':
|
case 'temp':
|
||||||
$register = $this->resolveTemp($index);
|
$register = $this->resolveTemp($index);
|
||||||
$this->write([
|
$this->write([
|
||||||
|
@ -255,6 +263,18 @@ class CodeWriter {
|
||||||
]);
|
]);
|
||||||
break;
|
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':
|
case 'temp':
|
||||||
$tempRegister = $this->resolveTemp($index);
|
$tempRegister = $this->resolveTemp($index);
|
||||||
$this->write([
|
$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
|
// Keeping this because book asked me to
|
||||||
function writePushPop(Int $command, String $segment , Int $index) {
|
function writePushPop(Int $command, String $segment , Int $index) {
|
||||||
switch ($command) {
|
switch ($command) {
|
||||||
|
|
Loading…
Reference in New Issue