DarwinBotsManual
Open the app EspañolES

Numbers and addresses

Which numbers you can write in the DNA, how memory is read with the asterisk, why a sysvar is just an address, and what happens with out-of-range values and addresses.

In the DNA, everything is a number. A quantity, a memory address and the name of a sysvar all end up as integers on the integer stack. This page covers which numbers you can write, how memory is read and written, and what the engine does when a value or an address goes out of range.

Literals

A number written in the DNA is pushed as is: 50, -5, 32000.

This literal makes the bot fail to load:

cond
start
  40000 50 store
stop

If you need a big number, build it with operations: 200 200 mult leaves 40000 on the stack with no problem.

A sysvar is an address

Each bot's memory has 1000 cells, numbered 1 to 1000. Some of them have a name: those are the sysvars. .up is cell 1, .nrg is cell 310. Writing .up in the DNA is exactly the same as writing 1: it pushes the address, not the contents.

That is why 10 .up store works: it pushes 10, pushes 1, and store writes 10 to cell 1. And that is why this bot stores the number 310 in cell 51, the address of the energy, and not the energy itself:

cond
start
  .nrg 51 store
stop

The cells without a name are free memory for whatever you like (see Free and epigenetic memory). With Variables with def you give them a name of your own. The full list of sysvars is in the sysvar reference, for example .nrg.

Reading memory: the asterisk

To push the contents of a cell, put an asterisk in front:

You writeIt pushes
50the number 50
*50whatever is in cell 50
.nrgthe number 310
*.nrgwhatever is in cell 310: the bot's energy

If you work out the address on the spot, use the separate * operator: it pops an address from the top and pushes the contents of that cell. 50 * is the same as *50, but it also works for *.nrg 10 div *, where the address comes out of a calculation.

This bot copies whatever is in cell 50 to cell 60:

cond
start
  *50 60 store
stop

Addresses outside 1 to 1000

From the DNA there is no way to read or write outside the 1000 cells. Every address, whether reading or writing, goes through the same rule:

  1. Take the absolute value (the sign is ignored).
  2. Take the remainder of dividing by 1000.
  3. If the remainder is 0, the cell is 1000.
Address requestedCell used
5050
105050
−5151
205252
1000, 20001000
0 when reading (*0, 0 *)1000
0 when writingnone (see below)

This bot writes 7, 8 and 9 to cells 50, 51 and 52 even though the addresses look like something else:

cond
start
  7 1050 store
  8 -51 store
  9 2052 store
stop

The Bestiary bot “Alga_Pair 1.1.1” takes advantage of the rule: it defines variables such as ninjaShoot at 1007 or ninjaUp at 1001, which in memory are .shoot (7) and .up (1) under another name. This bot moves even though it never writes .up:

def ninjaup 1001

cond
start
  10 .ninjaup store
stop

Writing to address 0

A store whose address is exactly 0 writes nothing and costs no energy. With store, addstore, substore and multstore, moreover, the value stays on the stack unconsumed. Here the store to 0 does nothing, the 7 stays on the stack and the second store puts it in cell 58:

cond
start
  7 0 store
  58 store
stop

The same happens if the 0 comes out of a calculation (5 5 sub store). The details of each store are in Writing to memory.

Arithmetic and clipping to ±32000

Memory holds values from −32000 to 32000, but the integer stack doesn't: there numbers can grow to about two billion. mult saturates at that limit and add and sub wrap around, but in practice you won't get there. A large intermediate result is no problem as long as you shrink it before storing it: 300 300 mult 300 div gives 300, even though in the middle the stack held 90000.

The clipping happens when you write. A store doesn't saturate: it keeps the remainder of dividing by 32000, preserving the sign. The only exception is exact multiples of 32000, which end up as 32000 (or −32000). That way a store never writes 0 unless the value is 0.

Value on the stackEnds up in memory as
3200032000
32500500
32767767
60000 (30000 30000 add)28000
6400032000
90000 (300 300 mult)26000
−40000−8000

This bot stores 28000, not 32000:

cond
start
  30000 30000 add 50 store
stop

The same clipping applies to inc and dec: a counter at 32000 that goes up by one becomes 1, not −32000; one at −32000 that goes down becomes −1. If you are counting something that can grow a lot, check it before it reaches the cap.

Some stores (such as divstore or sqrstore) don't apply the clipping because their result already falls within range; they are covered in Writing to memory.

Negative numbers

Negatives are ordinary citizens of the DNA, with a couple of details:

Several sysvars accept negatives: the .timer of a founder bot, for example, starts at a random value between −32000 and 32000. The range of each one is in its entry in the reference, and the arithmetic operators one by one are in Operators by family.

Summary

WhatRule
Literalfrom −32768 to 32767; outside that the bot doesn't load
Decimalrounds to the nearest integer, to the even one on a tie
Unknown wordis 0
.sysvarits address (a number from 1 to 1000)
*n, *.sysvar, n *the contents of the cell
Address out of rangeabsolute value, remainder of dividing by 1000, 0 → 1000
Store to address 0does nothing
Values on the stackup to about ±2 billion
Value when storingremainder of dividing by 32000, with sign; exact multiples → ±32000