Implement assembler directives .byte, .2byte, .4byte and .half
MODIFY
src/assembler.cpp
src/assembler.cpp
@@ -98,6 +98,16 @@ const QStringList branchInstructions = QStringList() << "beq"
<< "bge"
<< "bltu"
<< "bgeu";
+const QStringList DataAssemblerDirectives = QStringList() << ".word"
+ << ".half"
+ << ".byte"
+ << ".2byte"
+ << ".4byte";
+const static QMap<QString, size_t> DataAssemblerSizes{{".word",4},
+ {".half",2},
+ {".byte",1},
+ {".2byte",2},
+ {".4byte",4}};
} // namespace
Assembler::Assembler() {}
@@ -588,6 +598,18 @@ void Assembler::unpackPseudoOp(const QStringList& fields, int& pos) {
}
}
+void Assembler::assembleWords(const QStringList& fields, QByteArray& byteArray, size_t size){
+ Q_ASSERT(size >= 1 && size <= 4);
+ bool ok;
+ for(int i = 1; i < fields.size(); i++){
+ qlonglong val = getImmediate(fields[i], ok);
+ for (size_t i = 0; i < size; i++) {
+ byteArray.append(val & 0xff);
+ val >>= 8;
+ }
+ }
+}
+
void Assembler::assembleAssemblerDirective(const QStringList& fields) {
QByteArray byteArray;
if (fields[0] == QString(".string")) {
@@ -598,15 +620,8 @@ void Assembler::assembleAssemblerDirective(const QStringList& fields) {
}
string.remove('\"');
byteArray = string.toUtf8();
- } else if (fields[0] == QString(".word")) {
- bool ok;
- for(int i = 1; i < fields.size(); i++){
- qlonglong val = getImmediate(fields[i], ok);
- for (int i = 0; i < 4; i++) {
- byteArray.append(val & 0xff);
- val >>= 8;
- }
- }
+ } else if (DataAssemblerDirectives.contains(fields[0])) {
+ assembleWords(fields,byteArray, DataAssemblerSizes.value(fields[0]));
} else if (fields[0] == QString(".data")) {
// Following instructions will be assembled into the data segment
m_inDataSegment = true;

MODIFY
src/assembler.h
src/assembler.h
@@ -19,6 +19,7 @@ private:
void unpackPseudoOp(const QStringList& fields, int& pos);
void unpackOp(const QStringList& fields, int& pos);
void assembleAssemblerDirective(const QStringList& fields);
+ void assembleWords(const QStringList& fields, QByteArray& byteArr, size_t size);
void restart();
int getImmediate(QString string, bool& canConvert);
QByteArray uintToByteArr(uint32_t);

Implement assembler directives .byte, .2byte, .4byte and .half