Hello,
I am coding a MIPS processor in C++ and I am taking in instructions from a text file. I need to convert the stored strings within my string array to characters so I can search for the instruction within the string.
int instruction = 00000000000000000000000000000000; // 32 bit number
int reg1 = 00000; // register a
int reg2 = 00000; // register b
int writebackreg = 00000; // write back register
//int fetch(int instruction);
char * p[1000];
string textinput[1000]; // taking in the whole input text file
string opcode[26]; // gonna hold the opcodes received from text
int registers[26]; // organizing the registers
cout << " \n initializing instuctions into queue \n ";
fstream myfile;
myfile.open("Assignment5_input.txt"); // opening the text file
if(!myfile) // if file cant open
{
cout << " \n cannot find file, please load the correct file first. \n";
return 1;
}
for(int i = 0; i < lines; i++) // printing values from text file
{
getline(myfile, textinput[i]);
cout << "line "<< i <<" "<< textinput[i] << endl;
}
cout << " organizing instructions received \n";
for(int i = 0; i < lines; i ++)
{
textinput[5];
if(textinput[i] == op1)
{
//strcpy(p[i], textinput[i]);
if(opcode[i] == " ")
{
i = i + 1;
}
}
}
}
int fetch(int x)
{
int instruction;
cout << " taking in the first result " << endl;
return instruction;
}
would anyone be able to help me implement an algorithm to help convert? or recommend an alternative way to look at this.
- before i tried storing the text files into a character array but was running into issues.
strings pretty much are character arrays (wrappers around dynamically allocated arrays with some bells and whistles).
str[0] is the first character.
You don't need to use raw char arrays at all if you're already using strings. No need to use strcpy, just copy the std::strings by assignment.
so I can search for the instruction within the string
You want to search for a string within another string? You can also do that.
e.g.
1 2 3 4 5 6 7 8 9 10 11 12 13
// Example program
#include <iostream>
#include <string>
int main()
{
std::string line = "addi $s4, $s0, 12";
if (line.find("addi") != std::string::npos)
{
std::cout << "\"addi\" substring found\n";
}
}
Also MIPS has 32 general-purpose, 32-bit registers. I'm curious where the 26 comes from? The alphabet?
int instruction = 00000000000000000000000000000000; // 32 bit number
Careful with this... the compiler has no idea you're representing bits instead of base-10 numbers. C++ supports 0b prefixes on binary int literals, e.g. 0b01100 is 12(base-10).
(Of course, 0 happens to be 0 in any base, so it doesn't matter in this instance)
You can mix and match, but it is not generally recommended. The biggest reason to go C at it is to do binary file or network packet type serialization.
char* cp = &strvar[0]; //works, but if you modify cp you break strvar and enter undefined behavior. this can override the const char* vs not const problem using some old code though. If the old code modifies it, you are in trouble.
const char* cp = strvar.c_str(); //MUCH safer.
there isn't much you cannot do with string that you can do with char* and I strongly urge you to check out doing it as a string and asking if you can't see how to do it with that tool.