├── Assembler.cpp ├── README.md ├── input.txt ├── output_binary.txt └── output_hexa.txt /Assembler.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | void BinarytoHex(string binnum,ofstream &out) 9 | { 10 | char hexa[1000]; 11 | string app=""; 12 | int temp; 13 | long int i=0,j=0; 14 | 15 | while(binnum[i]){ 16 | binnum[i] = binnum[i] -48; 17 | ++i; 18 | } 19 | --i; 20 | while(i-2>=0){ 21 | temp = binnum[i-3] *8 + binnum[i-2] *4 + binnum[i-1] *2 + binnum[i] ; 22 | if(temp > 9) 23 | hexa[j++] = temp + 55; 24 | else 25 | hexa[j++] = temp + 48; 26 | i=i-4; 27 | } 28 | if(i ==1) 29 | hexa[j] = binnum[i-1] *2 + binnum[i] + 48 ; 30 | else if(i==0) 31 | hexa[j] = binnum[i] + 48 ; 32 | else 33 | --j; 34 | 35 | while(j>=0){ 36 | app += hexa[j--]; 37 | } 38 | out<(n).to_string(); 45 | 46 | return binary; 47 | } 48 | 49 | string decimalToBinary_ten(int n) 50 | { 51 | std::string binary = std::bitset<10>(n).to_string(); 52 | 53 | return binary; 54 | } 55 | 56 | int main() 57 | { 58 | 59 | string binary,bin; 60 | ifstream input; 61 | input.open("input.txt"); 62 | ofstream output; 63 | output.open("output_binary.txt"); 64 | ofstream output2; 65 | output2.open("output_hexa.txt"); 66 | 67 | string op, rs, rt, rd, j,cons ; 68 | string instruction,fil; 69 | 70 | string opn, rsn, rtn, rdn ; 71 | 72 | string R[2][13]; 73 | int temp=1; 74 | 75 | R[0][0] = "ADD"; 76 | R[1][0] = "0000"; 77 | 78 | R[0][1] = "SUB"; 79 | R[1][1] = "0001"; 80 | 81 | R[0][2] = "SRL"; 82 | R[1][2] = "0010"; 83 | 84 | R[0][3] = "SLL"; 85 | R[1][3] = "0011"; 86 | 87 | R[0][4] = "AND"; 88 | R[1][4] = "0100"; 89 | 90 | R[0][5] = "OR"; 91 | R[1][5] = "0101"; 92 | 93 | R[0][6] = "XOR"; 94 | R[1][6] = "0110"; 95 | 96 | R[0][7] = "ADDI"; 97 | R[1][7] = "0111"; 98 | 99 | R[0][8] = "LW"; 100 | R[1][8] = "1000"; 101 | 102 | R[0][9] = "SW"; 103 | R[1][9] = "1001"; 104 | 105 | R[0][10] = "BEQ"; 106 | R[1][10] = "1010"; 107 | 108 | R[0][11] = "BNE"; 109 | R[1][11] = "1011"; 110 | 111 | R[0][12] = "J"; 112 | R[1][12] = "1100"; 113 | 114 | 115 | string registers[2][8]; 116 | 117 | 118 | registers[0][0] = "$zero"; 119 | registers[1][0] = "000"; 120 | 121 | registers[0][1] = "$t0"; 122 | registers[1][1] = "001"; 123 | 124 | registers[0][2] = "$t1"; 125 | registers[1][2] = "010"; 126 | 127 | registers[0][3] = "$t2"; 128 | registers[1][3] = "011"; 129 | 130 | registers[0][4] = "$t3"; 131 | registers[1][4] = "100"; 132 | 133 | registers[0][5] = "$s0"; 134 | registers[1][5] = "101"; 135 | 136 | registers[0][6] = "$s1"; 137 | registers[1][6] = "110"; 138 | 139 | registers[0][7] = "$s2"; 140 | registers[1][7] = "111"; 141 | 142 | int i; 143 | 144 | while(input>>instruction) 145 | { 146 | 147 | for( i=0; i<13; i++) 148 | { 149 | if(instruction == R[0][i]) 150 | { 151 | op = R[1][i]; 152 | break; 153 | } 154 | } 155 | 156 | if(i==13) 157 | { 158 | cout<<"Instruction not found!"<>rd>>rs>>rt; 167 | 168 | rd.erase(rd.begin()+rd.length()-1, rd.end()); 169 | rs.erase(rs.begin()+rs.length()-1, rs.end()); 170 | 171 | int count = 0; 172 | for(int i=0; i<8; i++) 173 | { 174 | if(count==3) 175 | { 176 | break; 177 | } 178 | if(rd==registers[0][i]) 179 | { 180 | rdn = registers[1][i]; 181 | 182 | if(rdn=="000") 183 | { 184 | cout<<"Zero can't be used"<>rd>>rs>>sh; 226 | if(sh>15) 227 | { 228 | cout<<"Invalid Address"<>rd>>rs>>sh; 295 | if(sh>1) 296 | { 297 | cout<<"Invalid Address"<>rd>>rs>>sh; 362 | 363 | if( sh<0) 364 | { 365 | cout<<"Invalid Address"<15) 369 | { 370 | cout<<"Invalid Address"<>rdin>>source; 436 | for(int i=0; i=0 && value <16) 482 | { 483 | string shbin=decimalToBinary_four(value); 484 | 485 | immediate =shbin; 486 | 487 | if(temp) 488 | { 489 | output<>sh; 519 | if(sh<0) 520 | { 521 | cout<<"Invalid address"<1024) 526 | { 527 | cout<<"Invalid address"<