DarwinBotsManual
Open the app EspañolES

Basic

The arithmetic of DNA (addition, subtraction, product, division, remainder, sign, randomness) and the tools for rearranging the integer stack.

Cost: Basic command

OperatorWhat it does
addAdds the top two numbers of the integer stack and leaves the result.
subSubtracts the top number of the stack from the one below it: a b sub leaves a − b.
multMultiplies the top two numbers of the integer stack; if the product goes over 2 billion, it stays at that cap.
divDivides 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.
rndReplaces 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.
modLeaves the remainder of dividing the lower number by the upper one, with the sign of the dividend; with divisor 0 it gives 0.
sgnReplaces the top number of the stack with its sign: −1 if negative, 0 if zero and 1 if positive.
absReplaces the number on top of the stack with its absolute value: it strips the sign.
dupDuplicates the number on top of the integer stack, to use it twice without reading or computing it again.
dropPops the number on top of the integer stack and discards it.
clearEmpties the integer stack in one go; it does not touch the boolean stack.
swapSwaps the top two numbers of the integer stack: a b swap leaves b a.
overCopies 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.

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
stop

Every basic operator executed costs Basic command (free under the F1 rules). The overview of all the families is in Operators by family.