-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
238 lines (200 loc) · 8.41 KB
/
Copy pathmain.c
File metadata and controls
238 lines (200 loc) · 8.41 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "instruction-code-translator.h"
#include "radix-translator.h"
#include "mips-parser.h"
#include "pseudo-instruction-handler.h"
#include "mips-to-binary.h"
#define MAX_LENGTH 64
#define BINARY_LENGTH 32
#define HEX_LENGTH 8
#define OPCODE_LENGTH 6
#define RS_LENGTH 5
#define RT_LENGTH 5
#define RD_LENGTH 5
#define SHAMT_LENGTH 5
#define FN_LENGTH 6
#define IMMEDIATE_LENGTH 16
#define ADDRESS_LENGTH 26
#define INPUT_MIPS 0
#define INPUT_BINARY 1
#define INPUT_HEX 2
int getInputType(char*);
void binaryToMIPS(char*, char*);
int main() {
char userInput[MAX_LENGTH], binaryRep[BINARY_LENGTH + 1], hexRep[HEX_LENGTH + 1], mipsRep[MAX_LENGTH];
printf("\n------------------------\n");
printf("MIPS-BINARY-MIPS\n\n");
printf("With me, I can help you to:\n");
printf("1) Convert BINARY machine code to equivalent MIPS instruction\n");
printf("2) Convert HEX machine code to equivalent MIPS instruction.\n");
printf("3) Convert MIPS instruction to equivalent BINARY machine code.\n");
printf("4) Convert MIPS instruction to equivalent HEX machine code.\n");
printf("------------------------\n\n");
printf("Input BINARY/HEX/MIPS: ");
fgets(userInput, MAX_LENGTH, stdin);
if (userInput[strlen(userInput) - 1] == '\n') {
userInput[strlen(userInput) - 1] = '\0';
}
printf("\n------------------------\n\n");
printf("Your Input: %s\n", userInput);
int inputType = getInputType(userInput);
printf("Input Type: %s\n", inputType == INPUT_MIPS ? "MIPS" : inputType == INPUT_HEX ? "HEX" : "BINARY");
printf("\n------------------------\n\n");
printf("OUTPUT:\n\n");
switch (inputType) {
case INPUT_MIPS: {
ParsedInstruction parsed;
parseMipsInstruction(userInput, &parsed);
// Check if pseudo-instruction
if (isPseudoInstruction(parsed.instruction)) {
PseudoExpansion expansion;
expandPseudoInstruction(&parsed, &expansion);
printf("Pseudo-instruction detected: %s\n", parsed.instruction);
printf("Expanding '%s' into:\n\n", userInput);
// Display expansion with explanations
for (int i = 0; i < expansion.numExpanded; i++) {
printf(" %d) %s\n", i + 1, expansion.expandedInstructions[i]);
printf(" # %s\n", expansion.explanations[i]);
}
printf("\nBINARY EQUIVALENT:\n");
printf("HEX EQUIVALENT:\n");
// Convert each expanded instruction
for (int i = 0; i < expansion.numExpanded; i++) {
ParsedInstruction expandedParsed;
parseMipsInstruction(expansion.expandedInstructions[i], &expandedParsed);
char binary[33];
mipsToBinary(&expandedParsed, binary);
char hex[9];
binaryToHex(binary, hex);
printf(" %s: %s\n", expansion.expandedInstructions[i], binary);
printf(" %s: %s\n", expansion.expandedInstructions[i], hex);
if (expandedParsed.isLabel) {
printf(" Note: Label '%s' uses placeholder offset 0\n",
expandedParsed.operands[expandedParsed.numOperands - 1]);
}
}
} else {
// Regular instruction - convert directly
char binary[33];
mipsToBinary(&parsed, binary);
char hex[9];
binaryToHex(binary, hex);
printf("BINARY EQUIVALENT: %s\n", binary);
printf("HEX EQUIVALENT: %s\n", hex);
if (parsed.isLabel) {
printf("Note: Label '%s' uses placeholder offset 0\n",
parsed.operands[parsed.numOperands - 1]);
}
}
break;
}
case INPUT_BINARY:
binaryToHex(userInput, hexRep);
printf("HEX EQUIVALENT: %s\n", hexRep);
binaryToMIPS(userInput, mipsRep);
printf("MIPS INSTRUCTION : %s\n\n", mipsRep);
break;
case INPUT_HEX:
hexToBinary(userInput, binaryRep);
printf("BINARY EQUIVALENT: %s\n", binaryRep);
binaryToMIPS(binaryRep, mipsRep);
printf("MIPS INSTRUCTION : %s\n\n", mipsRep);
break;
}
printf("------------------------\n");
printf("Press ENTER key to exit\n");
getchar();
return 0;
}
int getInputType(char *input) {
if (strchr(input, ' ')) {
return INPUT_MIPS;
}
return strlen(input) == HEX_LENGTH ? INPUT_HEX : INPUT_BINARY;
}
void binaryToMIPS(char *binary, char *output) {
char opCode[OPCODE_LENGTH + 1];
for (int i = 0; i < OPCODE_LENGTH; i ++) {
opCode[i] = binary[i];
}
opCode[OPCODE_LENGTH] = '\0';
char instructionType = getInstructionType(opCode);
int offset = OPCODE_LENGTH;
switch (instructionType) {
case 'R': {
char rs[RS_LENGTH + 1], rt[RT_LENGTH + 1], rd[RD_LENGTH + 1], shamt[SHAMT_LENGTH + 1], fnCode[FN_LENGTH + 1], instruction[10];
for (int i = 0; i < RS_LENGTH; i ++) {
rs[i] = binary[offset + i];
}
rs[RS_LENGTH] = '\0';
offset += RS_LENGTH;
for (int i = 0; i < RT_LENGTH; i ++) {
rt[i] = binary[offset + i];
}
rt[RT_LENGTH] = '\0';
offset += RT_LENGTH;
for (int i = 0; i < RD_LENGTH; i ++) {
rd[i] = binary[offset + i];
}
rd[RD_LENGTH] = '\0';
offset += RD_LENGTH;
for (int i = 0; i < SHAMT_LENGTH; i ++) {
shamt[i] = binary[offset + i];
}
shamt[SHAMT_LENGTH] = '\0';
offset += SHAMT_LENGTH;
for (int i = 0; i < FN_LENGTH; i ++) {
fnCode[i] = binary[offset + i];
}
fnCode[FN_LENGTH] = '\0';
getInstruction(opCode, fnCode, instruction);
if (strcmp(instruction, "sll") == 0 || strcmp(instruction, "srl") == 0) {
snprintf(output, MAX_LENGTH, "%s $%d, $%d, %d", instruction, binaryToDecimal(rd), binaryToDecimal(rt), binaryToDecimal(shamt));
break;
}
snprintf(output, MAX_LENGTH, "%s $%d, $%d, $%d", instruction, binaryToDecimal(rd), binaryToDecimal(rs), binaryToDecimal(rt));
break;
}
case 'I': {
char rs[RS_LENGTH + 1], rt[RT_LENGTH + 1], immediate[IMMEDIATE_LENGTH + 1], instruction[10];
for (int i = 0; i < RS_LENGTH; i ++) {
rs[i] = binary[offset + i];
}
rs[RS_LENGTH] = '\0';
offset += RS_LENGTH;
for (int i = 0; i < RT_LENGTH; i ++) {
rt[i] = binary[offset + i];
}
rt[RT_LENGTH] = '\0';
offset += RT_LENGTH;
for (int i = 0; i < IMMEDIATE_LENGTH; i ++) {
immediate[i] = binary[offset + i];
}
immediate[IMMEDIATE_LENGTH] = '\0';
getInstruction(opCode, NULL, instruction);
if (strcmp(instruction, "lw") == 0 || strcmp(instruction, "sw") == 0) {
snprintf(output, MAX_LENGTH, "%s $%d, %d($%d)", instruction, binaryToDecimal(rt), binaryToTwoComplement(immediate), binaryToDecimal(rs));
break;
}
if (strcmp(instruction, "beq") == 0 || strcmp(instruction, "bne") == 0) {
snprintf(output, MAX_LENGTH, "%s $%d, $%d, %d", instruction, binaryToDecimal(rs), binaryToDecimal(rt), binaryToTwoComplement(immediate));
break;
}
snprintf(output, MAX_LENGTH, "%s $%d, $%d, %d", instruction, binaryToDecimal(rt), binaryToDecimal(rs), binaryToTwoComplement(immediate));
break;
}
case 'J': {
char address[ADDRESS_LENGTH + 1], addressHex[9], instruction[10];
for (int i = 0; i < ADDRESS_LENGTH; i ++) {
address[i] = binary[offset + i];
}
address[ADDRESS_LENGTH] = '\0';
getInstruction(opCode, NULL, instruction);
binaryToHex(address, addressHex);
snprintf(output, MAX_LENGTH, "%s 0x%s", instruction, addressHex);
break;
}
}
}