From 040cf91851036a0216e7b5faea1f74ecd674db50 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 3 Jan 2018 11:49:20 +0100 Subject: Finished Project 7 --- projects/07/MemoryAccess/BasicTest/BasicTest.asm | 250 +++++++++++++++++++++ projects/07/MemoryAccess/BasicTest/BasicTest.out | 2 + projects/07/MemoryAccess/BasicTest/delme.asm | 26 +++ projects/07/MemoryAccess/BasicTest/delme.vm | 8 + .../07/MemoryAccess/PointerTest/PointerTest.asm | 138 ++++++++++++ .../07/MemoryAccess/PointerTest/PointerTest.out | 2 + projects/07/MemoryAccess/StaticTest/StaticTest.asm | 86 +++++++ projects/07/MemoryAccess/StaticTest/StaticTest.out | 2 + projects/07/VMTranslator | Bin 46072 -> 65152 bytes projects/07/VMTranslator.hi | Bin 1515 -> 1693 bytes projects/07/VMTranslator.hs | 61 ++--- projects/07/VMTranslator.o | Bin 45304 -> 77480 bytes 12 files changed, 547 insertions(+), 28 deletions(-) create mode 100644 projects/07/MemoryAccess/BasicTest/BasicTest.asm create mode 100644 projects/07/MemoryAccess/BasicTest/BasicTest.out create mode 100644 projects/07/MemoryAccess/BasicTest/delme.asm create mode 100644 projects/07/MemoryAccess/BasicTest/delme.vm create mode 100644 projects/07/MemoryAccess/PointerTest/PointerTest.asm create mode 100644 projects/07/MemoryAccess/PointerTest/PointerTest.out create mode 100644 projects/07/MemoryAccess/StaticTest/StaticTest.asm create mode 100644 projects/07/MemoryAccess/StaticTest/StaticTest.out (limited to 'projects/07') diff --git a/projects/07/MemoryAccess/BasicTest/BasicTest.asm b/projects/07/MemoryAccess/BasicTest/BasicTest.asm new file mode 100644 index 0000000..d978f07 --- /dev/null +++ b/projects/07/MemoryAccess/BasicTest/BasicTest.asm @@ -0,0 +1,250 @@ +@256 +D=A +@SP +M=D +@10 +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 +@21 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@22 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@ARG +D=M +@2 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@ARG +D=M +@1 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@36 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THIS +D=M +@6 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@42 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@45 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THAT +D=M +@5 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@THAT +D=M +@2 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@510 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@11 +M=D +@LCL +D=M +@0 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +@THAT +D=M +@5 +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 +@THIS +D=M +@6 +D=A+D +A=D +D=M +@SP +A=M +M=D +@SP +M=M+1 +@THIS +D=M +@6 +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 +AM=M-1 +D=M-D +@SP +A=M +M=D +@SP +M=M+1 +@11 +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 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/MemoryAccess/BasicTest/BasicTest.out b/projects/07/MemoryAccess/BasicTest/BasicTest.out new file mode 100644 index 0000000..85c19a7 --- /dev/null +++ b/projects/07/MemoryAccess/BasicTest/BasicTest.out @@ -0,0 +1,2 @@ +|RAM[256]|RAM[300]|RAM[401]|RAM[402]|RAM[3006|RAM[3012|RAM[3015|RAM[11] | +| 472 | 10 | 21 | 22 | 36 | 42 | 45 | 510 | diff --git a/projects/07/MemoryAccess/BasicTest/delme.asm b/projects/07/MemoryAccess/BasicTest/delme.asm new file mode 100644 index 0000000..6392181 --- /dev/null +++ b/projects/07/MemoryAccess/BasicTest/delme.asm @@ -0,0 +1,26 @@ +@256 +D=A +@SP +M=D +@10 +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 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/MemoryAccess/BasicTest/delme.vm b/projects/07/MemoryAccess/BasicTest/delme.vm new file mode 100644 index 0000000..d1ae195 --- /dev/null +++ b/projects/07/MemoryAccess/BasicTest/delme.vm @@ -0,0 +1,8 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/07/MemoryAccess/BasicTest/BasicTest.vm + +// Executes pop and push commands using the virtual memory segments. +push constant 10 +pop local 0 diff --git a/projects/07/MemoryAccess/PointerTest/PointerTest.asm b/projects/07/MemoryAccess/PointerTest/PointerTest.asm new file mode 100644 index 0000000..67125df --- /dev/null +++ b/projects/07/MemoryAccess/PointerTest/PointerTest.asm @@ -0,0 +1,138 @@ +@256 +D=A +@SP +M=D +@3030 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@3 +M=D +@3040 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@4 +M=D +@32 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THIS +D=M +@2 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@46 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@THAT +D=M +@6 +D=A+D +@R13 +M=D +@SP +AM=M-1 +D=M +@R13 +A=M +M=D +@3 +D=M +@SP +A=M +M=D +@SP +M=M+1 +@4 +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 +@THIS +D=M +@2 +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 +@6 +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 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/MemoryAccess/PointerTest/PointerTest.out b/projects/07/MemoryAccess/PointerTest/PointerTest.out new file mode 100644 index 0000000..5d62de8 --- /dev/null +++ b/projects/07/MemoryAccess/PointerTest/PointerTest.out @@ -0,0 +1,2 @@ +|RAM[256]| RAM[3] | RAM[4] |RAM[3032|RAM[3046| +| 6084 | 3030 | 3040 | 32 | 46 | diff --git a/projects/07/MemoryAccess/StaticTest/StaticTest.asm b/projects/07/MemoryAccess/StaticTest/StaticTest.asm new file mode 100644 index 0000000..8cd989d --- /dev/null +++ b/projects/07/MemoryAccess/StaticTest/StaticTest.asm @@ -0,0 +1,86 @@ +@256 +D=A +@SP +M=D +@111 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@333 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@888 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@StaticTest.8 +M=D +@SP +AM=M-1 +D=M +@StaticTest.3 +M=D +@SP +AM=M-1 +D=M +@StaticTest.1 +M=D +@StaticTest.3 +D=M +@SP +A=M +M=D +@SP +M=M+1 +@StaticTest.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 +@StaticTest.8 +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 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/MemoryAccess/StaticTest/StaticTest.out b/projects/07/MemoryAccess/StaticTest/StaticTest.out new file mode 100644 index 0000000..2bc908b --- /dev/null +++ b/projects/07/MemoryAccess/StaticTest/StaticTest.out @@ -0,0 +1,2 @@ +|RAM[256]| +| 1110 | diff --git a/projects/07/VMTranslator b/projects/07/VMTranslator index e6e3fc2..65a46dc 100755 Binary files a/projects/07/VMTranslator and b/projects/07/VMTranslator differ diff --git a/projects/07/VMTranslator.hi b/projects/07/VMTranslator.hi index 5eb5c6a..078767f 100644 Binary files a/projects/07/VMTranslator.hi and b/projects/07/VMTranslator.hi differ diff --git a/projects/07/VMTranslator.hs b/projects/07/VMTranslator.hs index a1f93e6..ce8447d 100644 --- a/projects/07/VMTranslator.hs +++ b/projects/07/VMTranslator.hs @@ -1,46 +1,49 @@ 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" epilogue = "(END)\n@END\n0;JMP" -parse' :: [Char] -> [[Char]] -> Int -> [Char] -parse' op [] n +parse' :: [Char] -> [[Char]] -> Int -> [Char] -> [Char] +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"] = +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 ++ push "D" - | "pop" -> getAddr seg x ++ "@R13\nM=D\n" ++ pop "M" ++ "@R13\nA=M\nM=D\n" + "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" + "push" -> getAddr' seg x ++ "D=M\n" ++ push "D" + "pop" -> pop "M" ++ getAddr' seg x ++ "M=D\n" -parse' cmd ["static", x] _ = +parse' cmd ["static", x] _ filename = case cmd of - | "push" -> getAddr'' x ++ "D=M\n" ++ push "D" - | "pop" -> pop "M" ++ getAddr'' x ++ "M=D\n" + "push" -> getAddr'' x filename ++ "D=M\n" ++ push "D" + "pop" -> pop "M" ++ getAddr'' x filename ++ "M=D\n" -getAddr seg x = "@" ++ seg2Lab seg ++ "\nD=M\n@" ++ x ++ "\nA=D+M\nD=M\n" +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 ++ "." ++ x ++ "\n" +getAddr'' x filename = "@" ++ filename ++ "." ++ x ++ "\n" seg2Lab seg = case seg of - | "local" -> "LCL" - | "argument" -> "ARG" - | "this" -> "THIS" - | "that" -> "THAT" + "local" -> "LCL" + "argument" -> "ARG" + "this" -> "THIS" + "that" -> "THAT" push :: [Char] -> [Char] push xs = "@SP\nA=M\nM=" ++ xs ++ "\n@SP\nM=M+1\n" @@ -54,9 +57,9 @@ 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] -parseline [] _ acc = acc -parseline (line:lines) n acc = parseline lines (n + 1) (acc ++ parse' cmd target 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]] @@ -65,19 +68,21 @@ stripJunk = filter (not . isEmptyLine) . fmap (head . splitOn "//") . lines . re isEmptyLine :: [Char] -> Bool isEmptyLine = null . filter (not . flip elem " \t") -parseCode :: [Char] -> [Char] -parseCode xs = preamble ++ (parseline (stripJunk xs) 0 "") ++ epilogue +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 ---input = "push constant 17\npush constant 17\neq" +lastSplit c xs = (take (prefix - 1) xs, drop prefix xs) + where prefix = length xs - (fromJust . elemIndex c . reverse) xs main = do args <- getArgs - let filename = head args - code <- readFile $ filename - writeFile (head (splitOn "." filename) ++ ".asm") (parseCode code) - --} + 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/07/VMTranslator.o b/projects/07/VMTranslator.o index 049a123..f8e3523 100644 Binary files a/projects/07/VMTranslator.o and b/projects/07/VMTranslator.o differ -- cgit v1.2.3