Implement branching

This commit is contained in:
Hazem Krimi
2024-05-02 21:26:14 +01:00
parent 8262f49d85
commit 2040a363e3
5 changed files with 97 additions and 14 deletions
+39
View File
@@ -0,0 +1,39 @@
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string translateLabel(string label)
{
stringstream output;
output << "(" << label << ")" << endl;
return output.str();
}
string translateGoto(string label)
{
stringstream output;
output << "@" << label << endl;
output << "0;JMP" << endl;
return output.str();
}
string translateIfGoto(string label)
{
stringstream output;
output << "@SP" << endl;
output << "M=M-1" << endl;
output << "A=M" << endl;
output << "D=M" << endl;
output << "@" << label << endl;
output << "D;JNE" << endl;
return output.str();
}