0

I'm writing a MIPS simulator in C++. It takes in a text file in hex, and needs to output the register contents and the decoded instruction after every instruction. So I convert the hex file to binary to grab the opcodes, rs, rt, rd, funct, and sham field so I can figure out what instruction it is.

I'm stuck because I've come the SW and LW instructions and I'm not sure how to handle simulating the memory. I have my registers in an array[0-31] and as well as my instructions[whatever number of instructions read in], and words[whatever number or words read in].

But I'm not sure how to simulate LW and SW... I know how to encode/decode them, but the memory part (along with the offset) is throwing me off. Any suggestions? Thanks in advance for any help.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
  • Well, words in MIPS are 32 bits so why not just an array of `int32_t`? Then LW and SW become reading and writing to array indices respectively. – Sinkingpoint Feb 06 '15 at 23:41
  • @Quirliom So if I just declared an array of int32_t with a size of say 4000, whenever i have an instruction that is like SW $v0,8($gp) it would just save it at the "address" of $gp in the array index 8? – CoderRightInTheProgram Feb 07 '15 at 00:39
  • Have an array of `int_32t`s. When you get a `SW $v0,8($gp)` store the value of v0 at the index `$gp + 8` – Sinkingpoint Feb 07 '15 at 01:21

1 Answers1

2

If the contents are dense and/or speed is of the essence, and you have plenty of memory in the environment you're simulating in, you could just use a big honkin' array (just like you're doing with the registers - but bigger).

If the contents are sparse, and/or you don't have a boatload of memory in the environment in which you're running the simulator, and speed is less significant than space, you could use an associative array.

This question references a couple of C libraries for associative arrays:

Associative arrays in C

Community
  • 1
  • 1
Timothy Johns
  • 1,075
  • 7
  • 17
  • If you want to see how someone else has done it, you could take a look at Nachos: http://homes.cs.washington.edu/~tom/nachos/, in particular Machine::ReadMem in code/machine/translate.cc and Machine::OneInstruction (the cases for OP_LW and OP_SW), in mipssim.cc – Timothy Johns Feb 07 '15 at 00:03
  • I have a lot of memory, so I'm leaning toward a big array. I guess I'm just a little confused. If I "store" say $s1 into memory with offset 0, it would store it in the first index right? What if I then need to store $s2 into memory with offset 0 as well, won't that overwrite the first? – CoderRightInTheProgram Feb 07 '15 at 00:43