chore: move repetitive D register assignment commands into separate functions

This commit is contained in:
2026-04-03 14:41:59 +01:00
parent 9de4219cef
commit 74982ac6e9
+60 -63
View File
@@ -2,6 +2,25 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
static int pushFromDRegisterToStack(std::ostringstream &stream) {
stream << "@SP" << std::endl;
stream << "A=M" << std::endl;
stream << "M=D" << std::endl;
stream << "@SP" << std::endl;
stream << "M=M+1" << std::endl;
return 0;
}
static int popFromStackToDRegister(std::ostringstream &stream) {
stream << "@SP" << std::endl;
stream << "M=M-1" << std::endl;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
return 0;
}
static int translateStackOperation(std::ostringstream &stream, static int translateStackOperation(std::ostringstream &stream,
const std::string programName, Command cmd) { const std::string programName, Command cmd) {
switch (cmd.segmentType) { switch (cmd.segmentType) {
@@ -89,20 +108,16 @@ static int translateStackOperation(std::ostringstream &stream,
return 0; return 0;
} }
int translateArithmeticBinaryOperation(std::ostringstream &stream, static int translateArithmeticBinaryOperation(std::ostringstream &stream,
Command cmd) { Command cmd) {
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@BINARY_ARITHMETIC" << std::endl; stream << "@BINARY_ARITHMETIC" << std::endl;
stream << "M=D" << std::endl; stream << "M=D" << std::endl;
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@BINARY_ARITHMETIC" << std::endl; stream << "@BINARY_ARITHMETIC" << std::endl;
@@ -113,20 +128,13 @@ int translateArithmeticBinaryOperation(std::ostringstream &stream,
} }
stream << "D=M" << std::endl; stream << "D=M" << std::endl;
stream << "@SP" << std::endl;
stream << "A=M" << std::endl;
stream << "M=D" << std::endl;
stream << "@SP" << std::endl;
stream << "M=M+1" << std::endl;
return 0; return pushFromDRegisterToStack(stream);
} }
int translateArithmeticUnaryOperation(std::ostringstream &stream) { static int translateArithmeticUnaryOperation(std::ostringstream &stream) {
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@NEGATE" << std::endl; stream << "@NEGATE" << std::endl;
stream << "M=D" << std::endl; stream << "M=D" << std::endl;
@@ -134,40 +142,37 @@ int translateArithmeticUnaryOperation(std::ostringstream &stream) {
stream << "M=M-D" << std::endl; stream << "M=M-D" << std::endl;
stream << "D=M" << std::endl; stream << "D=M" << std::endl;
stream << "@SP" << std::endl; return pushFromDRegisterToStack(stream);
stream << "A=M" << std::endl;
stream << "M=D" << std::endl;
stream << "@SP" << std::endl;
stream << "M=M+1" << std::endl;
return 0;
} }
int translateEqualityOperation(std::ostringstream &stream, Command cmd) { static int translateEqualityOperation(std::ostringstream &stream, Command cmd) {
static int equalityCheckLabelCounter = 0; static int equalityCheckLabelCounter = 0;
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@EQUALITY_CHECK" << std::endl; stream << "@EQUALITY_CHECK" << std::endl;
stream << "M=D" << std::endl; stream << "M=D" << std::endl;
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@EQUALITY_CHECK" << std::endl; stream << "@EQUALITY_CHECK" << std::endl;
stream << "M=D-M" << std::endl; stream << "M=D-M" << std::endl;
stream << "D=M" << std::endl; stream << "D=M" << std::endl;
stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_TRUE" << std::endl; stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_TRUE"
stream << "D;" << (cmd.commandType == CommandType::GT ? "JGT" : cmd.commandType == CommandType::LT ? "JLT" : "JEQ") << std::endl; << std::endl;
stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_FALSE" << std::endl; stream << "D;"
<< (cmd.commandType == CommandType::GT ? "JGT"
: cmd.commandType == CommandType::LT ? "JLT"
: "JEQ")
<< std::endl;
stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_FALSE"
<< std::endl;
stream << "0;JMP" << std::endl; stream << "0;JMP" << std::endl;
stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_TRUE)" << std::endl; stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_TRUE)"
<< std::endl;
stream << "@SP" << std::endl; stream << "@SP" << std::endl;
stream << "A=M" << std::endl; stream << "A=M" << std::endl;
stream << "M=-1" << std::endl; stream << "M=-1" << std::endl;
@@ -176,7 +181,8 @@ int translateEqualityOperation(std::ostringstream &stream, Command cmd) {
stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << std::endl; stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << std::endl;
stream << "0;JMP" << std::endl; stream << "0;JMP" << std::endl;
stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_FALSE)" << std::endl; stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << "_FALSE)"
<< std::endl;
stream << "@SP" << std::endl; stream << "@SP" << std::endl;
stream << "A=M" << std::endl; stream << "A=M" << std::endl;
stream << "M=0" << std::endl; stream << "M=0" << std::endl;
@@ -185,25 +191,23 @@ int translateEqualityOperation(std::ostringstream &stream, Command cmd) {
stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << std::endl; stream << "@" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << std::endl;
stream << "0;JMP" << std::endl; stream << "0;JMP" << std::endl;
stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << ")" << std::endl; stream << "(" << "EQUALITY_CHECK_" << equalityCheckLabelCounter << ")"
<< std::endl;
equalityCheckLabelCounter++; equalityCheckLabelCounter++;
return 0; return 0;
} }
int translateBitwiseBinaryOperation(std::ostringstream &stream, Command cmd) { static int translateBitwiseBinaryOperation(std::ostringstream &stream,
stream << "@SP" << std::endl; Command cmd) {
stream << "M=M-1" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "A=M" << std::endl; return 1;
stream << "D=M" << std::endl;
stream << "@BINARY_BITWISE" << std::endl; stream << "@BINARY_BITWISE" << std::endl;
stream << "M=D" << std::endl; stream << "M=D" << std::endl;
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@BINARY_BITWISE" << std::endl; stream << "@BINARY_BITWISE" << std::endl;
@@ -214,20 +218,13 @@ int translateBitwiseBinaryOperation(std::ostringstream &stream, Command cmd) {
} }
stream << "D=M" << std::endl; stream << "D=M" << std::endl;
stream << "@SP" << std::endl;
stream << "A=M" << std::endl;
stream << "M=D" << std::endl;
stream << "@SP" << std::endl;
stream << "M=M+1" << std::endl;
return 0; return pushFromDRegisterToStack(stream);
} }
int translateBistwiseUnaryOperation(std::ostringstream &stream) { static int translateBistwiseUnaryOperation(std::ostringstream &stream) {
stream << "@SP" << std::endl; if (popFromStackToDRegister(stream) != 0)
stream << "M=M-1" << std::endl; return 1;
stream << "A=M" << std::endl;
stream << "D=M" << std::endl;
stream << "@SP" << std::endl; stream << "@SP" << std::endl;
stream << "A=M" << std::endl; stream << "A=M" << std::endl;