You might have noted my new infatuation with the hacking game Exapunks by Zachtronics. Being a programmer myself I could not help but write up some coding standards for Exapunks' programming language “Axiom”. 😎 Oh, and some loop patterns which might get you kickstarted on your next hack.

Coding standards

Even though the Axiom language seems not to be overly complex, it pays to have a proper coding style:

  • Use NOTE for comments. This will help you later on to identify the phase your EXA is supposed to be in.
  • For jump labels use descriptive names like READ_FILE or RECEIVE_DATA.
  • If you use indentation, indent by two spaces.
  • Add indentation after MARK and @REP.
  • Reset indentation after TJMP, FJMP, JUMP – and before @END.
  • Remember to use a blank line once in a while to improve readability.

So this…

GRAB 199
MARK READING
TEST F = X
SEEK 2
FJMP READING
SEEK -2
COPY F X

…becomes this:

NOTE X IS ENTRY WE ARE SEARCHING FOR
GRAB 199
MARK READING
  TEST F = X
  NOTE ADVANCE FILE HEADER TO NEXT LINE
  SEEK 2
  FJMP READING

NOTE GET THE NEXT ENTRY FROM FILE
SEEK -2
COPY F X

Loop patterns

Because Exapunks' Axiom language is very basic, I found some patterns for me which emulate constructs from other programming languages.

Loop with exit condition

As there are no WHILE loops, a combination of TEST and jump labels will do the trick:

COPY 10 X
MARK WHILE
  NOTE DO STUFF
  SUBI X 1 X
  TEST X > 1
  TJMP WHILE

Remember: If T evaluates to 0, the next TJMP / FJMP instruction will pick this up. Above listing could be written as:

COPY 10 T
MARK WHILE
  NOTE DO STUFF
  SUBI T 1 T
  TJMP WHILE

Obviously this only makes sense if there are more cycles to perform than lines of code needed for this construct, and/or you do not know in advance how many times the loop will be performed. Otherwise you might want to write out loops:

Written-out loops

As “Trash World News” states in its November 1997 issue: Sometimes it is faster not to use loops. This loop will need almost 10 cycles:

MARK LOOP
  COPY X F
  ADDI X 1 X
  TEST X > 2
  FJMP LOOP

The same loop could be written as:

COPY 0 F
COPY 1 F
COPY 2 F

For writing bigger blobs of repeating commands consider the @REP and @END commands:

@REP 3
  COPY @{0,1} F
@END

NOTE WILL BE EXPANDED TO
NOTE   COPY 0 F
NOTE   COPY 1 F
NOTE   COPY 2 F

File searches

Using loops you can quickly read files:

GRAB 199
MARK READING
  COPY F M
  TEST EOF
  FJMP READING

If you are searching for a specific entry, you can test for the value at the file pointer without having to copy F to some internal register:

NOTE X IS ENTRY WE ARE SEARCHING FOR
GRAB 199
MARK READING
  TEST F = X
  FJMP READING
NOTE GET THE NEXT ENTRY FROM FILE
NOTE YOU MIGHT WANT TO USE `SEEK` TO MOVE THE FILE POINTER
COPY F X

You can speed up searches in files by skipping rows. This needs more instructions, but in long files this will speed up the process:

NOTE X IS ENTRY WE ARE SEARCHING FOR
GRAB 199
MARK READING
  TEST F = X
  NOTE ADVANCE FILE HEADER TO NEXT LINE, SAVING 2 LOOPS
  SEEK 2
  FJMP READING
NOTE GET THE NEXT ENTRY FROM FILE
SEEK -2
COPY F X

In files you can read and delete at the same time. Well, almost, as you need to set the file pointer back for deleting the chunk you just read:

MARK GET_IT_OUT
  NOTE DO STUFF WITH F
  COPY F M
  SEEK -1
  VOID F
  TEST EOF
  FJMP GET_IT_OUT

Crash & burn loops

If you have to halt your EXA anyway after completing its loop, you may choose to deliberately crash it on exit. Reading a file, this can be easily achieved by reading past the file end. So this…

GRAB 200
MARK READ_FILE
  COPY F M
  TEST EOF
  FJMP READ_FILE
HALT 
NOTE `HALT` ALSO DOES `DROP`

..becomes this:

GRAB 200
MARK READ_FILE
  COPY F M
  JUMP READ_FILE
NOTE CRASH AND BURN ON EOF
NOTE CRASH ALSO DOES `DROP`

Sadly you cannot crash a network transfer. So have a look on Exapunks' network communication patterns on how to solve these.

Building new levels for Exapunks

Zachtronics published a guide on how to create new levels for Exapunks. Being a game for programmers, building levels (called “virtual network”) is done by programming… in Javascript.

The community is already at work: You will find new levels in the Exapunk Steam Workshop.