Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- using namespace std;
- #include <iostream>
- #include <fstream>
- #include <cstdarg>
- #include <string>
- #include <vector>
- #include <map>
- bool isquote(char c)
- {
- return (c == '"') || (c == '\'');
- }
- bool issymbol(char c)
- {
- return (c == '*') || (c == ':') || (c == ',') || (c == '[') || (c == ']');
- }
- std::string format(const char *fmt, ...)
- {
- va_list ap;
- va_start(ap, fmt);
- char tmp[1024];
- vsprintf(tmp, fmt, ap);
- va_end(ap);
- return tmp;
- }
- enum TType
- {
- Undefined = 0,
- SingleChar = 1 << 0,
- String = 1 << 1,
- Binary = 1 << 2,
- Heximal = 1 << 3,
- Decimal = 1 << 4,
- Identifier = 1 << 5,
- DataType = 1 << 6,
- Directive = 1 << 7,
- Register8 = 1 << 8,
- Register16 = 1 << 9,
- Register32 = 1 << 10,
- SRegister = 1 << 11,
- Instruction = 1 << 12,
- PtrType = 1 << 13,
- Operator = 1 << 14
- };
- struct Token
- {
- int index, first, last;
- string text, info;
- TType type;
- Token() : Token("", Undefined, 0, 0, 0, "Undefined") {}
- Token(const string &text, TType type, int index, int first, int last, const string &info) : text(text), type(type), index(index), first(first), info(info) {}
- };
- struct Symbol
- {
- string segment, value, type, text;
- Symbol() {}
- Symbol(const string &segment, const string &value, const string &type)
- {
- this->segment = segment;
- this->value = value;
- this->type = type;
- this->text = "";
- }
- };
- struct Info
- {
- int index, count;
- Info(int index, int count)
- {
- this->index = index;
- this->count = count;
- }
- };
- struct Operand
- {
- enum class Type
- {
- Undef, Reg, Mem, Imm, Text, Name
- } type;
- vector<Token> tokens;
- int first;
- int ptr, scale, imm, disp;
- Token reg, base, index;
- string sreg, name, text;
- bool valid;
- Operand(const int &first, const vector<Token> &tokens) : first(first), type(Type::Undef), valid(true), tokens(tokens) {}
- bool parse()
- {
- ptr = scale = imm = disp = 0;
- reg = base = index = Token();
- int i = 0, len = tokens.size();
- if ((i < len) && (tokens[i].type == PtrType))
- {
- string ptr = tokens[i++].text;
- if ((i < len) && (tokens[i].type == Operator))
- {
- i++;
- if (ptr.compare("BYTE") == 0)
- {
- this->ptr = 1;
- }
- else if (ptr.compare("WORD") == 0)
- {
- this->ptr = 2;
- }
- else if (ptr.compare("DWORD") == 0)
- {
- this->ptr = 4;
- }
- else return false;
- }
- else return valid = false;
- goto is_mem;
- }
- if ((i < len) && ((tokens[i].type == Register8) || (tokens[i].type == Register32)))
- {
- type = Type::Reg;
- this->reg = tokens[i++];
- return valid = i == len;
- }
- else if ((i < len) && (tokens[i].type == Binary))
- {
- type = Type::Imm;
- this->imm = stol(tokens[i++].text, 0, 2);
- return valid = i == len;
- }
- else if ((i < len) && (tokens[i].type == Decimal))
- {
- type = Type::Imm;
- this->imm = stol(tokens[i++].text, 0, 10);
- return valid = i == len;
- }
- else if ((i < len) && (tokens[i].type == Heximal))
- {
- type = Type::Imm;
- this->imm = stol(tokens[i++].text, 0, 16);
- return valid = i == len;
- }
- else if ((i < len) && (tokens[i].type == String))
- {
- type = Type::Text;
- this->text = tokens[i++].text;
- return valid = i == len;
- }
- is_mem:
- if ((i < len) && (tokens[i].type == SRegister))
- {
- const Token &sreg = tokens[i++];
- if ((i < len) && (tokens[i].text.compare(":") == 0))
- {
- i++;
- this->sreg = sreg.text;
- }
- else return valid = i == len;
- }
- if ((i < len) && (tokens[i].type == Identifier))
- {
- type = Type::Name;
- this->name = tokens[i++].text;
- if ((i < len) && (tokens[i].text.compare("[") == 0))
- {
- type = Type::Mem;
- i++;
- if ((i < len) && (tokens[i].type == Register32))
- {
- this->base = tokens[i++];
- if ((i < len) && (tokens[i].text.compare("]"))) i++;
- return i == len;
- }
- else return valid = false;
- }
- }
- else return valid = false;
- }
- bool IsRegister()
- {
- return type == Type::Reg;
- }
- bool IsMemory()
- {
- return type == Type::Mem;
- }
- bool IsImmediate()
- {
- return type == Type::Imm;
- }
- bool IsString()
- {
- return type == Type::Text;
- }
- bool IsName()
- {
- return type == Type::Name;
- }
- };
- struct Sentence
- {
- string prefix, bytes, source;
- bool printable, skip, valid;
- unsigned offset, length;
- Info label, name, mnemo;
- vector<Operand> operands;
- vector<Token> tokens;
- Sentence(const string &source, const vector<Token> tokens) : skip(false), valid(true), source(source), tokens(tokens), label(-1, 0), name(-1, 0), mnemo(-1, 0), printable(false), offset(0)
- {
- length = 0;
- int len = tokens.size(), i = 0;
- if ((i < len) && (tokens[i].type == Identifier))
- {
- int index = i++;
- if ((i < len) && (tokens[i].text.compare(":") == 0))
- {
- this->label.index = index;
- i++;
- }
- else
- {
- this->name.index = index;
- }
- }
- if ((i < len) && ((tokens[i].type == Directive) || (tokens[i].type == DataType) || (tokens[i].type == Instruction)))
- {
- this->mnemo.index = i++;
- }
- while (i < len)
- {
- vector<Token> operand_tokens;
- int index = i;
- while (i < len)
- {
- if (tokens[i].text.compare(",") == 0)
- {
- i++;
- break;
- }
- operand_tokens.push_back(tokens[i++]);
- }
- Operand operand(index, operand_tokens);
- operand.parse();
- operands.push_back(operand);
- }
- while (operands.size() < 2)
- {
- operands.push_back(Operand(-1, {}));
- }
- }
- bool lookup(struct Compiler *);
- void writeSyntax(FILE *);
- void writeOffset(FILE *);
- };
- struct If { bool value; };
- struct Compiler
- {
- map<string, vector<Token>> eques;
- map<string, unsigned> segments;
- map<string, Symbol> symbols;
- vector<Sentence> sentences;
- string filename, listing;
- vector<If> ifTable;
- bool lexicalError;
- unsigned offset;
- int lineNumber;
- string segment;
- Compiler() : lexicalError(false) {}
- static bool IsDirective(const std::string & text);
- static bool IsDataType(const std::string & text);
- static bool IsPtrType(const std::string & text);
- static bool IsPtrOperator(const std::string & text);
- static bool IsRegister8Bit(const std::string & text);
- static bool IsRegister16Bit(const std::string & text);
- static bool IsRegister32Bit(const std::string & text);
- static bool IsSegmentRegister(const std::string & text);
- static bool IsInstruction(const std::string & text);
- vector<Token> tokenize(string &);
- void parse(int argc, char *argv[]);
- void write();
- bool SetEqu(const string &text, const vector<Token> &tokens)
- {
- if (eques.find(text) != eques.end()) return false;
- eques[text] = tokens;
- return true;
- }
- bool AddSymbol(const string &text, const Symbol &symbol)
- {
- if (symbols.find(text) != symbols.end()) return false;
- symbols[text] = symbol;
- return true;
- }
- bool firstSegment(const string &text)
- {
- offset = 0;
- if (!segment.empty()) return false;
- segment = text;
- return true;
- }
- bool EndSegment(const string &text, const int &length)
- {
- if (segment.empty() || (segment.compare(text) != 0)) return false;
- segments[text] = length;
- segment.clear();
- return true;
- }
- };
- bool Compiler::IsDirective(const std::string & text)
- {
- return !text.compare("END")
- || !text.compare("SEGMENT")
- || !text.compare("ENDS")
- || !text.compare("MACRO")
- || !text.compare("ENDM")
- || !text.compare("ELSE")
- || !text.compare("ENDIF");
- }
- bool Compiler::IsDataType(const std::string & text)
- {
- return !text.compare("DB")
- || !text.compare("DW")
- || !text.compare("DD");
- }
- bool Compiler::IsPtrType(const std::string & text)
- {
- return !text.compare("BYTE")
- || !text.compare("WORD")
- || !text.compare("DWORD");
- }
- bool Compiler::IsPtrOperator(const std::string & text)
- {
- return !text.compare("PTR");
- }
- bool Compiler::IsRegister8Bit(const std::string & text)
- {
- return !text.compare("AH")
- || !text.compare("BH")
- || !text.compare("CH")
- || !text.compare("DH")
- || !text.compare("AL")
- || !text.compare("BL")
- || !text.compare("CL")
- || !text.compare("DL");
- }
- bool Compiler::IsRegister16Bit(const std::string & text)
- {
- return !text.compare("AX")
- || !text.compare("BX")
- || !text.compare("CX")
- || !text.compare("DX")
- || !text.compare("SI")
- || !text.compare("DI")
- || !text.compare("SP")
- || !text.compare("BP");
- }
- bool Compiler::IsRegister32Bit(const std::string & text)
- {
- return !text.compare("EAX")
- || !text.compare("EBX")
- || !text.compare("ECX")
- || !text.compare("EDX")
- || !text.compare("ESI")
- || !text.compare("EDI")
- || !text.compare("ESP")
- || !text.compare("EBP");
- }
- bool Compiler::IsSegmentRegister(const std::string & text)
- {
- return !text.compare("CS")
- || !text.compare("DS")
- || !text.compare("SS")
- || !text.compare("ES")
- || !text.compare("ES")
- || !text.compare("FS")
- || !text.compare("GS");
- }
- bool Compiler::IsInstruction(const std::string & text)
- {
- return
- !text.compare("CWDE") ||
- !text.compare("INC") ||
- !text.compare("STOS") ||
- !text.compare("ADD") ||
- !text.compare("CMP") ||
- !text.compare("AND") ||
- !text.compare("MOV") ||
- !text.compare("OR") ||
- !text.compare("JBE");
- }
- vector<Token> Compiler::tokenize(string &source)
- {
- vector<Token> tokens;
- int len = source.size(), index = 0, i = 0;
- while (i < len)
- {
- if (isspace(source[i])) i++;
- else if (isalpha(source[i]))
- {
- Token token;
- token.first = i;
- token.index = index++;
- while ((i < len) && isalnum(source[i]))
- {
- token.text += toupper(source[i++]);
- }
- token.last = i;
- [&token](const string &text)
- {
- if (IsDirective(text))
- {
- token.type = Directive;
- token.info = "Directive";
- }
- else if (IsDataType(text))
- {
- token.type = DataType;
- token.info = "Data type";
- }
- else if (IsPtrType(text))
- {
- token.type = PtrType;
- token.info = "Ptr type";
- }
- else if (IsPtrOperator(text))
- {
- token.type = Operator;
- token.info = "Ptr operator";
- }
- else if (IsRegister8Bit(text))
- {
- token.type = Register8;
- token.info = "8-bit register";
- }
- else if (IsRegister16Bit(text))
- {
- token.type = Register16;
- token.info = "16-bit register";
- }
- else if (IsRegister32Bit(text))
- {
- token.type = Register32;
- token.info = "32-bit register";
- }
- else if (IsSegmentRegister(text))
- {
- token.type = SRegister;
- token.info = "Segment register";
- }
- else if (IsInstruction(text))
- {
- token.type = Instruction;
- token.info = "Instruction";
- }
- else
- {
- token.type = Identifier;
- token.info = "Identifier";
- }
- }(token.text);
- if (token.type == Identifier)
- {
- const auto &equ = eques.find(token.text);
- if (equ != eques.end())
- {
- source = source.replace(token.first, token.text.size(), symbols[token.text].text);
- for (Token token : equ->second)
- {
- token.index = index++;
- tokens.push_back(token);
- }
- }
- else tokens.push_back(token);
- }
- else tokens.push_back(token);
- }
- else if (isdigit(source[i]))
- {
- Token token;
- token.first = i;
- token.index = index++;
- token.type = Decimal;
- while ((i < len) && isalnum(source[i]))
- {
- token.text += source[i++];
- }
- token.last = i;
- char c = token.text.back();
- if (toupper(c) == 'B')
- {
- token.type = Binary;
- token.info = "Binary";
- auto pos = token.text.find_first_not_of("01");
- // lexicalError = (pos != string::npos) && (pos != (token.text.size() - 1));
- }
- else if (toupper(c) == 'H')
- {
- token.type = Heximal;
- token.info = "Heximal";
- auto pos = token.text.find_first_not_of("0123456789ABCDEF");
- // lexicalError = (pos != string::npos) && (pos != (token.text.size() - 1));
- }
- else
- {
- token.type = Decimal;
- token.info = "Decimal";
- auto pos = token.text.find_first_not_of("0123456789");
- // lexicalError = (pos != string::npos) && (pos != (token.text.size() - 1));
- }
- tokens.push_back(token);
- }
- else if ((source[i] == '"') || (source[i] == '\''))
- {
- char quote = source[i++];
- int first = i;
- string text;
- while ((i < len) && (source[i] != '\n') && (source[i] != quote))
- {
- text += source[i++];
- }
- int last = i++;
- tokens.push_back(Token(text, String, index++, first, last, "String"));
- // lexicalError = (source[i++] != quote);
- }
- else
- {
- int first = i;
- bool symbol = (source[i] == '[') || (source[i] == ']') || (source[i] == ':') || (source[i] == ',');
- // lexicalError = !((source[i] == '[') || (source[i] == ']') || (source[i] == ':') || (source[i] == ','));
- int last = ++i;
- tokens.push_back(Token(string(1, source[first]), symbol ? SingleChar : Undefined, index++, first, last, symbol ? "Single char" : "Undefined"));
- }
- }
- return tokens;
- }
- int SizeOfImm(int type, int imm)
- {
- if (type == 1)
- {
- if ((-256 <= imm) && (imm < 256)) return 1;
- else return -1;
- }
- else if (type == 2)
- {
- if ((-65536 <= imm) && (imm < 65536))
- {
- return ((-128 <= imm) && (imm < 128)) ? 1 : 2;
- }
- else return -1;
- }
- return ((-128 <= imm) && (imm < 128)) ? 1 : 4;
- }
- int SizeOfDisp(int disp)
- {
- return ((0x80 <= disp) && (disp < 0xFF80) || (0x10000 <= disp) && (disp < 0xFFFF80) || (0x1000000 <= disp) && (disp < 0xFFFFFF80)) ? 4 : 1;
- }
- string GetSegmentOfReg(const Token ®)
- {
- string text = reg.text;
- if ((text.compare("ESP") == 0) || (text.compare("EBP") == 0) || (text.compare("SP") == 0) || (text.compare("BP") == 0)) return "SS";
- else if (reg.type == Register32) return "DS";
- else return "";
- }
- bool Sentence::lookup(Compiler *view)
- {
- int len = tokens.size();
- if (label.index != -1)
- {
- if (!view->AddSymbol(tokens[label.index].text, Symbol(view->segment, format(" %.4X ", view->offset), "L NEAR")))
- {
- return valid = false;
- }
- printable = true;
- }
- else if (mnemo.index != -1)
- {
- auto &mnemocode = tokens[mnemo.index];
- if (name.index != -1)
- {
- if (mnemocode.text.compare("SEGMENT") == 0)
- {
- if (!view->firstSegment(tokens[name.index].text)) return valid = false;
- printable = true;
- }
- else if (mnemocode.text.compare("ENDS") == 0)
- {
- if (!view->EndSegment(tokens[name.index].text, view->offset)) return valid = false;
- printable = true;
- }
- else if (mnemocode.text.compare("EQU") == 0)
- {
- vector<Token> equ;
- int i = mnemo.index + 1;
- if (i < len)
- {
- while (i < len)
- {
- equ.push_back(tokens[i++]);
- }
- }
- else return false;
- int count = equ.size();
- Symbol symbol;
- symbol.text = source.substr(equ[0].first, equ[count - 1].last);
- if (count == 1)
- {
- symbol.type = "NUMBER";
- if (equ[0].type == Binary)
- {
- symbol.value = format("%.4X", stol(symbol.text, 0, 2));
- }
- else if (equ[0].type == Heximal)
- {
- symbol.value = format("%.4X", stol(symbol.text, 0, 10));
- }
- else if (equ[0].type == Decimal)
- {
- symbol.value = format("%.4X", stol(symbol.text, 0, 16));
- }
- else return valid = false;
- prefix = format(" = %s ", symbol.value.c_str());
- }
- else if (count > 0)
- {
- symbol.type = "TEXT";
- symbol.value = symbol.text;
- prefix = " = ";
- }
- else return valid = false;
- if (!view->AddSymbol(tokens[name.index].text, symbol)) return valid = false;
- if (!view->SetEqu(tokens[name.index].text, equ)) return valid = false;
- }
- else if (mnemocode.type == DataType)
- {
- Symbol symbol;
- symbol.value = format(" %.4X ", view->offset);
- symbol.segment = view->segment;
- if (mnemocode.text.compare("DB") == 0)
- {
- symbol.type = "L BYTE";
- if (operands[0].valid)
- {
- if (operands[0].IsString())
- {
- length = operands[0].text.size();
- }
- else if (operands[0].IsImmediate())
- {
- length = 1;
- }
- else return valid = false;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("DW") == 0)
- {
- symbol.type = "L WORD";
- if (operands[0].valid)
- {
- if (operands[0].IsImmediate())
- {
- length = 2;
- }
- else return valid = false;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("DD") == 0)
- {
- symbol.type = "L DWORD";
- if (operands[0].valid)
- {
- if (operands[0].IsImmediate())
- {
- length = 4;
- }
- else return valid = false;
- }
- else return valid = false;
- }
- if (!view->AddSymbol(tokens[name.index].text, symbol))
- {
- return valid = false;
- }
- printable = true;
- }
- }
- else if (mnemocode.text.compare("IF") == 0)
- {
- if (operands[0].IsImmediate())
- {
- If context;
- context.value = operands[0].imm;
- skip = !context.value;
- view->ifTable.push_back(context);
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("ELSE") == 0)
- {
- if (view->ifTable.empty()) return valid = false;
- If &context = view->ifTable.back();
- skip = !(context.value = !context.value);
- }
- else if (mnemocode.text.compare("ENDIF") == 0)
- {
- if (view->ifTable.empty()) return valid = false;
- view->ifTable.pop_back();
- }
- else if (!view->ifTable.empty() && !view->ifTable.back().value)
- {
- skip = true;
- }
- else if (mnemocode.text.compare("END") == 0)
- {
- }
- else if (mnemocode.type == Instruction)
- {
- printable = true;
- if (mnemocode.text.compare("CWDE") == 0)
- {
- length = 2;
- }
- else if (mnemocode.text.compare("INC") == 0)
- {
- if (!operands[0].IsRegister()) return valid = false;
- length = 1;
- }
- else if (mnemocode.text.compare("STOS") == 0)
- {
- if (operands[0].IsMemory())
- {
- Token &base = operands[0].base;
- string &sreg = operands[0].sreg;
- length = 1/*instr*/ + 1/*mod*/ + 4/*ident*/;
- if (!sreg.empty() && (sreg.compare(GetSegmentOfReg(base)) || sreg.compare("DS"))) length += 1;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("ADD") == 0)
- {
- if (operands[0].IsRegister() && operands[1].IsRegister())
- {
- if (operands[0].reg.type == operands[1].reg.type) length = 2;
- else return valid = false;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("CMP") == 0)
- {
- if (operands[0].IsRegister() && operands[1].IsMemory())
- {
- const Token ® = operands[0].reg;
- Token &base = operands[1].base;
- string &sreg = operands[1].sreg;
- length = 1/*instr*/ + 1/*mod*/ + 4/*ident*/;
- if (!sreg.empty() && (sreg.compare(GetSegmentOfReg(base)) || sreg.compare("DS"))) length += 1;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("AND") == 0)
- {
- if (operands[0].IsMemory() && operands[1].IsRegister())
- {
- Token &base = operands[0].base;
- string &sreg = operands[0].sreg;
- length = 1/*instr*/ + 1/*mod*/ + 4/*ident*/;
- if (!sreg.empty() && (sreg.compare(GetSegmentOfReg(base)) || sreg.compare("DS"))) length += 1;
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("MOV") == 0)
- {
- if (operands[0].IsRegister() && operands[1].IsImmediate())
- {
- length = 1 + SizeOfImm(operands[0].tokens[0].type == Register8 ? 1 : 4, operands[1].imm & 0xFFFFFFFF);
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("OR") == 0)
- {
- if (operands[0].IsMemory() && operands[1].IsImmediate())
- {
- length = 1/*instr*/ + 1/*mod*/ + 4/*ident*/ + SizeOfImm(operands[0].ptr, operands[1].imm & 0xFFFFFFFF);
- }
- else return valid = false;
- }
- else if (mnemocode.text.compare("JBE") == 0)
- {
- if (operands[0].IsMemory())
- {
- Token &base = operands[0].base;
- string &sreg = operands[0].sreg;
- length = 1/*instr*/ + 1/*mod*/ + 4/*ident*/;
- if (!sreg.empty() && (sreg.compare(GetSegmentOfReg(base)) || sreg.compare("DS"))) length += 1;
- }
- else if (operands[0].IsName())
- {
- string &sreg = operands[0].sreg;
- length = (view->symbols.find(operands[0].name) != view->symbols.end()) ? 2 : 6;
- if (!sreg.empty() && sreg.compare("DS")) length += 1;
- }
- else return valid = false;
- }
- }
- }
- else if ((name.index != -1) || !operands.empty())
- {
- return valid = false;
- }
- return true;
- }
- void Sentence::writeSyntax(FILE *file)
- {
- if (source.empty()) return;
- fprintf(file, " Label Mnemocode 1st operand 2nd operand\n");
- fprintf(file, " index index index count index count\n");
- fprintf(file, " %5i %9i %5i %5i %5i %5i\n\n", label.index & name.index, mnemo.index, operands[0].first, operands[0].tokens.size(), operands[1].first, operands[1].tokens.size());
- int index = 0;
- for (auto &token : tokens)
- {
- fprintf(file, "%-2d | %11s | %2i | %16s |\n", index++, token.text.c_str(), token.text.size(), token.info.c_str());
- }
- fprintf(file, "\n");
- }
- void Sentence::writeOffset(FILE *file)
- {
- if (skip == false)
- {
- if (printable) fprintf(file, " %.4X ", offset);
- else if (prefix.empty()) fprintf(file, " ");
- else fprintf(file, prefix.c_str());
- fprintf(file, "\t\t%s\n", source.c_str());
- }
- }
- void Compiler::parse(int argc, char *argv[])
- {
- if (argc > 1) filename = argv[1];
- auto pos = filename.find_last_of(".");
- cout << "Source filename[.asm]: " << filename;
- if (argc <= 1) while (!getline(cin, filename)); else cout << endl;
- if (filename.find_last_of(".") == string::npos) filename += ".asm";
- if (argc > 2) listing = argv[2];
- cout << "Source listing[.lst]: " << listing;
- if (argc <= 2) getline(cin, listing); else cout << endl;
- if (listing.find_last_of(".") == string::npos) listing += ".lst";
- cout << endl;
- ifTable.clear();
- lineNumber = 0;
- string line;
- ifstream file(filename);
- offset = 0;
- while (getline(file, line))
- {
- lexicalError = false;
- lineNumber++;
- const auto &tokens = tokenize(line);
- Sentence sentence(line, tokens);
- sentence.lookup(this);
- sentence.offset = offset;
- offset += sentence.length;
- sentences.push_back(sentence);
- }
- file.close();
- }
- void Compiler::write()
- {
- if (FILE *file = fopen((filename.substr(0, filename.find_last_of(".")) + ".lex").c_str(), "w"))
- {
- int lineNumber = 0;
- for (auto &sentence : sentences)
- {
- fprintf(file, " %s\n", sentence.source.c_str());
- sentence.writeSyntax(file);
- lineNumber++;
- }
- fclose(file);
- }
- if (FILE *file = fopen(listing.c_str(), "w"))
- {
- int lineNumber = 0;
- for (auto &sentence : sentences)
- {
- sentence.writeOffset(file);
- lineNumber++;
- }
- fprintf(file, "\n\n N a m e Size Length\n\n");
- for (auto &segment : segments)
- {
- fprintf(file, "%-32s\t%-7s\t%-.4X\n", segment.first.c_str(), "32 Bit", segment.second);
- }
- fprintf(file, "\nSymbols:\n N a m e Type Value Attr\n");
- for (auto symbol : symbols)
- {
- fprintf(file, "%-32s\t%-7s\t%-s\t%s\n", symbol.first.c_str(), symbol.second.type.c_str(), symbol.second.value.c_str(), symbol.second.segment.c_str());
- }
- fprintf(file, "\n");
- fclose(file);
- }
- }
- int main(int argc, char *argv[])
- {
- Compiler compiler;
- compiler.parse(argc, argv);
- compiler.write();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement