Difference between revisions of "Advanced IC10 Programming"
From Unofficial Stationeers Wiki
(→Flow Control And Data Manipulation ==) |
|||
Line 1: | Line 1: | ||
− | = Flow Control And Data Manipulation | + | = Flow Control And Data Manipulation = |
''NOTE: This level of coding is NOT needed to play the to game to it's full extent. it's aimed at people looking for custom, self-inspired challenges.'' | ''NOTE: This level of coding is NOT needed to play the to game to it's full extent. it's aimed at people looking for custom, self-inspired challenges.'' | ||
Revision as of 12:18, 27 March 2019
Contents
Flow Control And Data Manipulation
NOTE: This level of coding is NOT needed to play the to game to it's full extent. it's aimed at people looking for custom, self-inspired challenges.
Functions
A function is just a label we jump to, but using the -al version of branch instructions (jal, beqzal) as they store the return address into the special ra register. When calling a function from another function (or from itself), ra will be overwritten so we have to store it in the stack and take it out when the function completes:
If we hadn't used the stack, the jump at line 10 would take us to line 9 instead of 4.
Loops
The challenge with loops is to keep them clean. Relative jumps are a great way to save lines but they can quickly turn code messy and adding lines might break them if you overlook them. If code length is not an issue, stick to labels where possible.
Indexing With Relative Jump
Use this to set one or more parameters or call functions using an index number you calculated (or read from a dial button). We multiply it by 2 because we need space for a jump after each move command so we don't run the subsequent ones and we add 1 because for a 0 index "jr 0" would lock up our IC.
Redefining Aliases
Say you have a complex function that manipulates a device (reads/writes multiple parameters, controls timing etc) and you want to reuse it for multiple devices. You could copy paste the whole function, or you could redefine the alias you used and point it to another device, like this:
NOTE: This wouldn't work in real languages since aliases and defines are just directives for the (pre)compiler, they're not evaluated during runtime by the CPU
You can combine this with a loop and indexing to run your function on multiple devices automatically.