From adec9f83f875da3586eadd0d8555a819801b7a43 Mon Sep 17 00:00:00 2001 From: Vipin Kumar Date: Fri, 25 Aug 2017 14:24:24 +0000 Subject: [PATCH] Implement writeIf() in CodeWriter.java --- CodeWriter.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CodeWriter.java b/CodeWriter.java index 33051da..c291fd7 100644 --- a/CodeWriter.java +++ b/CodeWriter.java @@ -401,6 +401,21 @@ public void writeGoto(String label) { // Write Assembly code for the if-goto command public void writeIf(String label) { try { + // pop top-most value from stack + out.write("@SP"); + out.newLine(); + out.write("M = M - 1"); + out.newLine(); + out.write("A = M"); + out.newLine(); + out.write("D = M"); + out.newLine(); + + // jump to the label, if the value is not equal to 0 + out.write("@" + label); + out.newLine(); + out.write("D;JNE"); + out.newLine(); } catch (IOException e) {