aboutsummaryrefslogtreecommitdiff
path: root/projects
diff options
context:
space:
mode:
Diffstat (limited to 'projects')
-rw-r--r--projects/08/FunctionCalls/SimpleFunction/SimpleFunction1.asm137
-rw-r--r--projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm16
-rw-r--r--projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries1.asm241
-rwxr-xr-xprojects/08/VMTranslatorbin76760 -> 86992 bytes
-rw-r--r--projects/08/VMTranslator.hibin1693 -> 2434 bytes
-rw-r--r--projects/08/VMTranslator.hs56
-rw-r--r--projects/08/VMTranslator.obin94640 -> 104536 bytes
7 files changed, 417 insertions, 33 deletions
diff --git a/projects/08/FunctionCalls/SimpleFunction/SimpleFunction1.asm b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction1.asm
new file mode 100644
index 0000000..7fe71a5
--- /dev/null
+++ b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction1.asm
@@ -0,0 +1,137 @@
+(SimpleFunction.test)
+@SP
+A=M
+M=0
+@SP
+M=M+1
+@SP
+A=M
+M=0
+@SP
+M=M+1
+@LCL
+D=M
+@0
+D=A+D
+A=D
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+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
+@SP
+AM=M-1
+D=M+D
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@SP
+AM=M-1
+D=M
+@SP
+A=M
+M=!D
+@SP
+M=M+1
+@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
+@SP
+AM=M-1
+D=M+D
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@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
+@SP
+AM=M-1
+D=M-D
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@LCL
+D=M
+@R13
+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
+@R13
+A=M-1
+A=M
+0;JMP
+(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 68a4c8e..3f06978 100644
--- a/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm
+++ b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm
@@ -1,7 +1,3 @@
-@256
-D=A
-@SP
-M=D
@ARG
D=M
@1
@@ -97,7 +93,7 @@ D=M
@R13
A=M
M=D
-(MAIN_LOOP_START)
+($MAIN_LOOP_START)
@ARG
D=M
@0
@@ -112,11 +108,11 @@ M=M+1
@SP
AM=M-1
D=M
-@COMPUTE_ELEMENT
+@$COMPUTE_ELEMENT
D;JNE
-@END_PROGRAM
+@$END_PROGRAM
0;JMP
-(COMPUTE_ELEMENT)
+($COMPUTE_ELEMENT)
@THAT
D=M
@0
@@ -233,9 +229,9 @@ D=M
@R13
A=M
M=D
-@MAIN_LOOP_START
+@$MAIN_LOOP_START
0;JMP
-(END_PROGRAM)
+($END_PROGRAM)
(END)
@END
0;JMP \ No newline at end of file
diff --git a/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries1.asm b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries1.asm
new file mode 100644
index 0000000..68a4c8e
--- /dev/null
+++ b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries1.asm
@@ -0,0 +1,241 @@
+@256
+D=A
+@SP
+M=D
+@ARG
+D=M
+@1
+D=A+D
+A=D
+D=M
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=M-1
+D=M
+@4
+M=D
+@ARG
+D=M
+@0
+D=A+D
+A=D
+D=M
+@SP
+A=M
+M=D
+@SP
+M=M+1
+@1
+D=A
+@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
+@ARG
+D=M
+@0
+D=A+D
+@R13
+M=D
+@SP
+AM=M-1
+D=M
+@R13
+A=M
+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 8764eb0..b1b665f 100755
--- a/projects/08/VMTranslator
+++ b/projects/08/VMTranslator
Binary files differ
diff --git a/projects/08/VMTranslator.hi b/projects/08/VMTranslator.hi
index 078767f..66e0a40 100644
--- a/projects/08/VMTranslator.hi
+++ b/projects/08/VMTranslator.hi
Binary files differ
diff --git a/projects/08/VMTranslator.hs b/projects/08/VMTranslator.hs
index 099ba75..c3a7047 100644
--- a/projects/08/VMTranslator.hs
+++ b/projects/08/VMTranslator.hs
@@ -3,54 +3,58 @@ import Data.List.Split (splitOn)
import System.Environment (getArgs)
import Data.Maybe (fromJust)
import Data.List (elemIndex)
+import System.Directory (listDirectory)
--preamble = "@256\nD=A\n@SP\nM=D\n"
preamble = ""
epilogue = "(END)\n@END\n0;JMP"
-parse' :: [Char] -> [[Char]] -> Int -> [Char] -> [Char]
+-- parse' command restOfCommand vmLineNumber fileName functionName = asm code
+parse' :: [Char] -> [[Char]] -> Int -> [Char] -> [Char] -> [Char]
-parse' "return" [] _ _ = backupLCL ++ popToARG ++ moveSP ++ restore "THAT"
- ++ restore "THIS" ++ restore "ARG" ++ restore "LCL"
- ++ gotoRet
+parse' "return" [] _ _ _ = backupLCL ++ popToARG ++ moveSP ++ restore "THAT"
+ ++ restore "THIS" ++ restore "ARG" ++ restore "LCL"
+ ++ gotoRet
where backupLCL = "@LCL\nD=M\n@R13\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"
-parse' op [] n _
+parse' op [] n _ _
| op `elem` ["add", "sub", "and", "or"] = pop "M" ++ pop ('M':(f op):"D") ++ push "D"
| op `elem` ["eq", "gt", "lt"] = pop "M" ++ pop "M-D" ++ ifThenElse op n
| otherwise = pop "M" ++ push (f op:"D")
where f "add" = '+'; f "sub" = '-'; f "and" = '&';
f "or" = '|'; f "neg" = '-'; f "not" = '!'
-parse' "push" ["constant", x] _ _ = "@" ++ x ++ "\nD=A\n" ++ push "D"
+parse' "push" ["constant", x] _ _ _ = "@" ++ x ++ "\nD=A\n" ++ push "D"
-parse' cmd [seg, x] _ _ | seg `elem` ["local", "argument", "this", "that"] =
+parse' cmd [seg, x] _ _ _ | seg `elem` ["local", "argument", "this", "that"] =
case cmd of
"push" -> getAddr seg x ++ "A=D\nD=M\n" ++ push "D"
"pop" -> getAddr seg x ++ "@R13\nM=D\n" ++ pop "M" ++ "@R13\nA=M\nM=D\n"
-parse' cmd [seg, x] _ _ | seg `elem` ["pointer", "temp"] =
+parse' cmd [seg, x] _ _ _ | seg `elem` ["pointer", "temp"] =
case cmd of
"push" -> getAddr' seg x ++ "D=M\n" ++ push "D"
"pop" -> pop "M" ++ getAddr' seg x ++ "M=D\n"
-parse' cmd ["static", x] _ filename =
+parse' cmd ["static", x] _ filename _ =
case cmd of
"push" -> getAddr'' x filename ++ "D=M\n" ++ push "D"
"pop" -> pop "M" ++ getAddr'' x filename ++ "M=D\n"
-parse' "label" [x] _ _ = "(" ++ x ++ ")\n"
+parse' "label" [x] _ _ fName = "(" ++ fName ++ "$" ++ x ++ ")\n"
-parse' "goto" [x] _ _ = "@" ++ x ++ "\n0;JMP\n"
+parse' "goto" [x] _ _ fName = "@" ++ fName ++ "$" ++ x ++ "\n0;JMP\n"
-parse' "if-goto" [x] _ _ = pop "M" ++ "@" ++ x ++ "\nD;JNE\n"
+parse' "if-goto" [x] _ _ fName = pop "M" ++ "@" ++ fName ++ "$" ++ x ++ "\nD;JNE\n"
-parse' "function" [f, n] _ _ = "(" ++ f ++ ")\n" ++ (mconcat $ replicate (read n) $ push "0")
+parse' "function" [f, n] _ _ _ = "(" ++ f ++ ")\n" ++ (mconcat $ replicate (read n) $ push "0")
+
+--parse' "call" [f, m] n _ =
getAddr seg x = "@" ++ seg2Lab seg ++ "\nD=M\n@" ++ x ++ "\nD=A+D\n"
@@ -76,10 +80,11 @@ ifThenElse cond n = "@" ++ cond' ++ show n ++ "\nD;J" ++ cond' ++ "\n" ++ push "
pop :: [Char] -> [Char]
pop xs = "@SP\nAM=M-1\nD=" ++ xs ++ "\n"
-parseline :: [[Char]] -> Int -> [Char] -> [Char] -> [Char]
-parseline [] _ acc _ = acc
-parseline (line:lines) n acc filename = parseline lines (n + 1) (acc ++ parse' cmd target n filename) filename
+parseline :: [[Char]] -> Int -> [Char] -> [Char] -> [Char] -> [Char]
+parseline [] _ acc _ _ = acc
+parseline (line:lines) n acc filename funName = parseline lines (n + 1) (acc ++ parse' cmd target n filename funName') filename funName'
where cmd:target = words line
+ funName' = if cmd == "function" then head target else funName
stripJunk :: [Char] -> [[Char]]
stripJunk = filter (not . isEmptyLine) . fmap (head . splitOn "//") . lines . replCrWithNl
@@ -88,7 +93,7 @@ isEmptyLine :: [Char] -> Bool
isEmptyLine = null . filter (not . flip elem " \t")
parseCode :: [Char] -> [Char] -> [Char]
-parseCode xs filename = preamble ++ (parseline (stripJunk xs) 0 "" filename) ++ epilogue
+parseCode xs filename = preamble ++ (parseline (stripJunk xs) 0 "" filename "") ++ epilogue
replCrWithNl :: [Char] -> [Char]
replCrWithNl = fmap cr2nl
@@ -98,10 +103,15 @@ replCrWithNl = fmap cr2nl
lastSplit c xs = (take (prefix - 1) xs, drop prefix xs)
where prefix = length xs - (fromJust . elemIndex c . reverse) xs
+
+-- assuming the input is a dir with a trailing '/'
main = do
- args <- getArgs
- let path = head args
- let pathWithoutExt = fst $ lastSplit '.' path
- let filename = snd $ lastSplit '/' pathWithoutExt
- code <- readFile path
- writeFile (pathWithoutExt ++ ".asm") (parseCode code filename)
+ dir <- head <$> getArgs
+ filesWODir <- filter isVMfile <$> listDirectory dir
+ let vmFiles = (dir++) <$> filesWODir
+ let ofPath = dir ++ (snd $ lastSplit '/' $ init dir) ++ ".asm"
+ let filenames = removeExt <$> filesWODir
+ codes <- sequence $ readFile <$> vmFiles
+ writeFile ofPath (mconcat $ zipWith parseCode codes filenames)
+ where isVMfile xs = drop (length xs - 3) xs == ".vm"
+ removeExt xs = take (length xs - 3) xs
diff --git a/projects/08/VMTranslator.o b/projects/08/VMTranslator.o
index 134f4c7..09c5269 100644
--- a/projects/08/VMTranslator.o
+++ b/projects/08/VMTranslator.o
Binary files differ