Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Filename: Assembler.cpp
- By: Matthew DeBord and Ronald Radut
- Created: 04/11/2011
- Last Modified: 04/20/2011
- Description: This cpp code builds and executes the
- assembler turning our .s to .o
- ******************************************************************************/
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include "Assembler.h"
- using namespace std;
- Assembler::Assembler()
- {
- this->rd=-1;
- this->rs=-1;
- this->constant=-129;
- }
- void Assembler::assemble()
- {
- fstream assemblyProg;
- assemblyProg.open("prog.s", ios::in);
- if (!assemblyProg.is_open()) {
- cout << "prog.s failed to open.\n";
- exit(1);
- }
- getline(assemblyProg, this->line);
- while (!assemblyProg.eof())
- {
- defineInst(this->line); // send line to defineInst to be parsed
- getline(assemblyProg, this->line);
- }
- assemblyProg.close();
- fstream outFile;
- outFile.open("prog.o", ios::out);
- for (int i=0;i<v.size();i++)
- {
- outFile << v[i] << endl; // write each instruction to a new line in the prog.o file.
- }
- outFile.close();
- }
- void Assembler::defineInst(string line)
- {
- istringstream str(line.c_str()); //convert to c-string
- str >> opcode;
- if (opcode!="!")
- {
- if (opcode == "load")
- {
- optemp = 0;
- imm = 0;
- str >> rd >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "loadi")
- {
- optemp = 0;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "store")
- {
- optemp = 1;
- imm = 0;
- str >> rd >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "add")
- {
- optemp = 2;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- v.push_back(instruction);
- }
- else if (opcode == "addi") //set carry
- {
- optemp = 2;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "addc") //set carry
- {
- optemp = 3;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- v.push_back(instruction);
- }
- else if (opcode == "addci") //set carry
- {
- optemp = 3;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "sub") //set carry
- {
- optemp = 4;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- cout << instruction;
- }
- else if (opcode == "subi") //set carry
- {
- optemp = 4;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "subc") //set carry
- {
- optemp = 5;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- cout << instruction;
- }
- else if (opcode == "subci") //set carry
- {
- optemp = 5;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "and")
- {
- optemp = 6;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- cout << instruction;
- }
- else if (opcode == "andi")
- {
- optemp = 6;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "xor")
- {
- optemp = 7;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- cout << instruction;
- }
- else if (opcode == "xori")
- {
- optemp = 7;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "compl")
- {
- optemp = 8;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "shl") //set carry
- {
- optemp = 9;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "shla") //set carry and sign extend
- {
- optemp = 10;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "shr") //set carry
- {
- optemp = 11;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "shra") //set carry and sign extend
- {
- optemp = 12;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "compr")
- {
- optemp = 13;
- imm = 0;
- str >> rd >> rs;
- int instruction = buildFormat1(optemp, rd, rs, imm);
- cout << instruction;
- }
- else if (opcode == "compri")
- {
- optemp = 13;
- imm = 1;
- str >> rd >> constant;
- int instruction = buildFormat2(optemp, rd, imm, constant);
- v.push_back(instruction);
- }
- else if (opcode == "getstat")
- {
- optemp = 14;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "putstat")
- {
- optemp = 15;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "jump")
- {
- optemp = 16;
- imm = 0;
- str >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "jumpl")
- {
- optemp = 17;
- imm = 0;
- str >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "jumpe")
- {
- optemp = 18;
- imm = 0;
- str >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "jumpg")
- {
- optemp = 19;
- imm = 0;
- str >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "call")
- {
- optemp = 20;
- imm = 0;
- str >> address;
- int instruction = buildFormat2(optemp, rd, imm, address);
- v.push_back(instruction);
- }
- else if (opcode == "return")
- {
- optemp = 21;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "read")
- {
- optemp = 22;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "write")
- {
- optemp = 23;
- imm = 0;
- str >> rd;
- int instruction = buildFormat2(optemp, rd, imm, 0);
- v.push_back(instruction);
- }
- else if (opcode == "halt")
- {
- optemp = 24;
- imm = 0;
- optemp = optemp << 11;
- int instruction = optemp;
- v.push_back(instruction);
- }
- else if (opcode == "noop")
- {
- optemp = 25;
- imm = 0;
- optemp = optemp << 11;
- int instruction = optemp;
- v.push_back(instruction);
- }
- else
- {
- cout << "OPCODE:" << opcode << " is unkown." << endl << "Press any key to exit." << endl;
- cin.get();
- exit(1);
- }
- }
- }
- int Assembler::buildFormat1(int opcode, int rd, int rs, int imm)
- {
- if (rd<0||rd>3||rs<0||rs>3) // error checking for legal register values
- {
- cout << "Invalid register" << endl << "Press any key to exit";
- cin.get();
- exit(1);
- }
- int finalvalue;
- opcode = opcode <<11; // begin bit shifts
- rd = rd << 9;
- rs = rs << 6;
- imm = imm << 8;
- finalvalue = opcode+rd+rs+imm;
- return finalvalue;
- }
- int Assembler::buildFormat2(int opcode, int rd, int imm, int tail)
- {
- if (rd<0||rd>3) // error checking for legal register values
- {
- cout << "Invalid register" << endl << "Press any key to exit";
- cin.get();
- exit(1);
- }
- int finalvalue;
- opcode = opcode << 11; // begin bit shifts
- rd = rd << 9;
- imm = imm << 8;
- if (tail<0) // must use masking to get the final 2's compliment value inserted to rightmost 8 bits of instruction.
- {
- finalvalue = opcode+rd+imm;
- finalvalue = finalvalue + 255;
- finalvalue = tail&finalvalue;
- }
- else
- {
- finalvalue = opcode+rd+imm+tail;
- }
- return finalvalue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement