-
Notifications
You must be signed in to change notification settings - Fork 1
/
Assembler.h
188 lines (159 loc) · 5.23 KB
/
Assembler.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Handling assembler directives
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include "MemoryAccess.h"
using namespace std;
vector <int> extractint(string str) { // recieves a string and extracts all the integers and returns them in a list (vector)
vector <int> result;
int sum,currentint;
bool valid = 0;
for(int strIndex = 0 ; strIndex < str.size() ; strIndex++) {
sum = 0;
bool intfound = 0;
while(strIndex < str.size() && isdigit(str[strIndex]) && valid) {
currentint = str[strIndex] - '0';
sum = sum*10 + currentint;
strIndex++;
intfound = 1;
}
if(intfound)
result.push_back(sum);
if(str[strIndex] == ':'){
valid = 1;
}
}
return result; //returning vector of extracted parameters
}
void assembler_initiate(MemoryAccess &memobject)
{
ifstream ifile("input.txt");
ofstream ofile("input1.txt",ios::out);
string current;
// matches the label to address in the memory or value it is referring to.
map <string , int > labelLookup;
bool start = 0;
bool starttext = 0;
bool labeldef = 0 ;
int address = 0;
while(getline(ifile,current))
{
bool useful = false;
for(int i = 0 ; i < current.size() ; i++)
{
if(isalpha(current[i]))
useful = 1;
}
if(!useful){
continue;
}
if(current == ".data"){
start = 1;
continue;
}
else if(current.size() == 0){
continue;
}
else if(current == ".text"){
starttext = 1;
start = 0;
continue;
}
else if(start == 1){ //.data portion has started
stringstream ss(current);
vector <int> data;
bool Word = 0, byte = 0;
string token;
string directive;
ss >> token;
ss >> directive;
if(directive == ".word"){
Word = true;
}
else if(directive == ".byte"){
byte = true;
}
if(token[token.size()-1] == ':'){
labeldef = 1;
token.pop_back();
data = extractint(current);
}
//write the array to memory (if at all it is an array ). , else write the single value or byte.
if(labeldef){
if(data.size() > 1){
labelLookup.insert(make_pair(token,address));
}
else{
labelLookup.insert(make_pair(token,data[0]));
}
if(Word == true){
for(int i=0; i < data.size(); i++){
memobject.writeWord(address,data[i]); //Word $$$$$
address += 4;
}
}
else if(byte == true){
for(int i=0; i < data.size(); i++){
memobject.writeByte(address,data[i]); //Word $$$$$
address += 1;
}
}
}
labeldef = 0;
}
else if(starttext == 1){
string insname,regname,label;
int i = 0;
while(!isalnum(current[i])) i++;
while(isalnum(current[i])){
insname.push_back(current[i]);
i++;
}
while(!isalnum(current[i])) i++;
while(isalnum(current[i])){
regname.push_back(current[i]);
i++;
}
while(!isalnum(current[i])) i++;
while( i < current.size() && current[i] != '#'){
if(current[i] == ' ' || current[i] == '\t'){
i++;
continue;
}
label.push_back(current[i]);
i++;
}
if( (insname == "la" || insname == "lw" || insname == "lb") && label[label.size()-1] != ')' ){
ofile<<"addi "<<regname<<",x0,"<<labelLookup[label]<<endl;
continue;
}
else
{
bool commline = 0;
for(int i = 0; i < current.size() ; i++)
{
if(current[i] == '#'){
if(i == 0 )
commline = 1;
break;
}
ofile<<current[i];
}
if(!commline)
ofile<<endl;
}
}
if(!start && !starttext){
for(int i = 0; i < current.size() && current[i] != '#' ; i++)
{
ofile<<current[i];
}
ofile<<endl;
}
}
ofile.close();
ifile.close();
}