mips64-assembler/ma.C
2025-11-04 14:00:16 -05:00

53 lines
1.3 KiB
C

/* CS5483 Project 1: MIPS64 Assembler
* Ryan Densmore
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Assembler.h"
std::string parseArgs(int argc, char ** argv);
void usage();
int main(int argc, char ** argv) {
std::string name = parseArgs(argc, argv);
std::fstream asmFile(argv[1], std::fstream::in);
if (!asmFile.is_open()) usage();
std::string line;
std::vector<std::string> lines;
while (asmFile.good()) {
std::getline(asmFile, line);
lines.push_back(std::move(line));
}
Assembler assembler(std::move(lines));
assembler.parse();
// assemble
std::vector<uint32_t> hexCode = assembler.assemble();
std::fstream hexFile(name + ".hex", std::fstream::out);
char hex[9];
for (uint32_t i = 0; i < hexCode.size(); i++) {
sprintf(hex, "%.8X", hexCode[i]);
hexFile << hex << "\n";
}
}
std::string parseArgs(int argc, char ** argv) {
// make sure only one arg
if (argc != 2) usage();
// make sure it's a .asm
std::string fileArg(argv[1]);
if (fileArg.length() < 4 || fileArg.substr(fileArg.length()-4, 4) != ".asm") usage();
return fileArg.substr(0, fileArg.length()-4);
}
void usage() {
printf("usage: ma <file>.asm\n");
exit(0);
}