-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJ.h
86 lines (84 loc) · 1.8 KB
/
J.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
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef J_H
#define J_H
#include <bits/stdc++.h>
#include <string>
#include "R.h"
#include "I.h"
#include "SB.h"
#include <unordered_map>
string J(string s, int j, string Mnemonic, unordered_map<string, int> m_label, int PC)
{
string ra = "";
string label = "";
string ans = "";
int flag = 0;
for (int i = j; i < s.size(); i++)
{
if (s[i] == 'x')
{
i++;
if (flag == 0)
{
while (s[i] != ',' && s[i] != ' ')
{
ra = ra + s[i];
i++;
}
flag++;
}
}
if (flag == 1)
{
i++;
while (s[i] == ' ')
{
i++;
}
while (i != s.size() && s[i] != ' ')
{
label = label + s[i];
i++;
}
flag = 0;
break;
}
}
// Adding opcode
ans = ans + "1101111";
// Adding ra
int ra_num = stoi(ra);
if (ra_num > 31 || ra_num < 0)
{
printf("register not found");
exit(0);
}
ans = dectobin(ra_num) + ans;
// Calculating offset for label
int pc_2 = m_label[label];
int offset = pc_2 - PC;
// Adding imm
string imm;
if (offset < 0)
{
imm = si(two(dec2bin(abs(offset))), 20) + ans;
}
else
imm = dtb(offset, 1, 21);
// imm[19:12]
string h = imm.substr(1, 8);
ans = h + ans;
// imm[11]
char eleven = imm[9];
ans = eleven + ans;
// imm[10:1]
h = imm.substr(10, 10);
ans = h + ans;
// imm[20]
char twenty = imm[0];
ans = twenty + ans;
// converting binary to hexa
string hex = bintodec(ans);
// returning in hexadecimal
return hex;
}
#endif