Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <sstream>
  9. #include <cstring>
  10.  
  11. using namespace std;
  12.  
  13.  
  14. template <class N> N ToNumber(string string_number) {
  15.     N number;
  16.     istringstream iss(string_number);
  17.     iss >> number;
  18.     return number;
  19. }
  20.  
  21.  
  22.  
  23. string ReverseString(string str) {
  24.     string new_string = "";
  25.     for(int i=(str.length()-1); i>=0; i--)
  26.         new_string += str.at(i);
  27.     return new_string;
  28. }
  29.  
  30.  
  31.  
  32.  
  33. string binary(int num) {
  34.     int coefficient = -1, numerator  = num;
  35.     string binary="";
  36.     string result="";
  37.  
  38.     if(num <=7) {
  39.         result= "";
  40.         if(num <=3) {
  41.             result="0";
  42.             if(num <=1) {
  43.                 result="00";
  44.             }
  45.         }
  46.     }
  47.     while(coefficient != 0) {
  48.         binary += to_string(numerator % 2);
  49.         coefficient = numerator / 2;
  50.         numerator = coefficient;
  51.     }
  52.     binary =  ReverseString(binary);
  53.     result+=binary;
  54.     return result;
  55. }
  56.  
  57.  
  58.  
  59. string printWords(string str) {
  60.     string word;
  61.     stringstream iss(str);
  62.  
  63.     while (iss >> word)
  64.         cout<<word<<endl;
  65.     cout<<endl;
  66.     return word;
  67. }
  68.  
  69.  
  70. // convert string n to dicimal
  71. int convertBinaryToDecimal(string  n) {
  72.     string reverse = ReverseString(n);
  73.     char cstr[n.size()];
  74.     strcpy(cstr, reverse.c_str());
  75.     unsigned int decimalNumber = 0, i = 0, remainder;
  76.     while (i!=sizeof(cstr)) {
  77.         remainder = (int)cstr[i] -48;
  78.         // cout<<decimalNumber <<endl;
  79.         decimalNumber += (remainder*(pow(2,i)));
  80.         // cout<<i <<endl;
  81.         // cout<<decimalNumber <<endl;
  82.         // cout<<endl;
  83.         ++i;
  84.     }
  85.     return decimalNumber;
  86. }
  87.  
  88. int main() {
  89.  
  90.     std::vector<string> ans (31);
  91.  
  92.     int number1;
  93.  
  94.     int number2;
  95.  
  96.     int number3;
  97.  
  98.     int number4;
  99.  
  100.     string inst;
  101.  
  102. /// test input
  103.     cout << "input your instruction :";
  104.     cin >> inst;
  105.     if(inst =="add" || inst =="nand") {
  106.  
  107.         cout << "input your rd :";
  108.         cin  >>number1;
  109.         while(number1 >= 8) {
  110.             cout << "rd must least than 8";
  111.             cout << endl;
  112.             cout << "input your rd :";
  113.             cin >> number1;
  114.         }
  115.  
  116.         cout << "input your rs :";
  117.         cin  >>number2;
  118.         while(number2 >= 8) {
  119.             cout << "rs must least than 8";
  120.             cout << endl;
  121.             cout << "input your rs :";
  122.             cin >> number2;
  123.         }
  124.  
  125.         cout << "input your rt :";
  126.         cin  >>number3;
  127.         while(number3 >= 8) {
  128.             cout << "rt must least than 8";
  129.             cout << endl;
  130.             cout << "input your rt :";
  131.             cin >> number3;
  132.         }
  133.  
  134.  
  135.         for(int j =0; j<7 ; j++) {   //fill 0 25-31
  136.             ans.push_back("0");
  137.         }
  138.         if(inst =="add") {
  139.  
  140.             ans.push_back("000"); //opcode =000
  141.         }
  142.         if(inst =="nand") {
  143.             ans.push_back("001"); // opcode =001
  144.         }
  145.         ans.push_back(binary(number3));
  146.         ans.push_back(binary(number2));
  147.  
  148.         for(int i =0; i<13 ; i++) {
  149.             ans.push_back("0");
  150.         }
  151.         ans.push_back(binary(number1));
  152.  
  153.     }
  154.     if(inst =="jalr" ) { // j-type
  155.  
  156.         cout << "input your rd :";
  157.         cin  >>number1;
  158.         while(number1 >= 8) {
  159.             cout << "rd must least than 8";
  160.             cout << endl;
  161.             cout << "input your rd :";
  162.             cin >> number1;
  163.         }
  164.  
  165.         cout << "input your rs :";
  166.         cin  >>number2;
  167.         while(number2 >= 8) {
  168.             cout << "rs must least than 8";
  169.             cout << endl;
  170.             cout << "input your rs :";
  171.             cin >> number2;
  172.         }
  173.  
  174.         cout << "input your rt :";
  175.         cin  >>number3;
  176.         while(number3 >= 8) {
  177.             cout << "rt must least than 8";
  178.             cout << endl;
  179.             cout << "input your rt :";
  180.             cin >> number3;
  181.         }
  182.  
  183.         for(int j =0; j<7 ; j++) {   //fill 0 25-31
  184.             ans.push_back("0");
  185.         }
  186.         ans.push_back("101");// opcode=101
  187.         ans.push_back(binary(number3));
  188.         ans.push_back(binary(number1));
  189.         for(int i =0; i<16 ; i++) {
  190.             ans.push_back("0");
  191.         }
  192.  
  193.     }
  194.     if(inst == "halt" || inst =="noop") {
  195.         for(int j =0; j<7 ; j++) {   //fill 0 25-31
  196.             ans.push_back("0");
  197.         }
  198.         if(inst == "halt") {
  199.             ans.push_back("110");// opcode=110
  200.         }
  201.         if(inst == "noop") {
  202.             ans.push_back("111");// opcode=111
  203.         }
  204.         for(int i =0; i<22 ; i++) {
  205.             ans.push_back("0");
  206.         }
  207.  
  208.     }
  209.     if(inst == "lw" || inst =="sw" || inst == "beq") {
  210.  
  211.  
  212.         cout << "input your rs :";
  213.         cin  >>number2;
  214.         while(number2 >= 8) {
  215.             cout << "rs must least than 8";
  216.             cout << endl;
  217.             cout << "input your rs :";
  218.             cin >> number2;
  219.         }
  220.  
  221.         cout << "input your rt :";
  222.         cin  >>number3;
  223.         while(number3 >= 8) {
  224.             cout << "rt must least than 8";
  225.             cout << endl;
  226.             cout << "input your rt :";
  227.             cin >> number3;
  228.         }
  229.  
  230.         cout << "input your im :";
  231.         cin  >>number4;
  232.         while(number4 >= 32768 || number4 <= -32769) {
  233.             cout << "wrong im";
  234.             cout << endl;
  235.             cout << "input your im :";
  236.             cin >> number4;
  237.         }
  238.  
  239.  
  240.         for(int j =0; j<7 ; j++) {   //fill 0 25-31
  241.             ans.push_back("0");
  242.         }
  243.         if(inst == "lw") {
  244.             ans.push_back("010");// opcode=010
  245.         }
  246.         if(inst == "sw") {
  247.             ans.push_back("011");// opcode=011
  248.         }
  249.         if(inst == "beq") {
  250.             ans.push_back("100");// opcode=100
  251.         }
  252.         ans.push_back(binary(number2));
  253.         ans.push_back(binary(number3));
  254.         if(number4 < 0) {
  255.             int con = number4 * (-1);
  256.             con = con - 1;
  257.             string neg;
  258.             string im = (binary(con));
  259.             ans.push_back("1");
  260.             if(binary(con).length()<15) {
  261.                 for(int i=14 ; i>=binary(con).length() ; i--) {
  262.                     ans.push_back("1");
  263.                 }
  264.             }
  265.  
  266.             for(int i=0 ; i <= im.length() ; i++) {
  267.                 if(im.substr(i,1) == "0") {
  268.                     neg += '1';
  269.                 } else if(im.substr(i,1) == "1") {
  270.                     neg += '0';
  271.                 }
  272.             }
  273.             ans.push_back(neg);
  274.  
  275.         }
  276.         if(number4 >= 0) {
  277.             ans.push_back("0");
  278.             if(binary(number4).length()<15) {
  279.                 for(int i=14 ; i>=binary(number4).length() ; i--) {
  280.                     ans.push_back("0");
  281.                 }
  282.             }
  283.             ans.push_back(binary(number4));
  284.         }
  285.  
  286.     }
  287.  
  288.  
  289.     string ansz;
  290.     int int_ans;
  291.     for(int i =0; i<ans.size() ; i++) { // print out the result!!
  292.         ansz+= ans.at(i);
  293.     }
  294.  
  295.     cout <<ansz;
  296.     cout <<endl;
  297.     ans.clear();
  298.     cout <<endl;
  299.     int_ans= convertBinaryToDecimal(ansz);
  300.     cout << ansz.length();
  301.     cout <<endl;
  302.  
  303.  
  304.  
  305.     cout << int_ans;
  306.     cout << endl;
  307.  
  308.     ifstream file("C:/Users/LuckyCat/Desktop/jom/comarch_prot/test1.txt");
  309.     string str;
  310.     while (std::getline(file, str)) {
  311.         printWords(str);
  312.     }
  313.  
  314.  
  315.     return 0;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement