Advertisement
Guest User

assembler.cpp

a guest
Apr 25th, 2011
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. /******************************************************************************
  2. Filename: Assembler.cpp
  3. By: Matthew DeBord and Ronald Radut
  4. Created: 04/11/2011
  5. Last Modified: 04/20/2011
  6. Description: This cpp code builds and executes the
  7. assembler turning our .s to .o
  8. ******************************************************************************/
  9. #include <iostream>
  10. #include <sstream>
  11. #include <vector>
  12. #include "Assembler.h"
  13. using namespace std;
  14.  
  15. Assembler::Assembler()
  16. {
  17.     this->rd=-1;
  18.     this->rs=-1;
  19.     this->constant=-129;
  20. }
  21. void Assembler::assemble()
  22. {
  23.     fstream assemblyProg;
  24.     assemblyProg.open("prog.s", ios::in);
  25.     if (!assemblyProg.is_open()) {
  26.     cout << "prog.s failed to open.\n";
  27.     exit(1);
  28.     }
  29.  
  30.     getline(assemblyProg, this->line);
  31.     while (!assemblyProg.eof())
  32.     {
  33.         defineInst(this->line); // send line to defineInst to be parsed
  34.         getline(assemblyProg, this->line);
  35.     }
  36.     assemblyProg.close();
  37.  
  38.     fstream outFile;
  39.     outFile.open("prog.o", ios::out);
  40.     for (int i=0;i<v.size();i++)
  41.         {
  42.         outFile << v[i] << endl; // write each instruction to a new line in the prog.o file.
  43.         }
  44.     outFile.close();
  45. }
  46. void Assembler::defineInst(string line)
  47. {
  48.         istringstream str(line.c_str()); //convert to c-string
  49.         str >> opcode;
  50.         if (opcode!="!")
  51.         {
  52.             if (opcode == "load")
  53.             {
  54.                 optemp = 0;
  55.                 imm = 0;
  56.                 str >> rd >> address;  
  57.                 int instruction = buildFormat2(optemp, rd, imm, address);
  58.                 v.push_back(instruction);
  59.             }
  60.  
  61.  
  62.             else if (opcode == "loadi")
  63.             {
  64.                 optemp = 0;
  65.                 imm = 1;
  66.                 str >> rd >> constant;
  67.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  68.                 v.push_back(instruction);
  69.             }
  70.  
  71.             else if (opcode == "store")
  72.             {
  73.                 optemp = 1;
  74.                 imm = 0;
  75.                 str >> rd >> address;
  76.                 int instruction = buildFormat2(optemp, rd, imm, address);
  77.                 v.push_back(instruction);
  78.             }    
  79.  
  80.             else if (opcode == "add")
  81.             {
  82.                 optemp = 2;
  83.                 imm = 0;
  84.                 str >> rd >> rs;
  85.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  86.                 v.push_back(instruction);
  87.             }    
  88.  
  89.             else if (opcode == "addi") //set carry
  90.             {
  91.                 optemp = 2;
  92.                 imm = 1;
  93.                 str >> rd >> constant;
  94.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  95.                 v.push_back(instruction);
  96.             }
  97.  
  98.             else if (opcode == "addc") //set carry
  99.             {
  100.                 optemp = 3;
  101.                 imm = 0;    
  102.                 str >> rd >> rs;
  103.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  104.                 v.push_back(instruction);
  105.             }
  106.  
  107.             else if (opcode == "addci") //set carry
  108.             {
  109.                 optemp = 3;
  110.                 imm = 1;  
  111.                 str >> rd >> constant;
  112.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  113.                 v.push_back(instruction);
  114.             }
  115.  
  116.             else if (opcode == "sub") //set carry
  117.             {
  118.                 optemp = 4;
  119.                 imm = 0;  
  120.                 str >> rd >> rs;
  121.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  122.                 cout << instruction;
  123.             }
  124.             else if (opcode == "subi") //set carry
  125.             {
  126.                 optemp = 4;
  127.                 imm = 1;
  128.                 str >> rd >> constant;
  129.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  130.                 v.push_back(instruction);
  131.             }
  132.             else if (opcode == "subc") //set carry
  133.             {
  134.                 optemp = 5;
  135.                 imm = 0;  
  136.                 str >> rd >> rs;
  137.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  138.                 cout << instruction;
  139.             }
  140.             else if (opcode == "subci") //set carry
  141.             {
  142.                 optemp = 5;
  143.                 imm = 1;
  144.                 str >> rd >> constant;
  145.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  146.                 v.push_back(instruction);
  147.             }
  148.             else if (opcode == "and")
  149.             {
  150.                 optemp = 6;
  151.                 imm = 0;
  152.                 str >> rd >> rs;
  153.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  154.                 cout << instruction;
  155.             }
  156.             else if (opcode == "andi")
  157.             {
  158.                 optemp = 6;
  159.                 imm = 1;
  160.                 str >> rd >> constant; 
  161.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  162.                 v.push_back(instruction);
  163.             }
  164.             else if (opcode == "xor")
  165.             {
  166.                 optemp = 7;
  167.                 imm = 0;
  168.                 str >> rd >> rs;
  169.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  170.                 cout << instruction;
  171.             }
  172.             else if (opcode == "xori")
  173.             {
  174.                 optemp = 7;
  175.                 imm = 1;
  176.                 str >> rd >> constant;
  177.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  178.                 v.push_back(instruction);
  179.             }
  180.             else if (opcode == "compl")
  181.             {
  182.                 optemp = 8;
  183.                 imm = 0;
  184.                 str >> rd;
  185.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  186.                 v.push_back(instruction);
  187.             }
  188.             else if (opcode == "shl") //set carry
  189.             {
  190.                 optemp = 9;
  191.                 imm = 0;  
  192.                 str >> rd; 
  193.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  194.                 v.push_back(instruction);
  195.             }
  196.             else if (opcode == "shla") //set carry and sign extend
  197.             {
  198.                 optemp = 10;
  199.                 imm = 0;
  200.                 str >> rd;
  201.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  202.                 v.push_back(instruction);
  203.             }
  204.             else if (opcode == "shr") //set carry
  205.             {
  206.                 optemp = 11;
  207.                 imm = 0;                    
  208.                 str >> rd;
  209.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  210.                 v.push_back(instruction);
  211.             }
  212.             else if (opcode == "shra") //set carry and sign extend
  213.             {
  214.                 optemp = 12;
  215.                 imm = 0;                    
  216.                 str >> rd;
  217.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  218.                 v.push_back(instruction);
  219.             }
  220.             else if (opcode == "compr")
  221.             {
  222.                 optemp = 13;
  223.                 imm = 0;
  224.                 str >> rd >> rs;
  225.                 int instruction = buildFormat1(optemp, rd, rs, imm);
  226.                 cout << instruction;
  227.             }
  228.             else if (opcode == "compri")
  229.             {
  230.                 optemp = 13;
  231.                 imm = 1;                    
  232.                 str >> rd >> constant;
  233.                 int instruction = buildFormat2(optemp, rd, imm, constant);
  234.                 v.push_back(instruction);
  235.             }
  236.             else if (opcode == "getstat")
  237.             {
  238.                 optemp = 14;
  239.                 imm = 0;
  240.                 str >> rd;
  241.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  242.                 v.push_back(instruction);
  243.             }
  244.             else if (opcode == "putstat")
  245.             {
  246.                 optemp = 15;
  247.                 imm = 0;                    
  248.                 str >> rd;
  249.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  250.                 v.push_back(instruction);
  251.             }
  252.             else if (opcode == "jump")
  253.             {
  254.                 optemp = 16;
  255.                 imm = 0;                    
  256.                 str >> address;
  257.                 int instruction = buildFormat2(optemp, rd, imm, address);
  258.                 v.push_back(instruction);
  259.             }
  260.             else if (opcode == "jumpl")
  261.             {
  262.                 optemp = 17;
  263.                 imm = 0;                    
  264.                 str >> address;
  265.                 int instruction = buildFormat2(optemp, rd, imm, address);
  266.                 v.push_back(instruction);
  267.             }
  268.             else if (opcode == "jumpe")
  269.             {
  270.                 optemp = 18;
  271.                 imm = 0;                    
  272.                 str >> address;
  273.                 int instruction = buildFormat2(optemp, rd, imm, address);
  274.                 v.push_back(instruction);
  275.             }
  276.             else if (opcode == "jumpg")
  277.             {
  278.                 optemp = 19;
  279.                 imm = 0;                    
  280.                 str >> address;
  281.                 int instruction = buildFormat2(optemp, rd, imm, address);
  282.                 v.push_back(instruction);
  283.             }
  284.             else if (opcode == "call")
  285.             {
  286.                 optemp = 20;
  287.                 imm = 0;                    
  288.                 str >> address;
  289.                 int instruction = buildFormat2(optemp, rd, imm, address);
  290.                 v.push_back(instruction);
  291.             }
  292.             else if (opcode == "return")
  293.             {
  294.                 optemp = 21;
  295.                 imm = 0;
  296.                 str >> rd;
  297.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  298.                 v.push_back(instruction);
  299.             }
  300.             else if (opcode == "read")
  301.             {
  302.                 optemp = 22;
  303.                 imm = 0;
  304.                 str >> rd;
  305.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  306.                 v.push_back(instruction);
  307.             }
  308.             else if (opcode == "write")
  309.             {
  310.                 optemp = 23;
  311.                 imm = 0;    
  312.                 str >> rd;
  313.                 int instruction = buildFormat2(optemp, rd, imm, 0);
  314.                 v.push_back(instruction);
  315.             }
  316.             else if (opcode == "halt")
  317.             {
  318.                 optemp = 24;
  319.                 imm = 0;
  320.                 optemp = optemp << 11;
  321.                 int instruction = optemp;
  322.                 v.push_back(instruction);
  323.             }
  324.             else if (opcode == "noop")
  325.             {
  326.                 optemp = 25;
  327.                 imm = 0;
  328.                 optemp = optemp << 11;
  329.                 int instruction = optemp;
  330.                 v.push_back(instruction);
  331.             }
  332.  
  333.             else
  334.             {
  335.                 cout << "OPCODE:" << opcode << " is unkown." << endl << "Press any key to exit." << endl;
  336.                 cin.get();
  337.                 exit(1);
  338.             }
  339.         }
  340. }
  341. int Assembler::buildFormat1(int opcode, int rd, int rs, int imm)
  342. {
  343.     if (rd<0||rd>3||rs<0||rs>3) // error checking for legal register values
  344.         {
  345.             cout << "Invalid register" << endl << "Press any key to exit";
  346.             cin.get();
  347.             exit(1);
  348.         }
  349.     int finalvalue;
  350.     opcode = opcode <<11; // begin bit shifts
  351.     rd = rd << 9;
  352.     rs = rs << 6;
  353.     imm = imm << 8;
  354.     finalvalue = opcode+rd+rs+imm;
  355.     return finalvalue;
  356. }
  357. int Assembler::buildFormat2(int opcode, int rd, int imm, int tail)
  358. {
  359.     if (rd<0||rd>3) // error checking for legal register values
  360.         {
  361.             cout << "Invalid register" << endl << "Press any key to exit";
  362.             cin.get();
  363.             exit(1);
  364.         }
  365.     int finalvalue;
  366.     opcode = opcode << 11; // begin bit shifts
  367.     rd = rd << 9;
  368.     imm = imm << 8;
  369.     if (tail<0) // must use masking to get the final 2's compliment value inserted to rightmost 8 bits of instruction.
  370.         {
  371.         finalvalue = opcode+rd+imm;
  372.         finalvalue = finalvalue + 255;
  373.         finalvalue = tail&finalvalue;
  374.         }
  375.     else
  376.         {
  377.         finalvalue = opcode+rd+imm+tail;
  378.         }
  379.     return finalvalue;
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement