aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2018-01-06 17:24:28 +0100
committerYuchen Pei <me@ypei.me>2018-01-06 17:24:28 +0100
commit29a1931f2ee4e72ddd240096bf6fe9a04668f131 (patch)
tree03bbc0366ed2b00de6a276f908c751971d0d5628
parentf84dcda74e13120c5433591feeeed7e7d4c5f322 (diff)
finished project 8.
-rw-r--r--projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm16
-rw-r--r--projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm11
-rw-r--r--projects/08/FunctionCalls/StaticsTest/StaticsTest.asm586
-rw-r--r--projects/08/FunctionCalls/StaticsTest/StaticsTest.out2
-rw-r--r--projects/08/ProgramFlow/BasicLoop/BasicLoop.asm11
-rw-r--r--projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm3
-rwxr-xr-xprojects/08/VMTranslatorbin93776 -> 93880 bytes
-rw-r--r--projects/08/VMTranslator.hs10
-rw-r--r--projects/08/VMTranslator.obin118200 -> 118760 bytes
9 files changed, 616 insertions, 23 deletions
diff --git a/projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm b/projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm
index 9d98a3f..a2ca368 100644
--- a/projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm
+++ b/projects/08/FunctionCalls/FibonacciElement/FibonacciElement.asm
@@ -114,6 +114,11 @@ M=M+1
D=M
@R13
M=D
+@5
+A=D-A
+D=M
+@R14
+M=D
@SP
AM=M-1
D=M
@@ -144,8 +149,7 @@ AM=M-1
D=M
@LCL
M=D
-@R13
-A=M-1
+@R14
A=M
0;JMP
(Main.fibonacci$IF_FALSE)
@@ -318,6 +322,11 @@ M=M+1
D=M
@R13
M=D
+@5
+A=D-A
+D=M
+@R14
+M=D
@SP
AM=M-1
D=M
@@ -348,8 +357,7 @@ AM=M-1
D=M
@LCL
M=D
-@R13
-A=M-1
+@R14
A=M
0;JMP
(Sys.init)
diff --git a/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm
index 7fe71a5..de3a0da 100644
--- a/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm
+++ b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm
@@ -98,6 +98,11 @@ M=M+1
D=M
@R13
M=D
+@5
+A=D-A
+D=M
+@R14
+M=D
@SP
AM=M-1
D=M
@@ -128,10 +133,6 @@ AM=M-1
D=M
@LCL
M=D
-@R13
-A=M-1
+@R14
A=M
0;JMP
-(END)
-@END
-0;JMP \ No newline at end of file
diff --git a/projects/08/FunctionCalls/StaticsTest/StaticsTest.asm b/projects/08/FunctionCalls/StaticsTest/StaticsTest.asm
new file mode 100644
index 0000000..2d6f2aa
--- /dev/null
+++ b/projects/08/FunctionCalls/StaticsTest/StaticsTest.asm
@@ -0,0 +1,586 @@
+@256
+D=A
+@SP
+M=D
+@RET0
+D=A
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@ARG
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@THIS
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@THAT
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@SP
+D=M
+@LCL
+M=D
+@5
+D=D-A
+@0
+D=D-A
+@ARG
+M=D
+@Sys.init
+0;JMP
+(RET0)
+(Class2.set)
+@ARG
+D=M
+@0
+D=A+D
+A=D
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@SP
+AM=M-1
+D=M
+@Class2.0
+M=D
+@ARG
+D=M
+@1
+D=A+D
+A=D
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@SP
+AM=M-1
+D=M
+@Class2.1
+M=D
+@0
+D=A
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+D=M
+@R13
+M=D
+@5
+A=D-A
+D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P
+M=M+1
+@SP
+AM=M-1
+D=M
+@Class1.1
+M=D
+@0
+D=A
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+D=M
+@R13
+M=D
+@5
+A=D-A
+D=M
+@R14
+M=D
+@SP
+AM=M-1
+D=M
+@ARG
+A=M
+M=D
+@ARG
+D=M+1
+@SP
+M=D
+@R13
+AM=M-1
+D=M
+@THAT
+M=D
+@R13
+AM=M-1
+D=M
+@THIS
+M=D
+@R13
+AM=M-1
+D=M
+@ARG
+M=D
+@R13
+AM=M-1
+D=M
+@LCL
+M=D
+@R14
+A=M
+0;JMP
+(Class1.get)
+@Class1.0
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@Class1.1
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@SP
+AM=M-1
+D=M
+@SP
+AM=M-1
+D=M-D
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+D=M
+@R13
+M=D
+@5
+A=D-A
+D=M
+@R14
+M=D
+@SP
+AM=M-1
+D=M
+@ARG
+A=M
+M=D
+@ARG
+D=M+1
+@SP
+M=D
+@R13
+AM=M-1
+D=M
+@THAT
+M=D
+@R13
+AM=M-1
+D=M
+@THIS
+M=D
+@R13
+AM=M-1
+D=M
+@ARG
+M=D
+@R13
+AM=M-1
+D=M
+@LCL
+M=D
+@R14
+A=M
+0;JMP
diff --git a/projects/08/FunctionCalls/StaticsTest/StaticsTest.out b/projects/08/FunctionCalls/StaticsTest/StaticsTest.out
new file mode 100644
index 0000000..309f058
--- /dev/null
+++ b/projects/08/FunctionCalls/StaticsTest/StaticsTest.out
@@ -0,0 +1,2 @@
+| RAM[0] |RAM[261]|RAM[262]|
+| 263 | -2 | 8 |
diff --git a/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm b/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm
index 62aa0cf..b2e42a7 100644
--- a/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm
+++ b/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm
@@ -1,7 +1,3 @@
-@256
-D=A
-@SP
-M=D
@0
D=A
@SP
@@ -21,7 +17,7 @@ D=M
@R13
A=M
M=D
-(LOOP_START)
+($LOOP_START)
@ARG
D=M
@0
@@ -122,7 +118,7 @@ M=M+1
@SP
AM=M-1
D=M
-@LOOP_START
+@$LOOP_START
D;JNE
@LCL
D=M
@@ -135,6 +131,3 @@ A=M
M=D
@SP
M=M+1
-(END)
-@END
-0;JMP \ No newline at end of file
diff --git a/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm
index 3f06978..3436cef 100644
--- a/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm
+++ b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm
@@ -232,6 +232,3 @@ M=D
@$MAIN_LOOP_START
0;JMP
($END_PROGRAM)
-(END)
-@END
-0;JMP \ No newline at end of file
diff --git a/projects/08/VMTranslator b/projects/08/VMTranslator
index c4481c4..2b04f49 100755
--- a/projects/08/VMTranslator
+++ b/projects/08/VMTranslator
Binary files differ
diff --git a/projects/08/VMTranslator.hs b/projects/08/VMTranslator.hs
index d8a2e46..d5a7075 100644
--- a/projects/08/VMTranslator.hs
+++ b/projects/08/VMTranslator.hs
@@ -1,3 +1,5 @@
+--usage: ./VMTranslator path/to/dir/
+--will produce path/to/dir/dir.asm from path/to/dir/*.vm
import Data.Char (toUpper)
import Data.List.Split (splitOn)
import System.Environment (getArgs)
@@ -6,6 +8,9 @@ import Data.List (elemIndex)
import System.Directory (listDirectory)
--preamble = "@256\nD=A\n@SP\nM=D\n"
+--For ProgramFlow and SimpleFunction preamble should be set as ""
+--preamble = ""
+--For StaticsTest and FibonacciElement, preamble should be setSP ++ callInit
preamble = setSP ++ callInit
where setSP = "@256\nD=A\n@SP\nM=D\n"
callInit = parse' "call" ["Sys.init", "0"] 0 "" ""
@@ -16,14 +21,15 @@ epilogue = ""
-- parse' command restOfCommand vmLineNumber fileName functionName = asm code
parse' :: [Char] -> [[Char]] -> Int -> [Char] -> [Char] -> [Char]
-parse' "return" [] _ _ _ = backupLCL ++ popToARG ++ moveSP ++ restore "THAT"
+parse' "return" [] _ _ _ = backupLCL ++ backupRet ++ popToARG ++ moveSP ++ restore "THAT"
++ restore "THIS" ++ restore "ARG" ++ restore "LCL"
++ gotoRet
where backupLCL = "@LCL\nD=M\n@R13\nM=D\n"
+ backupRet = "@5\nA=D-A\nD=M\n@R14\nM=D\n"
popToARG = pop "M" ++ "@ARG\nA=M\nM=D\n"
moveSP = "@ARG\nD=M+1\n@SP\nM=D\n"
restore name = "@R13\nAM=M-1\nD=M\n@" ++ name ++ "\nM=D\n"
- gotoRet = "@R13\nA=M-1\nA=M\n0;JMP\n"
+ gotoRet = "@R14\nA=M\n0;JMP\n"
parse' op [] n fileName _
| op `elem` ["add", "sub", "and", "or"] = pop "M" ++ pop ('M':(f op):"D") ++ push "D"
diff --git a/projects/08/VMTranslator.o b/projects/08/VMTranslator.o
index 3afefab..1d984cf 100644
--- a/projects/08/VMTranslator.o
+++ b/projects/08/VMTranslator.o
Binary files differ