From ae3274304fabe408b93682ea0483b3969980b91f Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 23 Apr 2024 20:22:41 +0100 Subject: [PATCH] Finish first part spec --- src/include/code.h | 3 +- src/include/operations.h | 86 +++++++++++++++++++++++++++++----------- src/include/utils.h | 19 +++++++++ 3 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 src/include/utils.h diff --git a/src/include/code.h b/src/include/code.h index a62effd..8bb605d 100644 --- a/src/include/code.h +++ b/src/include/code.h @@ -2,6 +2,7 @@ #include #include #include "types.h" +#include "utils.h" #include "operations.h" #include "memory.h" @@ -80,8 +81,6 @@ public: break; } } - - file << endl; } } diff --git a/src/include/operations.h b/src/include/operations.h index efbd396..8b54abb 100644 --- a/src/include/operations.h +++ b/src/include/operations.h @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; @@ -116,6 +117,10 @@ string translateEq() { stringstream output; + srand(static_cast(time(nullptr))); + + string LABEL = generateRandomLabel(); + output << "@SP" << endl; output << "M=M-1" << endl; output << "A=M" << endl; @@ -130,25 +135,32 @@ string translateEq() output << "D=M" << endl; output << "@R" << endl; - output << "M=M-D" << endl; - output << "@TRUE" << endl; - output << "M;JEQ" << endl; - output << "@FALSE" << endl; - output << "M;JNE" << endl; + output << "M=D-M" << endl; + output << "D=M" << endl; + output << "@" << LABEL << "_TRUE" << endl; + output << "D;JEQ" << endl; + output << "@" << LABEL << "_FALSE" << endl; + output << "D;JNE" << endl; - output << "(TRUE)" << endl; + output << "(" << LABEL << "_TRUE)" << endl; output << "@SP" << endl; output << "A=M" << endl; output << "M=-1" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; - output << "(FALSE)" << endl; + output << "(" << LABEL << "_FALSE)" << endl; output << "@SP" << endl; output << "A=M" << endl; - output << "M=-1" << endl; + output << "M=0" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; + + output << "(" << LABEL << ")" << endl; return output.str(); } @@ -157,6 +169,10 @@ string translateGt() { stringstream output; + srand(static_cast(time(nullptr))); + + string LABEL = generateRandomLabel(); + output << "@SP" << endl; output << "M=M-1" << endl; output << "A=M" << endl; @@ -171,25 +187,34 @@ string translateGt() output << "D=M" << endl; output << "@R" << endl; - output << "M=M-D" << endl; - output << "@TRUE" << endl; - output << "M;JGT" << endl; - output << "@FALSE" << endl; - output << "M;JLT" << endl; + output << "M=D-M" << endl; + output << "D=M" << endl; + output << "@" << LABEL << "_TRUE" << endl; + output << "D;JGT" << endl; + output << "@" << LABEL << "_FALSE" << endl; + output << "D;JLT" << endl; + output << "@" << LABEL << "_FALSE" << endl; + output << "D;JEQ" << endl; - output << "(TRUE)" << endl; + output << "(" << LABEL << "_TRUE)" << endl; output << "@SP" << endl; output << "A=M" << endl; output << "M=-1" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; - output << "(FALSE)" << endl; + output << "(" << LABEL << "_FALSE)" << endl; output << "@SP" << endl; output << "A=M" << endl; - output << "M=-1" << endl; + output << "M=0" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; + + output << "(" << LABEL << ")" << endl; return output.str(); } @@ -198,6 +223,10 @@ string translateLt() { stringstream output; + srand(static_cast(time(nullptr))); + + string LABEL = generateRandomLabel(); + output << "@SP" << endl; output << "M=M-1" << endl; output << "A=M" << endl; @@ -212,25 +241,34 @@ string translateLt() output << "D=M" << endl; output << "@R" << endl; - output << "M=M-D" << endl; - output << "@TRUE" << endl; - output << "M;JLT" << endl; - output << "@FALSE" << endl; - output << "M;JGT" << endl; + output << "M=D-M" << endl; + output << "D=M" << endl; + output << "@" << LABEL << "_TRUE" << endl; + output << "D;JLT" << endl; + output << "@" << LABEL << "_FALSE" << endl; + output << "D;JGT" << endl; + output << "@" << LABEL << "_FALSE" << endl; + output << "D;JEQ" << endl; - output << "(TRUE)" << endl; + output << "(" << LABEL << "_TRUE)" << endl; output << "@SP" << endl; output << "A=M" << endl; output << "M=-1" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; - output << "(FALSE)" << endl; + output << "(" << LABEL << "_FALSE)" << endl; output << "@SP" << endl; output << "A=M" << endl; - output << "M=-1" << endl; + output << "M=0" << endl; output << "@SP" << endl; output << "M=M+1" << endl; + output << "@" << LABEL << endl; + output << "0;JMP" << endl; + + output << "(" << LABEL << ")" << endl; return output.str(); } diff --git a/src/include/utils.h b/src/include/utils.h new file mode 100644 index 0000000..0de1ae8 --- /dev/null +++ b/src/include/utils.h @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +using namespace std; + +string generateRandomLabel() { + random_device rd; + mt19937 gen(rd()); + uniform_int_distribution<> dis('A', 'Z'); + string label; + + for (int i = 0; i < 8; ++i) { + label += static_cast(dis(gen)); + } + + return label; +} \ No newline at end of file