was working with @belle on some assembly required, and this funny RISC-V assembly fact came up today!
some background:
• RISC-V is a simple assembly language made for academic use
• when you want to save numbers for later use in assembly, you save them in something called a register
so here’s the funny bit. let’s say you want to store a number into a register! should be easy, right? wouldn’t it be nice if you could do something like:
# Load the number 30 into register 18
LOAD x18, 30
well, you totally can’t!!! which confused me for awhile! but it turns out, if you use an unrelated command in a funny way, you can get the same effect.
enter the `ADD` instruction. `ADD` adds a number from *one register* to a *second register* and saves them to *third register*.
# Add the contents of register 2 to register 3 and save that into register 1
ADD x1, x2, x3
now let’s enter the `ADDI` instruction. it stands for `add immediate`, which adds a number from *one register* to a *number* you provide and saves that to a *second register*.
# Add the contents of register 2 to the number 30 and save that into register 1
ADDI x1, x2, 30
now let’s introduce our final actor, the `x0` register. this register is special - it’s a 0 constant, meant to be used whenever you need a 0. why not just use a number 0? great question - often, these commands require registers, so this register can act as 0 for us whenever we need it. for example, in the case of `ADDI`, at least one of the numbers to add has to be from a register.
now that we have all this set up, here’s what we can do.
# Add 30 to the contents of register zero, or x0, and save that into register x18
ADDI x18, zero, 30
so, to repeat that: let’s add the contents of register 0 (which is 0) to 30, and add that into register 18.
30 + 0 = 30, save that into register 18.
we did it!!! 🎉
anyways, come hang out with me in #some-assembly-required, where development of some assembly required is happening!