Skip to content

Commit

Permalink
project 7: pass the test of StackTest
Browse files Browse the repository at this point in the history
  • Loading branch information
OccupyMars2025 committed May 2, 2024
1 parent a9f03e5 commit 8e372fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
50 changes: 34 additions & 16 deletions nand2tetris/projects/7/StackArithmetic/StackTest/StackTest.asm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ D=M-D
M=-1
@END_EQUAL_ID_25
D;JEQ
@SP
A=M-1
M=0
(END_EQUAL_ID_25)
// push constant 17
Expand Down Expand Up @@ -52,10 +54,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_EQUAL_ID_54
@END_EQUAL_ID_56
D;JEQ
@SP
A=M-1
M=0
(END_EQUAL_ID_54)
(END_EQUAL_ID_56)
// push constant 16
@16
D=A
Expand All @@ -81,10 +85,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_EQUAL_ID_83
@END_EQUAL_ID_87
D;JEQ
@SP
A=M-1
M=0
(END_EQUAL_ID_83)
(END_EQUAL_ID_87)
// push constant 892
@892
D=A
Expand All @@ -110,10 +116,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_LT_ID_112
@END_LT_ID_118
D;JLT
@SP
A=M-1
M=0
(END_LT_ID_112)
(END_LT_ID_118)
// push constant 891
@891
D=A
Expand All @@ -139,10 +147,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_LT_ID_141
@END_LT_ID_149
D;JLT
@SP
A=M-1
M=0
(END_LT_ID_141)
(END_LT_ID_149)
// push constant 891
@891
D=A
Expand All @@ -168,10 +178,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_LT_ID_170
@END_LT_ID_180
D;JLT
@SP
A=M-1
M=0
(END_LT_ID_170)
(END_LT_ID_180)
// push constant 32767
@32767
D=A
Expand All @@ -197,10 +209,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_GT_ID_199
@END_GT_ID_211
D;JGT
@SP
A=M-1
M=0
(END_GT_ID_199)
(END_GT_ID_211)
// push constant 32766
@32766
D=A
Expand All @@ -226,10 +240,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_GT_ID_228
@END_GT_ID_242
D;JGT
@SP
A=M-1
M=0
(END_GT_ID_228)
(END_GT_ID_242)
// push constant 32766
@32766
D=A
Expand All @@ -255,10 +271,12 @@ D=M
A=M-1
D=M-D
M=-1
@END_GT_ID_257
@END_GT_ID_273
D;JGT
@SP
A=M-1
M=0
(END_GT_ID_257)
(END_GT_ID_273)
// push constant 57
@57
D=A
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| RAM[0] | RAM[256] | RAM[257] | RAM[258] | RAM[259] | RAM[260] |
| 266 | -1 | 0 | 0 | 0 | -1 |
| RAM[261] | RAM[262] | RAM[263] | RAM[264] | RAM[265] |
| 0 | -1 | 0 | 0 | -91 |
16 changes: 16 additions & 0 deletions nand2tetris/projects/7/my_vm/code_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def writeArithmetic(self, command: str):
self.output.append('A=M\n')
self.output.append('D=M\n')
# get the 1st operand from the top of the stack and you can refer to it using the M register
# Caution: I actually don't decrease the stack pointer here when getting the 1st operand
self.output.append('@SP\n')
self.output.append('A=M-1\n')
elif command in UNARY_OPEARATORS:
Expand All @@ -39,6 +40,11 @@ def writeArithmetic(self, command: str):
current_id_of_eq_command = len(self.output)
self.output.append(f'@END_EQUAL_ID_{current_id_of_eq_command}\n')
self.output.append('D;JEQ\n')
## ======== Caution: you need to assign SP-1 to A register "again" in case the A register has been modified by the previous command,
# if the A register has been modified by the previous command, then the M register contains some unkown value, so we need to assign SP-1 to A register again
self.output.append('@SP\n')
self.output.append('A=M-1\n')
## =========
self.output.append('M=0\n')
self.output.append(f'(END_EQUAL_ID_{current_id_of_eq_command})\n')
elif command == 'gt':
Expand All @@ -51,6 +57,11 @@ def writeArithmetic(self, command: str):
current_id_of_gt_command = len(self.output)
self.output.append(f'@END_GT_ID_{current_id_of_gt_command}\n')
self.output.append('D;JGT\n')
## ======== Caution: you need to assign SP-1 to A register "again" in case the A register has been modified by the previous command,
# if the A register has been modified by the previous command, then the M register contains some unkown value, so we need to assign SP-1 to A register again
self.output.append('@SP\n')
self.output.append('A=M-1\n')
## =========
self.output.append('M=0\n')
self.output.append(f'(END_GT_ID_{current_id_of_gt_command})\n')
elif command == 'lt':
Expand All @@ -63,6 +74,11 @@ def writeArithmetic(self, command: str):
current_id_of_lt_command = len(self.output)
self.output.append(f'@END_LT_ID_{current_id_of_lt_command}\n')
self.output.append('D;JLT\n')
## ======== Caution: you need to assign SP-1 to A register "again" in case the A register has been modified by the previous command,
# if the A register has been modified by the previous command, then the M register contains some unkown value, so we need to assign SP-1 to A register again
self.output.append('@SP\n')
self.output.append('A=M-1\n')
## =========
self.output.append('M=0\n')
self.output.append(f'(END_LT_ID_{current_id_of_lt_command})\n')
else:
Expand Down
1 change: 1 addition & 0 deletions nand2tetris/projects/7/my_vm/vm_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def translate(self):

if __name__ == '__main__':
# python -m my_vm.vm_translator ./StackArithmetic/SimpleAdd/SimpleAdd.vm
# python -m my_vm.vm_translator ./StackArithmetic/StackTest/StackTest.vm
import sys
if len(sys.argv) != 2:
print('Please provide a VM file.')
Expand Down

0 comments on commit 8e372fc

Please sign in to comment.