From 3fdf4c12819b0bedce83e06cb94fd59f4d6b5e3a Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sun, 1 Nov 2020 19:39:27 -0300 Subject: [PATCH] Fix several bugs --- templates.h | 28 ++++++---------------------- translator.c | 7 +++++-- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/templates.h b/templates.h index 1236346..5c9aa26 100644 --- a/templates.h +++ b/templates.h @@ -63,17 +63,6 @@ char* tpop[TPOPN] = { #define TPOPSTATN 7 char* tpopstat[TPOPSTATN] = { - "", - "@SP", - "M=M-1", - "A=M+1", - "D=M", - "", - "" -}; - -#define TPOPTEMPN 7 -char* tpoptemp[TPOPTEMPN] = { "", "@SP", "M=M-1", @@ -83,16 +72,11 @@ char* tpoptemp[TPOPTEMPN] = { "" }; -#define TPOPPOINTERN 7 -char* tpoppointer[TPOPPOINTERN] = { - "", - "@SP", - "M=M-1", - "A=M", - "D=M", - "", - "M=D" -}; +#define TPOPTEMPN TPOPSTATN +char** tpoptemp = tpopstat; + +#define TPOPPOINTERN TPOPSTATN +char** tpoppointer = tpopstat; #define TARITHN 7 char* tarith[TARITHN] = { @@ -131,7 +115,7 @@ char* tcomp[TCOMPN] = { "D=M", "A=A-1", "D=D-M", - "M=1", + "M=-1", "", "", "@SP", diff --git a/translator.c b/translator.c index f61da68..aba4569 100644 --- a/translator.c +++ b/translator.c @@ -297,6 +297,9 @@ void poppointer(struct Translator* t, struct line* ln, int indlen) { // @THIS/@THAT tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen); + // M=D + tpoppointer[TPOPPOINTERN-1] = heapstr(t, "M=D"); + addasmlns(t, ln, tpoppointer, TPOPPOINTERN); } @@ -380,9 +383,9 @@ void switchop(struct Translator* t, struct line* ln) { else if(!strcmp(op, "eq")) comp(t, ln, "EQ"); else if(!strcmp(op, "gt")) - comp(t, ln, "GT"); - else if(!strcmp(op, "lt")) comp(t, ln, "LT"); + else if(!strcmp(op, "lt")) + comp(t, ln, "GT"); else if(!strcmp(op, "and")) arith(t, ln, "M=M&D"); else if(!strcmp(op, "or"))