Converting text from file into operations

I am writing a program that simulate an assembly language (MIPS). What it does is read binary strings from a file and then convert those into instructions and then simulate those instructions. I have gotten to where I can recognize those instructions and have put them in a struct container. But I am stuck on how to simulate those instructions. I have variables set for the MIPS registers and not sure how to change the values.

For example if I have this binary string

001001 00000 00010 00000 00000 000101

which says add 5 to the v0 variable

How can I make that happen?
Depends on how you want to simulate it. Guess we'd need to see more code to really know what you're asking.

Are you asking how to convert the string "000101" to 5? Starting in C++11, you can do this with std::stoi.
1
2
3
4
5
6
7
8
9
10
11
12
13
// Example program
#include <iostream>
#include <string>

int main()
{
    int i = std::stoi("00000101", nullptr, 2);
    std::cout << i << std::endl;
    
    int v0 = 0;
    
    v0 += i; // add 5 to the v0 variable
}


Since there's a finite number of registers in MIPS (32, 5-bit), I would just make an array of registers with values

int32_t registers[32];

Edit: Also, good luck! Seems like to fun to write your own MIPS "interpreter".
Last edited on
Thank you for the array tip, I think I know what to do now.
Topic archived. No new replies allowed.