From 0b1849231f9fbd5b6ab44dfb8c831a3893996dfb Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 5 Jan 2018 13:10:41 +0100 Subject: partially finished project 08 - finished flow bit (will need to change the labels to f$label) - finished simplefunction test --- .../SimpleFunction/SimpleFunction.asm | 137 ++++++++++++ .../SimpleFunction/SimpleFunction.out | 2 + projects/08/ProgramFlow/BasicLoop/BasicLoop.asm | 140 ++++++++++++ projects/08/ProgramFlow/BasicLoop/BasicLoop.out | 2 + .../FibonacciSeries/FibonacciSeries.asm | 241 +++++++++++++++++++++ .../FibonacciSeries/FibonacciSeries.out | 2 + projects/08/VMTranslator | Bin 0 -> 76760 bytes projects/08/VMTranslator.hi | Bin 0 -> 1693 bytes projects/08/VMTranslator.hs | 107 +++++++++ projects/08/VMTranslator.o | Bin 0 -> 94640 bytes 10 files changed, 631 insertions(+) create mode 100644 projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm create mode 100644 projects/08/FunctionCalls/SimpleFunction/SimpleFunction.out create mode 100644 projects/08/ProgramFlow/BasicLoop/BasicLoop.asm create mode 100644 projects/08/ProgramFlow/BasicLoop/BasicLoop.out create mode 100644 projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm create mode 100644 projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.out create mode 100755 projects/08/VMTranslator create mode 100644 projects/08/VMTranslator.hi create mode 100644 projects/08/VMTranslator.hs create mode 100644 projects/08/VMTranslator.o diff --git a/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.asm new file mode 100644 index 0000000..7fe71a5 --- /dev/null +++ b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.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/FunctionCalls/SimpleFunction/SimpleFunction.out b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.out new file mode 100644 index 0000000..ef72b7a --- /dev/null +++ b/projects/08/FunctionCalls/SimpleFunction/SimpleFunction.out @@ -0,0 +1,2 @@ +| RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] |RAM[310]| +| 311 | 305 | 300 | 3010 | 4010 | 1196 | diff --git a/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm b/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm new file mode 100644 index 0000000..62aa0cf --- /dev/null +++ b/projects/08/ProgramFlow/BasicLoop/BasicLoop.asm @@ -0,0 +1,140 @@ +@256 +D=A +@SP +M=D +@0 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@LCL +D=M +@0 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +(LOOP_START) +@ARG +D=M +@0 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +@LCL +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 +@LCL +D=M +@0 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +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 +@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 +@LOOP_START +D;JNE +@LCL +D=M +@0 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/08/ProgramFlow/BasicLoop/BasicLoop.out b/projects/08/ProgramFlow/BasicLoop/BasicLoop.out new file mode 100644 index 0000000..1786c7c --- /dev/null +++ b/projects/08/ProgramFlow/BasicLoop/BasicLoop.out @@ -0,0 +1,2 @@ +| RAM[0] |RAM[256]| +| 257 | 6 | diff --git a/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm new file mode 100644 index 0000000..68a4c8e --- /dev/null +++ b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.asm @@ -0,0 +1,241 @@ +@256 +D=A +@SP +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 +@4 +M=D +@0 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THAT +D=M +@0 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@1 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THAT +D=M +@1 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@ARG +D=M +@0 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +@2 +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) +@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 +@COMPUTE_ELEMENT +D;JNE +@END_PROGRAM +0;JMP +(COMPUTE_ELEMENT) +@THAT +D=M +@0 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +@THAT +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 +@THAT +D=M +@2 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@4 +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 +@SP +AM=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/ProgramFlow/FibonacciSeries/FibonacciSeries.out b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.out new file mode 100644 index 0000000..304e7c1 --- /dev/null +++ b/projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.out @@ -0,0 +1,2 @@ +|RAM[3000]|RAM[3001]|RAM[3002]|RAM[3003]|RAM[3004]|RAM[3005]| +| 0 | 1 | 1 | 2 | 3 | 5 | diff --git a/projects/08/VMTranslator b/projects/08/VMTranslator new file mode 100755 index 0000000..8764eb0 Binary files /dev/null and b/projects/08/VMTranslator differ diff --git a/projects/08/VMTranslator.hi b/projects/08/VMTranslator.hi new file mode 100644 index 0000000..078767f Binary files /dev/null and b/projects/08/VMTranslator.hi differ diff --git a/projects/08/VMTranslator.hs b/projects/08/VMTranslator.hs new file mode 100644 index 0000000..099ba75 --- /dev/null +++ b/projects/08/VMTranslator.hs @@ -0,0 +1,107 @@ +import Data.Char (toUpper) +import Data.List.Split (splitOn) +import System.Environment (getArgs) +import Data.Maybe (fromJust) +import Data.List (elemIndex) + +--preamble = "@256\nD=A\n@SP\nM=D\n" +preamble = "" + +epilogue = "(END)\n@END\n0;JMP" + +parse' :: [Char] -> [[Char]] -> Int -> [Char] -> [Char] + +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 _ + | 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' 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"] = + 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 = + 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' "goto" [x] _ _ = "@" ++ x ++ "\n0;JMP\n" + +parse' "if-goto" [x] _ _ = pop "M" ++ "@" ++ x ++ "\nD;JNE\n" + +parse' "function" [f, n] _ _ = "(" ++ f ++ ")\n" ++ (mconcat $ replicate (read n) $ push "0") + +getAddr seg x = "@" ++ seg2Lab seg ++ "\nD=M\n@" ++ x ++ "\nD=A+D\n" + +getAddr' seg x = "@" ++ show (read x + (if seg == "pointer" then 3 else 5)) ++ "\n" + +getAddr'' x filename = "@" ++ filename ++ "." ++ x ++ "\n" + +seg2Lab seg = case seg of + "local" -> "LCL" + "argument" -> "ARG" + "this" -> "THIS" + "that" -> "THAT" + +push :: [Char] -> [Char] +push xs = "@SP\nA=M\nM=" ++ xs ++ "\n@SP\nM=M+1\n" + +ifThenElse :: [Char] -> Int -> [Char] +ifThenElse cond n = "@" ++ cond' ++ show n ++ "\nD;J" ++ cond' ++ "\n" ++ push "0" + ++ "@ENDIF" ++ cond' ++ show n ++ "\n0;JMP\n(" ++ cond' ++ show n ++ ")\n" + ++ push "-1" ++ "(ENDIF" ++ cond' ++ show n ++ ")\n" + where cond' = toUpper <$> cond + +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 + where cmd:target = words line + +stripJunk :: [Char] -> [[Char]] +stripJunk = filter (not . isEmptyLine) . fmap (head . splitOn "//") . lines . replCrWithNl + +isEmptyLine :: [Char] -> Bool +isEmptyLine = null . filter (not . flip elem " \t") + +parseCode :: [Char] -> [Char] -> [Char] +parseCode xs filename = preamble ++ (parseline (stripJunk xs) 0 "" filename) ++ epilogue + +replCrWithNl :: [Char] -> [Char] +replCrWithNl = fmap cr2nl + where cr2nl '\r' = '\n' + cr2nl c = c + +lastSplit c xs = (take (prefix - 1) xs, drop prefix xs) + where prefix = length xs - (fromJust . elemIndex c . reverse) xs + +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) diff --git a/projects/08/VMTranslator.o b/projects/08/VMTranslator.o new file mode 100644 index 0000000..134f4c7 Binary files /dev/null and b/projects/08/VMTranslator.o differ -- cgit v1.2.3