-4

I need an array to store registers ($t0, $t1, ...) and if they're being used or not.

So far I got this, where the first field is the register name and the second field must be 0 or 1 if the register is being used ot not.

char* registers[2][10] = {
                      {"$t0", "0"},
                      {"$t1", "0"},
                      {"$t2", "0"},
                      {"$t3", "0"},
                      {"$t4", "0"},
                      {"$t5", "0"},
                      {"$t6", "0"},
                      {"$t7", "0"},
                      {"$t8", "0"},
                      {"$t9", "0"}
                      };

But I'm so lost right now because I don't know how to access to a register and check if it's being used or not, or how to set a register as used, etc..

  • only the compiler (or assembly writer) knows which register is being used. And which architecture? MIPS? – phuclv May 10 '18 at 03:30
  • Do you mean register in the sense of the processor register? Or are you calling these char *'s registers for some other purpose? If the later, in C code, you don't ever really think about registers or whether they are 'in use' or not. The compiler will take care of all of that (and have its own conventions, in concert with the processor architecture, about which registers are used for what purposes (e.g. caller-saved vs. callee-saved) and only it will "know" which ones are "in use". – Doug May 10 '18 at 04:21

1 Answers1

0

On the assumption, that you do not mean the registers of your hardware: (if you do mean them, see below)

I would rather build a struct of registers somehow like this:

struct register {
   int is_used;
   char* name;
}

To store and access them, an array would suffice perfectly:

struct register* registers;

Then you can access your "registers" for example like this:

registers[n].is_used //where n is the position of your register you want to refer

To access the hardware registers (I assume you mean the assembly registers rax, rbx etc.) you can use inline assembly (see here for linux and here for windows) No guarantee on the windows link, I do not program for windows systems

Then you can use the same strategy as above to store and access registers

Brahms
  • 11
  • 4