-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathU.h
73 lines (71 loc) · 1.71 KB
/
U.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef U_H
#define U_H
#include <bits/stdc++.h>
#include <string>
#include "R.h"
#include "I.h"
using namespace std;
string U(string s, int j, string Mnemonic)
{
string ans = "";
string rd = "";
string imm = "";
int flag = 0;
// Extracting rd,imm fields
if (Mnemonic == "auipc" || Mnemonic == "lui")
{
for (int i = j; i < s.size(); i++)
{
if (s[i] == 'x')
{
i++;
if (flag == 0)
{
while (s[i] != ',' && s[i] != ' ')
{
rd = rd + s[i];
i++;
}
flag++;
}
}
if (flag == 1)
{
i++;
while (s[i] == ' ')
{
i++;
}
while (i != s.size() && s[i] != ' ')
{
imm = imm + s[i];
i++;
}
flag = 0;
break;
// here the compiler terminates the line as soon as it gets the required values for instruction
}
}
}
// Adding opcode
if (Mnemonic == "auipc")
ans = ans + "0010111";
else
ans += "0110111";
// Adding rd
int rd_num = stoi(rd);
if (rd_num > 31 || rd_num < 0)
{
printf("register not found");
exit(0);
}
ans = dectobin(rd_num, 5, 1) + ans;
// Adding immediate
int imm_num = stoi(imm);
ans = dectobin(imm_num, 20, 1) + ans;
// converting binary to hexa
string hex = bintodec(ans);
// returning in hexadecimal
return hex;
}
#endif