Basic
The arithmetic of DNA (addition, subtraction, product, division, remainder, sign, randomness) and the tools for rearranging the integer stack.
Cost: Basic command
| Operator | What it does |
|---|---|
add | Adds the top two numbers of the integer stack and leaves the result. |
sub | Subtracts the top number of the stack from the one below it: a b sub leaves a − b. |
mult | Multiplies the top two numbers of the integer stack; if the product goes over 2 billion, it stays at that cap. |
div | Divides the lower number by the top one and rounds to the nearest integer (to the even one on a tie); dividing by zero gives 0. |
rnd | Replaces the top number of the stack with a random one between 0 and that number, both included. |
* | Reads memory at a computed address: pops an address from the stack and pushes what is in that cell. |
mod | Leaves the remainder of dividing the lower number by the upper one, with the sign of the dividend; with divisor 0 it gives 0. |
sgn | Replaces the top number of the stack with its sign: −1 if negative, 0 if zero and 1 if positive. |
abs | Replaces the number on top of the stack with its absolute value: it strips the sign. |
dup | Duplicates the number on top of the integer stack, to use it twice without reading or computing it again. |
drop | Pops the number on top of the integer stack and discards it. |
clear | Empties the integer stack in one go; it does not touch the boolean stack. |
swap | Swaps the top two numbers of the integer stack: a b swap leaves b a. |
over | Copies the second number of the integer stack to the top: a b over leaves a b a. |
The basic operators come in two kinds, and all of them work only with the integer stack: they pop their operands from there and leave the result there.
- Calculations:
add,sub,mult,div,mod,sgn,absandrnd, plus*, which reads memory at a computed address. - Stack handling:
dup,drop,swap,overandclear. They calculate nothing, they just rearrange what is there.
The ones you will write most are add, sub, mult and div, almost always to adjust a reading before storing it (*.nrg 10 div), and rnd to give the bot some randomness. The stack ones are for when a value is needed twice: dup saves you from reading the same cell twice.
As everywhere in DNA, the order is operands first: a b sub is a − b, and the one lower in the stack is the first operand. None of them fails: with an empty stack they work with zeros. And the integer stack holds much bigger numbers than memory does, so an intermediate result of 90000 is no problem as long as you shrink it before storing it (see Numbers and addresses).
One usage idea that combines several: a counter that goes from 0 to 9 and starts over, kept in cell 50, and a gene that takes advantage of the wraparound to do something every 10 cycles.
cond
start
*50 1 add 10 mod 50 store
stop
cond
*50 0 =
start
1256 rnd .setaim store
stopEvery basic operator executed costs Basic command (free under the F1 rules). The overview of all the families is in Operators by family.