The assembler

Status: experimental, and not published yet.

zuring-asm reads assembly for the virtual machine and gives its image: the code the machine runs and the data that is a run's starting memory. It also disassembles code back into assembly. Styx's own engine is programmed through a Rust API instead (Programs).

Assembly

A line holds an optional label (name:), an instruction or a directive, and an optional comment from ; or #.

  • Registers are g0 to g7. By convention at is g7, the pseudo-instructions' scratch register, ra is g6, the return address, and sp is g5, the stack pointer.
  • Expressions are computed in the field: integers in decimal or hex, labels, .equ names and ., the current address, with +, -, *, << and parentheses. Labels in .text are code addresses and in .data memory addresses, each from 1: the machine keeps code and data apart.
  • Terms. Most instructions take a term: an expression over at most two registers, with at most one product of them. So set g1, 3*g2 + g4 - 7 and load g3, [g2 + 4*g1 + 16] are one instruction each.

The instructions, with T a term:

InstructionWhat it does
set d, Td ← T
inv d, Td ← 1/T; a fault if T is 0
eqz d, Td ← 1 if T = 0, else 0
split d, Td ← T / 64, rounded down, with the remainder left to the prover
limb TA fault unless T < 64: the end of a range check
pow5 d, Td ← T5
load d, [T], store s, [T]Reads or writes memory at address T
ladd d, t, [T], lmul d, t, [T]d ← t + the memory at T, or t times it
bnz T, L, bz T, LJumps to L if T is nonzero, or zero
jalr d, Td ← the next address, then jumps to T
in d, in d, 1d ← the step's input word, or the page cell
hint dd ← the step's next hint
out TThe output word ← T
assert TA fault unless T = 0
haltStops

Pseudo-instructions include nop, mov, neg, addi, subi, muli, jmp, call, ret, beqz, bnez, beq, bne, push, pop, and range s, n, which checks that s < 2n. The directives are .text, .data, .word, .zero, .equ, .rept … .endr, .k (the size a program is built for), .step (the end of a step), .loc, .inst (a raw entry) and .live (the registers live after a jump through a register).

The assembler packs instructions: an instruction whose value only the next one reads goes into that one's term. --no-pack turns it off. Labels, and every instruction after a branch or a jump, start a new entry, so a label always names an entry: write jump targets as labels.

An example

N! by recursion, with a stack from address STACK up:

.equ N, 5
.equ STACK, 160

        const g7, STACK         ; the stack pointer
        const g5, 1
        const g0, N
        jalr  g6, g3, fact      ; a call: g3 is 0
        out   g1
        halt
fact:   bnz   g0, recurse
        const g1, 1
        jalr  g2, g6, 0         ; return
recurse:
        store g6, [g7]          ; push the return address, then the argument
        add   g7, g7, g5
        store g0, [g7]
        add   g7, g7, g5
        sub   g0, g0, g5
        jalr  g6, g3, fact
        sub   g7, g7, g5        ; pop them
        load  g0, [g7]
        sub   g7, g7, g5
        load  g6, [g7]
        mul   g1, g1, g0
        jalr  g2, g6, 0

It uses the machine's older three-register forms, const, add, sub, mul and jalr d, s, e, which the assembler still takes.

Running it

CommandWhat it does
zuring-asm check <file>Assembles the file: its size, or its errors
zuring-asm list <file>Each entry's address, words and source line
zuring-asm dis <file>The code, disassembled into assembly that gives it again
zuring-asm run <file>Runs it to the halt: the output, or the fault and its line
zuring-asm prove <file>Proves its run

--x gives every step's input word (step gives each step its number), --xs a file of input words, one a step, --hints a file of hints, and --steps the most steps to run.