diff --git a/TACx86Pkg/x86Templates.java b/TACx86Pkg/x86Templates.java index ebd338a..d164aae 100644 --- a/TACx86Pkg/x86Templates.java +++ b/TACx86Pkg/x86Templates.java @@ -143,4 +143,25 @@ public static String writeInteger(String var1){ return writeIntegerTemplate; } + + public static String negTemplate(String var1, String var2) { + String negTemplate = ""; + if(var2.charAt(0) == '@'){ + negTemplate = negTemplate.concat("\t\tmov bx,").concat(var2.substring(1, var2.length())).concat("\n"); + negTemplate = negTemplate.concat("\t\tmov ax, [bx]").concat("\n"); + negTemplate = negTemplate.concat("\t\tneg ax").concat(var2).concat("\n"); + } else { + negTemplate = negTemplate.concat("\t\tmov ax, ").concat(var2.substring(1, var2.length())).concat("\n"); + negTemplate = negTemplate.concat("\t\tneg ax").concat("\n"); + } + + if(var1.charAt(0) == '@'){ + negTemplate = negTemplate.concat("\t\tmov bx, ").concat(var1.substring(1, var1.length())).concat("\n"); + negTemplate = negTemplate.concat("\t\tmov [bx], ax"); + } else { + negTemplate = negTemplate.concat("\t\tmov ").concat(var1).concat(", ax").concat("\n"); + } + + return negTemplate; + } } diff --git a/TACx86Pkg/x86Translator.java b/TACx86Pkg/x86Translator.java index 54c8fb0..723b541 100644 --- a/TACx86Pkg/x86Translator.java +++ b/TACx86Pkg/x86Translator.java @@ -123,18 +123,22 @@ else if(statement.length() > 38){ if(operator.equals("+")){ asmWriter.println(x86Templates.additionTemplate(var1,var2,var3)); } else if(operator.equals("-")){ - + //todo subtraction } else if(operator.equals("*")){ asmWriter.println(x86Templates.multiplicationTemplate(var1,var2,var3)); } else if(operator.equals("/")){ - + // todo division } continue; } // Assignment statement #2 : x = op y - else if(statement.length() > 22 && (statement.charAt(16) == '-' || statement.charAt(16) == '+')){ + else if(statement.length() > 22 && (statement.charAt(16) == '-')){ + String var1 = fixBP(firstToken); + tokenizer.nextToken(); + String var2 = fixBP(tokenizer.nextToken()); + asmWriter.println(x86Templates.negTemplate(var1, var2)); continue; } @@ -151,6 +155,8 @@ else if(statement.length() > 22 && (statement.charAt(16) == '-' || statement.cha } } while (statement != null); + isSuccessfullyTranslated = true; + asmWriter.close(); }