forked from crystae2003/RISC-V-Assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathU.h
61 lines (60 loc) · 1.29 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
#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(i!=s.size() && s[i]!=' ')
{
imm=imm+s[i];
i++;
}
flag=0;
break;
}
}
}
//Adding opcode
if(Mnemonic=="auipc")
ans=ans+"0010111";
else ans+="0110111";
//Adding rd
int rd_num=stoi(rd);
ans=dectobin(rd_num,5)+ans;
//Adding immediate
int imm_num=stoi(imm);
ans=dectobin(imm_num,20)+ans;
//converting binary to hexa
string hex=bintodec(ans);
//returning in hexadecimal
return hex;
}
#endif