File tree Expand file tree Collapse file tree 1 file changed +35
-11
lines changed Expand file tree Collapse file tree 1 file changed +35
-11
lines changed Original file line number Diff line number Diff line change @@ -64,17 +64,41 @@ def write_arithmetic(command)
64
64
end
65
65
66
66
def write_push_pop ( command , segment , index )
67
- output . puts <<-EOF
68
- // Load index into M[SP]
69
- @#{ index }
70
- D=A
71
- @SP
72
- A=M
73
- M=D
74
- // SP++
75
- @SP
76
- M=M+1
77
- EOF
67
+ case command
68
+ when Parser ::C_PUSH
69
+ output . puts <<-EOF
70
+ // Load index into M[SP]
71
+ @#{ index }
72
+ D=A
73
+ @SP
74
+ A=M
75
+ M=D
76
+ // SP++
77
+ @SP
78
+ M=M+1
79
+ EOF
80
+ when Parser ::C_POP
81
+ output . puts <<-EOF
82
+ // Get base address of the local segment
83
+ @LCL // A=1
84
+ D=M // D=RAM[1]=300
85
+ // Add the index offset to the base address
86
+ @#{ index } // A=2
87
+ D=A+D // D=302
88
+ // Store the destination in R13
89
+ @R13 // A=13
90
+ M=D // RAM[13]=302
91
+ // SP--
92
+ @SP // A=0
93
+ AM=M-1 // A,RAM[0]=RAM[0]-1=257
94
+ // D = RAM[A] = the value to pop from the stack
95
+ D=M // D=RAM[257]=23
96
+ // RAM[302] = 23
97
+ @R13 // A=13
98
+ A=M // A=RAM[13]=302
99
+ M=D // RAM[302]=23
100
+ EOF
101
+ end
78
102
end
79
103
80
104
private
You can’t perform that action at this time.
0 commit comments