From 381e730b9b4f07eec97865ed94cff4c189c39ad1 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Tue, 2 Jan 2018 15:49:12 +0100 Subject: finished first part of project 7 --- .../07/StackArithmetic/SimpleAdd/SimpleAdd.asm | 32 ++ .../07/StackArithmetic/SimpleAdd/SimpleAdd.out | 2 + .../07/StackArithmetic/StackTest/StackTest.asm | 426 +++++++++++++++++++++ .../07/StackArithmetic/StackTest/StackTest.out | 4 + projects/07/VMTranslator | Bin 0 -> 46072 bytes projects/07/VMTranslator.hi | Bin 0 -> 1515 bytes projects/07/VMTranslator.hs | 57 +++ projects/07/VMTranslator.o | Bin 0 -> 45304 bytes tools/TextComparer.sh | 0 9 files changed, 521 insertions(+) create mode 100644 projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm create mode 100644 projects/07/StackArithmetic/SimpleAdd/SimpleAdd.out create mode 100644 projects/07/StackArithmetic/StackTest/StackTest.asm create mode 100644 projects/07/StackArithmetic/StackTest/StackTest.out create mode 100755 projects/07/VMTranslator create mode 100644 projects/07/VMTranslator.hi create mode 100644 projects/07/VMTranslator.hs create mode 100644 projects/07/VMTranslator.o mode change 100644 => 100755 tools/TextComparer.sh diff --git a/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm b/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm new file mode 100644 index 0000000..faa8af7 --- /dev/null +++ b/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.asm @@ -0,0 +1,32 @@ +@256 +D=A +@SP +M=D +@7 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@8 +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 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.out b/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.out new file mode 100644 index 0000000..6b3b20e --- /dev/null +++ b/projects/07/StackArithmetic/SimpleAdd/SimpleAdd.out @@ -0,0 +1,2 @@ +| RAM[0] | RAM[256] | +| 257 | 15 | diff --git a/projects/07/StackArithmetic/StackTest/StackTest.asm b/projects/07/StackArithmetic/StackTest/StackTest.asm new file mode 100644 index 0000000..560ec6c --- /dev/null +++ b/projects/07/StackArithmetic/StackTest/StackTest.asm @@ -0,0 +1,426 @@ +@256 +D=A +@SP +M=D +@17 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@17 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@EQ2 +D;JEQ +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFEQ2 +0;JMP +(EQ2) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFEQ2) +@17 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@16 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@EQ5 +D;JEQ +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFEQ5 +0;JMP +(EQ5) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFEQ5) +@16 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@17 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@EQ8 +D;JEQ +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFEQ8 +0;JMP +(EQ8) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFEQ8) +@892 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@891 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@LT11 +D;JLT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFLT11 +0;JMP +(LT11) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFLT11) +@891 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@892 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@LT14 +D;JLT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFLT14 +0;JMP +(LT14) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFLT14) +@891 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@891 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@LT17 +D;JLT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFLT17 +0;JMP +(LT17) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFLT17) +@32767 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@32766 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@GT20 +D;JGT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFGT20 +0;JMP +(GT20) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFGT20) +@32766 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@32767 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@GT23 +D;JGT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFGT23 +0;JMP +(GT23) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFGT23) +@32766 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@32766 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@SP +AM=M-1 +D=M +@SP +AM=M-1 +D=M-D +@GT26 +D;JGT +@SP +A=M +M=0 +@SP +M=M+1 +@ENDIFGT26 +0;JMP +(GT26) +@SP +A=M +M=-1 +@SP +M=M+1 +(ENDIFGT26) +@57 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@31 +D=A +@SP +A=M +M=D +@SP +M=M+1 +@53 +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 +@112 +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 +@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 +@82 +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 +@SP +A=M +M=!D +@SP +M=M+1 +(END) +@END +0;JMP \ No newline at end of file diff --git a/projects/07/StackArithmetic/StackTest/StackTest.out b/projects/07/StackArithmetic/StackTest/StackTest.out new file mode 100644 index 0000000..cb182ec --- /dev/null +++ b/projects/07/StackArithmetic/StackTest/StackTest.out @@ -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 | diff --git a/projects/07/VMTranslator b/projects/07/VMTranslator new file mode 100755 index 0000000..e6e3fc2 Binary files /dev/null and b/projects/07/VMTranslator differ diff --git a/projects/07/VMTranslator.hi b/projects/07/VMTranslator.hi new file mode 100644 index 0000000..5eb5c6a Binary files /dev/null and b/projects/07/VMTranslator.hi differ diff --git a/projects/07/VMTranslator.hs b/projects/07/VMTranslator.hs new file mode 100644 index 0000000..289c0d4 --- /dev/null +++ b/projects/07/VMTranslator.hs @@ -0,0 +1,57 @@ +import Data.Char (toUpper) +import Data.List.Split (splitOn) +import System.Environment (getArgs) + +preamble = "@256\nD=A\n@SP\nM=D\n" + +epilogue = "(END)\n@END\n0;JMP" + +parse' :: [Char] -> [[Char]] -> Int -> [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" + +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] +parseline [] _ acc = acc +parseline (line:lines) n acc = parseline lines (n + 1) (acc ++ parse' cmd target n) + 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] +parseCode xs = preamble ++ (parseline (stripJunk xs) 0 "") ++ epilogue + +replCrWithNl :: [Char] -> [Char] +replCrWithNl = fmap cr2nl + where cr2nl '\r' = '\n' + cr2nl c = c + +--input = "push constant 17\npush constant 17\neq" + +main = do + args <- getArgs + let filename = head args + code <- readFile $ filename + writeFile (head (splitOn "." filename) ++ ".asm") (parseCode code) + --} diff --git a/projects/07/VMTranslator.o b/projects/07/VMTranslator.o new file mode 100644 index 0000000..049a123 Binary files /dev/null and b/projects/07/VMTranslator.o differ diff --git a/tools/TextComparer.sh b/tools/TextComparer.sh old mode 100644 new mode 100755 -- cgit v1.2.3