From 32147b215e5066545e4f971384f6f9c2865354fd Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 15 Apr 2018 10:20:11 +0200 Subject: Better generated code --- Compiler.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Compiler.hs b/Compiler.hs index f02a50a..23ce5d3 100644 --- a/Compiler.hs +++ b/Compiler.hs @@ -64,12 +64,18 @@ compileIns (ICopy _ _ 0) = return () compileIns (ICopy from to m) = do emit $ "mov al, [" ++ cursorOffset from ++ "]" case m of - 1 -> return () - _ | Just p <- isTwoPower m -> emit $ "shl al, " ++ show p + 1 -> emit $ "add [" ++ cursorOffset to ++ "], al" + -1 -> emit $ "sub [" ++ cursorOffset to ++ "], al" + _ | Just p <- isTwoPower m -> do + emit $ "shl al, " ++ show p + emit $ "add [" ++ cursorOffset to ++ "], al" + | Just p <- isTwoPower (-m) -> do + emit $ "shl al, " ++ show p + emit $ "sub [" ++ cursorOffset to ++ "], al" | otherwise -> do - emit $ "mov cl, " ++ show m - emit "mul byte cl" - emit $ "add [" ++ cursorOffset to ++ "], al" + emit $ "mov cl, " ++ show m + emit "mul byte cl" + emit $ "add [" ++ cursorOffset to ++ "], al" compileIns (ISlide off) = emit $ "add " ++ cursorReg ++ ", " ++ show off compileIns (ILoop inss off) = do loopid <- genId @@ -84,8 +90,7 @@ compileIns (IInput off) = do emit $ generateCall "getchar" emit $ "mov [" ++ cursorOffset off ++ "], al" compileIns (IOutput off) = do - emit "xor edi, edi" - emit $ "mov dil, [" ++ cursorOffset off ++ "]" + emit $ "movzx edi, byte [" ++ cursorOffset off ++ "]" emit $ generateCall "putchar" compileIns IStart = return () -- cgit v1.2.3