Ternary
OverviewWhyLearnWord labSpecificationArithmeticNumbersLabsPlayground

balanced ternary computer — reference

ISA cheat sheet

6-trit word · 729 states · range -364..364 · app v0.12.0

Machine control

NOP0No state change. PC += 3.
HALT1Sets HALT flag; execution stops.

Data movement

MOV Rd, Rs2Rd <- Rs. Updates SIGN.
MOV Rd, value3Rd <- immediate. Updates SIGN.

Memory access

LOAD Rd, [addr]4Rd <- mem[addr]. Updates SIGN.
LOAD Rd, [Rs]5Rd <- mem[Rs]. Updates SIGN.
STORE [addr], Rs6mem[addr] <- Rs.
STORE [Rd], Rs7mem[Rd] <- Rs.

Arithmetic

ADD Rd, Rs8Rd <- Rd + Rs. Updates SIGN, OVF.
ADD Rd, value9Rd <- Rd + immediate. Updates SIGN, OVF.
SUB Rd, Rs10Rd <- Rd - Rs. Updates SIGN, OVF.
SUB Rd, value11Rd <- Rd - immediate. Updates SIGN, OVF.
MUL Rd, Rs12Rd <- Rd * Rs. Updates SIGN, OVF.
MUL Rd, value13Rd <- Rd * immediate. Updates SIGN, OVF.
NEG Rd14Rd <- -Rd (trit-wise inversion). Updates SIGN.
DIV Rd, Rs26Rd <- round(Rd / Rs), ties away from zero. Updates SIGN, OVF.
DIV Rd, value27Rd <- round(Rd / immediate), ties away from zero. Updates SIGN, OVF.
REM Rd, Rs28Rd <- Rd - DIV(Rd, Rs) * Rs. Updates SIGN, OVF.
REM Rd, value29Rd <- Rd - DIV(Rd, imm) * imm. Updates SIGN, OVF.
SHL Rd, Rs30Rd <- Rs * 3 (shift one trit place left). Updates SIGN, OVF.
SHR Rd, Rs31Rd <- round(Rs / 3) (shift one trit place right). Updates SIGN, OVF.

Comparison

CMP Rd, Rs15SIGN <- sign(Rd - Rs), raw difference. OVF untouched.
CMP Rd, value16SIGN <- sign(Rd - immediate), raw difference. OVF untouched.

Control flow

JMP label17PC <- addr.
JEQ label18PC <- addr if SIGN = 0.
JNE label19PC <- addr if SIGN /= 0.
JLT label20PC <- addr if SIGN = -.
JGT label21PC <- addr if SIGN = +.
CALL label22Push return address, PC <- addr.
RET23Pop return address into PC.

Stack

PUSH Rs24mem[SP] <- Rs, SP -= 1.
POP Rd25SP += 1, Rd <- mem[SP].

Trit-wise logic

LAND Rd, Rs32Rd <- trit-wise min(Rd, Rs). Updates SIGN, OVF.
LOR Rd, Rs33Rd <- trit-wise max(Rd, Rs). Updates SIGN, OVF.

Input / output

IN Rd34Rd <- next input value. Errors if the input buffer is empty.
OUT Rs35Output <- Rd.

Flags contract: result ops set SIGN + OVF from the stored word; CMP sets SIGN from the unwrapped difference; everything else leaves flags untouched. Division rounds to nearest, ties away from zero.

Printed from the interactive specification at /spec — every line of this sheet is a live instruction you can run there.

6-trit word729-word memorydeterministic executionv0.12.0