-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
209 lines (189 loc) · 4.73 KB
/
helpers.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "helpers.h"
char *intToBinary(int n, int bits) {
char *binary = malloc(bits * sizeof(char));
if (binary == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(1);
}
for (int i = bits - 1; i >= 0; i--) {
binary[i] = (n & 1) + '0';
n >>= 1;
}
return binary;
}
char *removeLabelFromInstruction(char *instruction) {
// label1: instruction returns instruction
char *token = strchr(instruction, ':');
if (token == NULL) {
return instruction;
}
size_t len = strlen(token + 1);
char *result = (char *)malloc(len + 1);
if (result == NULL) {
return NULL;
}
strcpy(result, token + 1);
return result;
}
char *binaryToHexadecimal(char *binaryInstruction) {
char *hexadecimal = malloc(8 * sizeof(char));
if (hexadecimal == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(1);
}
int decimal = (int) strtol(binaryInstruction, NULL, 2);
sprintf(hexadecimal, "0x%08X", decimal);
return hexadecimal;
}
int getFunctBinaryString(char *opcode) {
if(strncmp(opcode, "sub", 3) == 0) {
return 34;
}
else if(strncmp(opcode, "and", 3) == 0) {
return 36;
}
else if(strncmp(opcode, "or", 2) == 0) {
return 37;
}
else {
return -1;
}
}
int getRegisterInt(char *regist) {
if(strncmp(regist, "$zero", 5) == 0) {
return 0;
}
else if(strncmp(regist, "$at", 3) == 0) {
return 1;
}
else if(strncmp(regist, "$v0", 3) == 0) {
return 2;
}
else if(strncmp(regist, "$v1", 3) == 0) {
return 3;
}
else if(strncmp(regist, "$a0", 3) == 0) {
return 4;
}
else if(strncmp(regist, "$a1", 3) == 0) {
return 5;
}
else if(strncmp(regist, "$a2", 3) == 0) {
return 6;
}
else if(strncmp(regist, "$a3", 3) == 0) {
return 7;
}
else if(strncmp(regist, "$t0", 3) == 0) {
return 8;
}
else if(strncmp(regist, "$t1", 3) == 0) {
return 9;
}
else if(strncmp(regist, "$t2", 3) == 0) {
return 10;
}
else if(strncmp(regist, "$t3", 3) == 0) {
return 11;
}
else if(strncmp(regist, "$t4", 3) == 0) {
return 12;
}
else if(strncmp(regist, "$t5", 3) == 0) {
return 13;
}
else if(strncmp(regist, "$t6", 3) == 0) {
return 14;
}
else if(strncmp(regist, "$t7", 3) == 0) {
return 15;
}
else if(strncmp(regist, "$s0", 3) == 0) {
return 16;
}
else if(strncmp(regist, "$s1", 3) == 0) {
return 17;
}
else if(strncmp(regist, "$s2", 3) == 0) {
return 18;
}
else if(strncmp(regist, "$s3", 3) == 0) {
return 19;
}
else if(strncmp(regist, "$s4", 3) == 0) {
return 20;
}
else if(strncmp(regist, "$s5", 3) == 0) {
return 21;
}
else if(strncmp(regist, "$s6", 3) == 0) {
return 22;
}
else if(strncmp(regist, "$s7", 3) == 0) {
return 23;
}
else if(strncmp(regist, "$t8", 3) == 0) {
return 24;
}
else if(strncmp(regist, "$t9", 3) == 0) {
return 25;
}
else if(strncmp(regist, "$k0", 3) == 0) {
return 26;
}
else if(strncmp(regist, "$k1", 3) == 0) {
return 27;
}
else if(strncmp(regist, "$gp", 3) == 0) {
return 28;
}
else if(strncmp(regist, "$sp", 3) == 0) {
return 29;
}
else if(strncmp(regist, "$fp", 3) == 0) {
return 30;
}
else if(strncmp(regist, "$ra", 3) == 0) {
return 31;
}
else {
return -1;
}
}
char *replaceSubstring(const char *str, const char *oldSubstr, const char *newSubstr) {
char *result = NULL;
const char *start = str;
const char *found = strstr(start, oldSubstr);
size_t oldLen = strlen(oldSubstr);
size_t newLen = strlen(newSubstr);
if (!found) {
result = strdup(str);
} else {
size_t count = 0;
while (found) {
count++;
start = found + oldLen;
found = strstr(start, oldSubstr);
}
size_t resultLen = strlen(str) + (newLen - oldLen) * count + 1;
result = (char *)malloc(resultLen);
if (!result) {
perror("Memory allocation failed");
exit(1);
}
char *dest = result;
start = str;
found = strstr(start, oldSubstr);
while (found) {
size_t len = found - start;
strncpy(dest, start, len);
dest += len;
strcpy(dest, newSubstr);
dest += newLen;
start = found + oldLen;
found = strstr(start, oldSubstr);
}
strcpy(dest, start);
}
return result;
}