Skip to content

A character from a string

A string is read by address too: a character index instead of a slot number, a character code instead of a value

mediumYou need: The memory cell, Print Char

The third thing read can read is an ordinary string. The address here is a character index, and the answer is its code, a number:

read code "mlog" 0

code gets 109 — that is m. Not a letter, a number: mlog has no separate “character” kind at all.

tick 0
Read0
=
Print Char1
char
Operation2
=
Jump -> 03
if
Print Flush4
Stop5
@counter0number
codenullnull
indexnullnull

The program walks the string character by character and puts them back together with printchar. The board shows mlog — the same string, only assembled one character at a time.

Strings in mlog are almost immovable: they cannot be concatenated, compared or trimmed. The only things you can do with one are print it whole and take it apart character by character. Hence the uses:

  • print text letter by letter — a crawling line on a board, a letter per iteration;
  • break a string into codes and store them in memory, to reassemble them in another order later;
  • fetch a single character out of a prepared set: read code "0123456789" digit turns a number into the code of its digit.
Address What happens
0 … length minus one the character’s code
length and above emptiness
negative emptiness
fractional truncated: 2.9 is character 2

There is no way to find out a string’s length — the language has no “how many characters” instruction. So a loop over a string either knows the length in advance or runs until the first emptiness:

read code "mlog" index
jump 5 strictEqual code null
printchar code
op add index index 1
jump 0 always
printflush message1

Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.