The address register (ra) is not meant to be written to by the user, it's only used for branching to code that needs to be accessed from multiple areas in your code (functions).
Registers are what most programming languages call variables. But where most programming languages let you create as many variables as you want, MIPS has only 18 registers, and only 16 of them are intended for regular use.
Registers are for storing temporary data. They can be overwritten any time you want.
Think of a register as a shoe-box. You write something on a piece of paper and store it in the box, you can open the box and look at what's written on the note, and you can write a new note and put it in there, but then you have to take out the old note. The shoe-box can only ever hold one note.
In game-terms, registers are where you store things like pressure from a Gas Sensor, the horizontal angle of the Sun, etc.
In MIPS, two of the functions you'll use the most are ***l*** and ***s***. ***s*** also has a lot of logical comparators you can use to do boolean operations on the input.
Boolean comparisons are comparisons made to be either true (1) or false (0). These are great for controlling the on/off state of devices like pumps and lights etc.
If you use one of the operators that compare to zero, you do not need to supply two values for comparison, since the instruction already says one of the values will be zero.
When you want to automate something, the Stationpedia is your best friend! It contains a catalogue of all the things you can make, and lists all the data you can *load from* and/or *set to* it. Here's an example:
The section we're interested in here is the ***Logic*** section. As you saw in the Setting-example above, I set the ***On*** end-point to the value stored in a register. As we can see in the picture above, ***On*** can be read (***l***oaded) or written (***s***et). ***On*** can take a 1 to turn on, or a 0 to turn off.
Aha! But setting Power could be like making a dimmer for the light, right? That would have been nice, but that would more likely be done with an end-point called ***Setting***, which this variant of light doesn't have. To my knowledge, there are no dimmable lights in Stationeers (yet).
In Stationeers, everything happens in "ticks", and a single tick is 0.5 seconds. But the code we write in ICs can execute much (***much!***) faster than once per tick. But looping over the code many times in a single tick doesn't make it work better, it just "burns resources".
What happens is, each MIPS script is allowed 128 lines of exection per tick. If your script executes one loop totalling 70 lines, you will go through almost two loops in one tick, then the execution is halted until the next tick. If you execute 70 lines in a loop, then you execute the remaining 58 lines this tick, and the script will be at line 59 of the loop when it resumes next tick. This can have unintended consequences. Plus, if you read the pressure from a Gas Sensor 30 times in a tick, the pressure will be the same every time since pressure is only updated by the game *once per tick*.
The solution is to begin or end the loop with `yield`! It doesn't really matter if you put the `yield` as the first or last line in your loop, as long as there is at least one `yield` executed every single tick. This ensures that you start execution at a controlled location next tick.
While the MIPS implementation in Stationeers has some limitations (128 lines, with a maximum of 52 characters per line), I've rarely run into problems like running out of room. This is mostly because I try not to write "multi-mega-scripts" that do a ton of things. I'm a fan of the UNIX mantra of "do one thing, and do it well". This also means that I use ***aliases*** and ***defines*** a lot. They take more space than just writing things straight up, but they also make the code much easier to read!
If you're juggling 10+ registers in your code without naming them, the chances of getting them mixed up increases greatly. And if you do make a mistake somewhere, trying to follow the code can be more complicated than if you used named variables.
This is not to say that you *have* to name variables. It's up to you.
Making an ***alias*** is the act of giving a register or a device a more human-readable name. For devices, this has the added benefit of naming the pins on the IC housing, making it easier to remember what devices goes on what pin when you set up the housing.
This gives the device on pin ***d0*** the ***alias*** (name) ***Furnace***, and the register ***r0*** the ***alias*** (name) ***FurnaceTemperature***. In the code you can now refer to Furnace instead of d0, and FurnaceTemperature instead of r0. Like this:
A define is what we in other programming languages would call a constant. As the name implies, a constant is ... constant. It never changes. This is great for threshold-values and item-hashes (a unique number given to each type of item in the game, used for comparing items in sorting, and for reading from or writing to a lot of identical items, called batch-reading/-writing).
This ***define***s ***MinimumTemperature*** as ***20*** (maybe for use as 20 Celsius in a temperature control circuit), and ***define***s ***KelvinConvert*** (the conversion-number from Kelvin to Celsius) as ***273.15***.
There is also logic for relative jumping in code, and it works by adding an 'r' after the 'b' when branching (so 'b' becomes 'br'). Instead of giving the branch a label to go to, you tell it the number of lines to jump (positive number jumps forwards, negative number jumps backwards).
</td><tdclass="col2">b-</td><tdclass="col3">b-al</td><tdclass="col4">s-</td></tr><trclass="row2"><thclass="col0">Description</th><thclass="col1">Branch to line</th><thclass="col2">Branch to line and store return address</th><thclass="col3">Set register</th></tr><trclass="row3"><thclass="col0"><none></th><tdclass="col1">unconditional</td><tdclass="col2">j</td><tdclass="col3">jal</td><tdclass="col4">s</td></tr><trclass="row4"><thclass="col0">-eq</th><tdclass="col1">if a == b</td><tdclass="col2">beq</td><tdclass="col3">beqal</td><tdclass="col4">seq</td></tr><trclass="row5"><thclass="col0">-eqz</th><tdclass="col1">if a == 0</td><tdclass="col2">beqz</td><tdclass="col3">beqzal</td><tdclass="col4">seqz</td></tr><trclass="row6"><thclass="col0">-ge</th><tdclass="col1">if a >= b</td><tdclass="col2">bge</td><tdclass="col3">bgeal</td><tdclass="col4">sge</td></tr><trclass="row7"><thclass="col0">-gez</th><tdclass="col1">if a >= 0</td><tdclass="col2">bgez</td><tdclass="col3">bgezal</td><tdclass="col4">sgez</td></tr><trclass="row8"><thclass="col0">-gt</th><tdclass="col1">if a > b</td><tdclass="col2">bgt</td><tdclass="col3">bgtal</td><tdclass="col4">sgt</td></tr><trclass="row9"><thclass="col0">-gtz</th><tdclass="col1">if a > 0</td><tdclass="col2">bgtz</td><tdclass="col3">bgtzal</td><tdclass="col4">sgtz</td></tr><trclass="row10"><thclass="col0">-le</th><tdclass="col1">if a ⇐ b</td><tdclass="col2">ble</td><tdclass="col3">bleal</td><tdclass="col4">sle</td></tr><trclass="row11"><thclass="col0">-lez</th><tdclass="col1">if a ⇐ 0</td><tdclass="col2">blez</td><tdclass="col3">blezal</td><tdclass="col4">slez</td></tr><trclass="row12"><thclass="col0">-lt</th><tdclass="col1">if a < b</td><tdclass="col2">blt</td><tdclass="col3">bltal</td><tdclass="col4">slt</td></tr><trclass="row13"><thclass="col0">-ltz</th><tdclass="col1">if a < 0</td><tdclass="col2">bltz</td><tdclass="col3">bltzal</td><tdclass="col4">sltz</td></tr><trclass="row14"><thclass="col0">-ne</th><tdclass="col1">if a != b</td><tdclass="col2">bne</td><tdclass="col3">bneal</td><tdclass="col4">sne</td></tr><trclass="row15"><thclass="col0">-nez</th><tdclass="col1">if a != 0</td><tdclass="col2">bnez</td><tdclass="col3">bnezal</td><tdclass="col4">snez</td></tr><trclass="row16"><thclass="col0">-dns</th><tdclass="col1">if d? is not set</td><tdclass="col2">bdns</td><tdclass="col3">bdnsal</td><tdclass="col4">sdns</td></tr><trclass="row17"><thclass="col0">-dse</th><tdclass="col1">if d? is set</td><tdclass="col2">bdse</td><tdclass="col3">bdseal</td><tdclass="col4">sdse</td></tr><trclass="row18"><thclass="col0">-ap</th><tdclass="col1">if a ~ b</td><tdclass="col2">bap</td><tdclass="col3">bapal</td><tdclass="col4">sap</td></tr><trclass="row19"><thclass="col0">-apz</th><tdclass="col1">if a ~ 0</td><tdclass="col2">bapz</td><tdclass="col3">bapzal</td><tdclass="col4">sapz</td></tr><trclass="row20"><thclass="col0">-na</th><tdclass="col1">if a !~ b</td><tdclass="col2">bna</td><tdclass="col3">bnaal</td><tdclass="col4">sna</td></tr><trclass="row21"><thclass="col0">-naz</th><tdclass="col1">if a !~ 0</td><tdclass="col2">bnaz</td><tdclass="col3">bnazal</td><tdclass="col4">snaz</td></tr></tbody></table>
Since the maximum number of devices an IC Housing can connect is 6 (d0-5), this would break the script. This is why multi-level referencing should be used extremely sparingly.