The Inform 6 Programmer's Guide

Chapter 27: Z-Machine Instruction Set (Assembly)

[Z-machine] This chapter documents the Z-machine instruction set as supported by the compiler. It covers the binary encoding of instructions, the complete set of opcodes organized by category, and the assembly-language syntax that lets programmers embed Z-machine instructions directly in their source code.

27.1 Encoding Format: Opcodes, Operand Types

Every Z-machine instruction consists of an opcode (identifying the operation) followed by zero or more operands, and optionally a store destination and/or a branch offset. The encoding of each part depends on the instruction's operand count category.

27.1.1 Operand Count Categories

The Z-machine groups instructions into categories based on the number of operands they accept. The compiler defines these internally as:

Category Constant Operand Count Description
2OP TWO Exactly 2 (sometimes 3-4) Two-operand instructions
VAR VAR 0 to 4 Variable-operand instructions
VAR_LONG VAR_LONG 0 to 8 Long variable-operand instructions
1OP ONE Exactly 1 One-operand instructions
0OP ZERO Exactly 0 Zero-operand instructions
EXT EXT 0 to 4 Extended instructions
EXT_LONG EXT_LONG 0 to 8 Long extended instructions

27.1.2 Opcode Byte Encoding

The first byte (or bytes) of an instruction encodes the opcode and sometimes the operand types. The compiler writes the opcode byte in assemblez_instruction() using the following scheme:

2OP short form (when both operands are small constants or variables):

Bit:  7  6  5  4  3  2  1  0
     [0] [t1] [t2] [--opcode--]
  • Bit 7: always 0
  • Bit 6 (t1): first operand type (0 = small constant, 1 = variable)
  • Bit 5 (t2): second operand type (0 = small constant, 1 = variable)
  • Bits 4-0: opcode number (0x00-0x1F)

If either operand is a long constant (2 bytes), the instruction is automatically promoted to VAR form by adding 0xC0 to the opcode byte and including a types byte.

1OP form:

Bit:  7  6  5  4  3  2  1  0
     [1] [0] [type] [--opcode--]
  • Bits 7-6: 10 (identifies 1OP)
  • Bits 5-4: operand type
  • Bits 3-0: opcode number (0x00-0x0F)

0OP form:

Bit:  7  6  5  4  3  2  1  0
     [1] [0] [1] [1] [--opcode--]
  • Bits 7-4: 1011 (identifies 0OP; top bits = 0xB0)
  • Bits 3-0: opcode number (0x00-0x0F)

VAR form:

Byte 1:  [1] [1] [---opcode---]     (top bits = 0xC0)
Byte 2:  [types for up to 4 operands]
  • Bits 7-6 of byte 1: 11 (identifies VAR)
  • Bits 5-0 of byte 1: opcode number (0x00-0x3F); native VAR opcodes have bit 5 set (0x20-0x3F), while 2OP opcodes promoted to VAR form have bit 5 clear (0x00-0x1F)
  • Byte 2: operand types, 2 bits each for up to 4 operands

VAR_LONG form: Same as VAR but with two type bytes, supporting up to 8 operands.

EXT form:

Byte 1:  0xBE (extended prefix)
Byte 2:  [opcode number]
Byte 3:  [types for up to 4 operands]

EXT instructions always begin with the prefix byte 0xBE, followed by the opcode number and a types byte.

EXT_LONG form: Same as EXT but with two type bytes, supporting up to 8 operands.

27.1.3 Operand Type Encoding

Each operand's type is encoded as a 2-bit value:

Bits Type Size Description
00 Large constant 2 bytes A 16-bit constant value
01 Small constant 1 byte An 8-bit constant value (0-255)
10 Variable 1 byte A variable number (0 = stack, 1-15 = locals, 16-255 = globals)
11 Omitted 0 bytes No operand in this position

In types bytes, operand types are packed from the most significant bits downward. For a VAR instruction with a single types byte:

Bit:  7  6  5  4  3  2  1  0
     [op1 ] [op2 ] [op3 ] [op4 ]

Unused operand positions are filled with 11 (omitted). For a VAR_LONG instruction, a second types byte provides positions for operands 5-8.

27.1.4 Store Destinations

Instructions that produce a result (flagged with St in the opcode table) include a store byte after the operands. This byte gives the variable number where the result is stored:

  • 0: push onto the stack
  • 1-15: local variable
  • 16-255: global variable

27.1.5 Branch Offsets

Instructions that can branch (flagged with Br) include a branch specification after the operands (and after any store byte).

Short branch (1 byte):

Bit:  7     6    5  4  3  2  1  0
     [pol] [1]  [------offset------]
  • Bit 7: branch polarity (1 = branch on true, 0 = branch on false)
  • Bit 6: 1 indicates short form
  • Bits 5-0: 6-bit offset, or special values:
    • 0 = return false from current routine
    • 1 = return true from current routine
    • 2-63 = branch forward by (offset - 2) bytes

Long branch (2 bytes):

Byte 1:  [pol] [0] [-----high 6 bits-----]
Byte 2:  [----------low 8 bits-----------]
  • Bit 7 of byte 1: branch polarity
  • Bit 6 of byte 1: 0 indicates long form
  • Remaining 14 bits: signed offset from the address after the branch bytes (offset - 2 gives the actual displacement)

27.2 Instruction Categories and Flags

27.2.1 Opcode Flags

Each opcode in the compiler's tables carries a set of flags describing its behavior:

Flag Value Meaning
St 1 Instruction stores a result
Br 2 Instruction has a branch
Rf 4 Execution never continues past this instruction (return or unconditional jump)
St2 8 Second-to-last operand is a store destination (Glulx only)

An instruction may combine flags. For example, get_sibling has flags St+Br (3), meaning it both stores a result and branches.

27.2.2 Special Operand Rules

Some opcodes have unusual operand assembly rules, indicated by the op_rules field:

Rule Value Meaning
VARIAB 1 First operand is a variable number, assembled as a small constant
TEXT 2 One text operand, Z-encoded inline in the instruction stream
LABEL 3 One operand is a label, assembled as a long constant offset
CALL 4 First operand is a routine name, assembled as a packed address

VARIAB rule: Used by opcodes like inc, dec, store, load, pull, inc_chk, and dec_chk. The first operand names a variable (given as a variable name in assembly). Still, it is encoded as a small constant holding the variable's number rather than as a variable reference.

TEXT rule: Used only by print and print_ret. The operand is a double-quoted string that is Z-encoded directly into the instruction stream.

LABEL rule: Used only by jump. The operand is a label name, encoded as a 2-byte offset.

CALL rule: Used by all call instructions (call, call_vs, call_2s, call_1s, call_vn, call_2n, call_1n, call_vs2, call_vn2). The first operand is a routine name that is assembled as a packed address.

27.2.3 Flags 2 Requirements

Some opcodes set a bit in the story file's Flags 2 header word when they are used. This informs the interpreter that the game requires certain features. The flags2_set field in the opcode table specifies which bit to set:

Bit Feature Opcodes
3 Pictures draw_picture, picture_data, erase_picture, picture_table
4 Undo save_undo, restore_undo
5 Mouse read_mouse, mouse_window
6 Colours set_colour
7 Sound effects sound_effect
8 Menus make_menu

27.3 Opcodes by Version

The complete per-opcode reference - every opcode with its code, operand category, version availability, flags, rules, Flags 2 bit, and a short description - is in §27.8, organized by operand-count category. This section summarises what each Z-machine version contributes, and details the opcodes whose encoding changes between versions.

27.3.1 Version Evolution

  • Version 3 provides the base instruction set: the arithmetic, comparison, object-tree, property, array, input/output, and control-flow opcodes that all later versions inherit.
  • Version 4 adds result-storing and extra call forms (call_2s, call_1s, call_vs2) and the screen-model opcodes (erase_window, set_cursor, read_char, scan_table, and related).
  • Version 5 adds the discard-result call forms (call_2n, call_1n, call_vn, call_vn2), text opcodes (tokenise, encode_text, check_arg_count, print_table, copy_table), and the EXT range (log_shift, art_shift, set_font, save_undo, restore_undo, and more). not also moves from 1OP $0F to VAR $38, freeing $0F for call_1n. Versions 7 and 8 share this same instruction set: the compiler resolves their opcodes with an instruction-set number of 5, so "5+" in the reference tables covers versions 5, 7, and 8 alike.
  • Version 6 adds the graphics and windowing EXT opcodes (draw_picture, picture_data, move_window, window_size, make_menu, read_mouse, and related).
  • Z-Machine Standard 1.0 adds print_unicode and check_unicode; Standard 1.1 adds set_true_colour and buffer_screen.

For the exact version availability of every opcode, see the Ver column of the reference tables in §27.8.

27.3.2 Version-Variant Opcodes

Several opcodes change their encoding or behavior between Z-machine versions. The compiler tracks these in the extension_table_z[] array. When the compiler encounters one of these opcodes, it selects the appropriate form based on the target version.

not - Bitwise NOT:

Version Category Code Flags
3 1OP 0x0F St
4 1OP 0x0F St
5+ VAR 0x38 St

In versions 3 and 4, not is a 1OP instruction at opcode 0x0F that stores its result. From version 5 onwards, it becomes a VAR instruction at opcode 0x38 (still storing); the freed 1OP 0x0F slot is then reused by call_1n.

save - Save game state:

Version Category Code Flags
3 0OP 0x05 Br
4 0OP 0x05 St
5+ EXT 0x00 St

In version 3, save is a 0OP instruction that branches on success/failure. In version 4, it becomes a 0OP that stores a result. From version 5 onwards, it is an EXT instruction that stores a result.

restore - Restore game state:

Version Category Code Flags
3 0OP 0x06 Br
4 0OP 0x06 St
5+ EXT 0x01 St

Follows the same pattern as save.

pull - Pull from stack:

Version Category Code Flags Rules
3-5 VAR 0x29 - VARIAB
6 VAR 0x29 St -

In versions 3-5, pull takes a variable reference as its operand (the variable to receive the popped value). In version 6, it stores the result using the standard store mechanism.

read - Read a line of input:

Version Category Code Flags
3 VAR 0x24 -
4+ VAR 0x24 St

In version 3, read takes no store operand. From version 4 onwards, it stores a result. In assembly, this opcode is typed as sread (version 3) or aread (version 4+) after the @ prefix; read is its name in the reference tables but is not itself accepted as an @ mnemonic.

27.4 The @ Assembly Notation

You can embed Z-machine instructions directly in source code using the @ prefix. The assembly statement begins with @, followed by the opcode name and its operands, and terminates with a semicolon.

27.4.1 Basic Syntax

The general form of an assembly statement is:

@opcode operand1 operand2 ... ;

For example:

@add 3 4 -> result;
@jz x ?is_zero;
@print "Hello, world!";

Spaces separate the operands. There are no commas between operands.

27.4.2 Store Destinations

For opcodes that store a result (those with the St flag), the store destination is specified with -> followed by a variable name or sp (to push the result onto the stack):

@add x y -> result;        ! Store sum in 'result'
@random 100 -> sp;          ! Push random number onto stack
@get_parent obj -> parent;  ! Store parent object number

If no explicit -> is given, the compiler automatically uses the last operand as the store destination (which must be a variable). Use explicit -> for clarity.

27.4.3 Branch Destinations

For opcodes that branch (those with the Br flag), the branch destination is specified with ? (branch on true) or ?~ (branch on false), followed by a label name:

@je x y ?equal;              ! Branch to 'equal' if x == y
@test_attr obj a ?~no_attr;  ! Branch to 'no_attr' if object lacks attribute
@jz count ?done;             ! Branch to 'done' if count is zero

Two special branch targets are available:

@je x y ?rtrue;              ! Return true if x == y
@jz count ?rfalse;           ! Return false if count is zero

?rtrue and ?rfalse cause the routine to return true (1) or false (0) respectively, rather than branching to a label. These are encoded efficiently as special values in the branch offset.

27.4.4 Labels

Assembly labels are defined using the standard label syntax (a dot followed by the label name) and referenced in branch operands:

[ MyRoutine x;
    @jz x ?is_zero;
    @print_num x;
    @new_line;
    @rtrue;
  .is_zero;
    @print "Zero!";
    @new_line;
    @rfalse;
];

For the jump opcode, the operand is a label name (not prefixed with ?):

@jump done;
...
.done;

27.4.5 Text Operands

The print and print_ret opcodes take a double-quoted string as their operand. The string is Z-encoded directly into the instruction stream:

@print "Hello, world!";
@print_ret "Goodbye!";    ! Also prints newline and returns true

27.4.6 Indirect Addressing

Opcodes with the VARIAB operand rule (such as inc, dec, store, load, pull, inc_chk, dec_chk) take a variable reference as their first operand. The variable name is assembled as a small constant holding the variable's number.

To use indirect addressing - where the variable number is itself stored in another variable - enclose the first operand in square brackets:

@inc [x];       ! Increment the variable whose number is stored in x
@load [x] -> y; ! Load the value of the variable numbered by x into y

Without brackets, a variable name is treated as a literal reference to that specific variable.

27.4.7 The sp Variable

In assembly statements, the name sp refers to the stack pointer (variable number 0). It can be used as an operand to push or pop values:

@push value;        ! Same as: push value onto stack
@pull sp;           ! Pop stack (discarding the value)
@add x y -> sp;     ! Push the sum onto the stack
@storew table 0 sp; ! Store top-of-stack into table entry

27.5 Custom Opcode Syntax

The compiler lets you emit arbitrary Z-machine opcodes that may not be in the compiler's built-in opcode table. This is done using a double-quoted opcode specification string after @.

27.5.1 Syntax

@"TYPE:NUMBERflags" operands... ;

Where:

  • TYPE is one of: 0OP, 1OP, 2OP, VAR, EXT, VAR_LONG, EXT_LONG
  • NUMBER is the opcode number (decimal) within the type's range
  • flags are optional single characters:
    • B - the opcode has a branch
    • S - the opcode stores a result
    • T - the opcode takes a text operand
    • I - the first operand uses indirect addressing (variable number)
    • F n - set bit n in Flags 2

27.5.2 Opcode Number Ranges

Each type has a valid range of opcode numbers:

Type Minimum Maximum
0OP 0 15
1OP 0 15
2OP 0 31
VAR 32 63
VAR_LONG 32 63
EXT 0 255
EXT_LONG 0 255

27.5.3 Examples

! Emit a VAR opcode number 40 that stores a result:
@"VAR:40S" arg1 arg2 -> result;

! Emit an EXT opcode number 20 that branches:
@"EXT:20B" value ?label;

! Emit a 2OP opcode number 30 with no special flags:
@"2OP:30" x y;

! Emit an EXT opcode that sets Flags 2 bit 9:
@"EXT:100F9" arg1 arg2;

This feature is useful for experimenting with opcodes defined in newer versions of the Z-Machine Specification that the compiler may not yet support, or for testing custom interpreter extensions.

27.6 Inline Byte and Word Assembly

Beyond structured opcode assembly, raw bytes or words can be emitted directly into the code stream using @-> and @-->.

27.6.1 Byte Assembly (@->)

@-> value1 value2 value3 ... ;

Each value is truncated to 8 bits and emitted as a raw byte into the code area. Values must be known constants (no backpatchable references are allowed). The compiler issues a warning if any value is 256 or greater.

Example:

! Emit three raw bytes into the code stream:
@-> $BE $05 $FF;

27.6.2 Word Assembly (@-->)

@--> value1 value2 ... ;

Each value is emitted as a 2-byte big-endian word. Unlike byte assembly, word values may contain backpatchable references (such as routine addresses or string addresses), because the marker is recorded on the high byte.

Example:

! Emit two raw words into the code stream:
@--> $1234 MyRoutine;

27.6.3 Use Cases

Inline byte and word assembly is rarely needed in ordinary programming. Potential uses include:

  • Emitting raw data that must appear at a specific position in the code area
  • Constructing instruction sequences that the compiler's assembly parser does not directly support
  • Embedding lookup tables in the code stream

Warning: Inline assembly is not checked for correctness. Emitting invalid bytes can produce a story file that crashes the interpreter. Use these features only when no alternative exists.

27.7 Common Assembly Patterns

This section illustrates common patterns for using Z-machine assembly in source code. Assembly is useful for performance-critical code, direct hardware access, or operations that cannot be expressed in high-level source.

27.7.1 Stack Manipulation

@push value;          ! Push a value onto the stack
@pull variable;       ! Pop the stack into a variable

! Using the stack as a temporary:
@push x;
! ... do something that modifies x ...
@pull x;              ! Restore x from the stack

27.7.2 Object Tree Traversal

! Get the parent, child, and sibling of an object:
@get_parent obj -> p;
@get_child obj -> c ?has_children;
@get_sibling obj -> s ?has_sibling;

! Traverse all children of an object:
[ PrintChildren obj child;
    @get_child obj -> child ?~done;
  .loop;
    @print_obj child;
    @new_line;
    @get_sibling child -> child ?loop;
  .done;
];

27.7.3 Direct Property Access

! Get a property value:
@get_prop obj prop_num -> value;

! Set a property value:
@put_prop obj prop_num new_value;

! Get the address and length of property data:
@get_prop_addr obj prop_num -> addr;
@get_prop_len addr -> len;

27.7.4 Memory Access

! Read from and write to memory:
@loadw base index -> value;     ! value = base-->index
@loadb base index -> value;     ! value = base->index
@storew base index new_value;   ! base-->index = new_value
@storeb base index new_value;   ! base->index = new_value

27.7.5 Conditional Branching

! Compare two values:
@je x y ?are_equal;
@jl x y ?x_is_less;
@jg x y ?x_is_greater;

! Test a value against zero:
@jz x ?is_zero;

! Test attributes:
@test_attr obj light ?has_light;

! Test if object is a child of another:
@jin child parent ?is_child;

! Test a bitmask:
@test flags mask ?all_bits_set;

27.7.6 Arithmetic

@add x y -> sum;
@sub x y -> diff;
@mul x y -> product;
@div x y -> quotient;
@mod x y -> remainder;

! Increment and decrement with branch:
@inc_chk score 100 ?reached_max;
@dec_chk lives 0 ?game_over;

! Bitwise operations:
@or x y -> result;
@and x y -> result;
@not x -> result;         ! In v5+, this is: @"VAR:56S" x -> result;

! Shifting (v5+):
@log_shift x 2 -> result;    ! Logical shift left by 2
@log_shift x -3 -> result;   ! Logical shift right by 3
@art_shift x -1 -> result;   ! Arithmetic shift right by 1

27.7.7 I/O Operations

! Output:
@print "Hello!";
@print_char 65;           ! Print 'A'
@print_num 42;            ! Print "42"
@print_obj obj;           ! Print object's short name
@new_line;

! Input (v4+):
@aread text_buffer parse_buffer -> result;  ! Read a line of input
@read_char 1 -> ch;       ! Read a single keypress

! Unicode (ZSpec 1.0+):
@print_unicode $00e9;     ! Print 'é' by Unicode value

27.7.8 Calling Routines

The Z-machine provides several calling conventions that differ in whether they store the return value or discard it, and in how many arguments they accept:

! Calls that store the result:
@call_vs routine arg1 arg2 arg3 -> result;  ! 0-3 args (VAR)
@call_vs2 routine a1 a2 a3 a4 -> result;   ! 0-7 args (VAR_LONG)
@call_2s routine arg1 -> result;            ! exactly 1 arg (2OP)
@call_1s routine -> result;                 ! 0 args (1OP)

! Calls that discard the result:
@call_vn routine arg1 arg2;                 ! 0-3 args (VAR)
@call_vn2 routine a1 a2 a3 a4 a5;          ! 0-7 args (VAR_LONG)
@call_2n routine arg1;                      ! exactly 1 arg (2OP)
@call_1n routine;                           ! 0 args (1OP)

Use the "s" variants (store) when you need the return value. The "n" variants (no-store) are used when you can discard the return value, saving one byte in the instruction encoding.

27.7.9 Save and Restore

! Save/restore undo (v5+):
@save_undo -> result;
@jz result ?undo_not_available;
! result: 0 = failed, 1 = saved, 2 = restored from undo

@restore_undo -> result;
! If successful, execution continues from the matching save_undo
! with result = 2

! Save/restore to file (v5+, EXT form):
@save table bytes name prompt -> result;
@restore table bytes name prompt -> result;

27.7.10 Catch and Throw (v5+)

! Catch the current stack frame:
@catch -> frame;

! ... later, unwind the stack back to that frame:
@throw return_value frame;

catch stores a stack frame identifier. throw unwinds the stack to the identified frame and causes the routine that executed catch to return with the given value. This provides a non-local return mechanism.

27.8 Opcode Reference Tables

The following tables present the complete opcode set in a format suitable for quick reference. Each table is organized by operand count category.

27.8.1 2OP Opcodes

# Name Code Ver Flags Rules F2 Description
0 je $01 3+ Br - - Jump if equal (up to 4 operands in VAR form)
1 jl $02 3+ Br - - Jump if less than (signed)
2 jg $03 3+ Br - - Jump if greater than (signed)
3 dec_chk $04 3+ Br VARIAB - Decrement variable and branch if less than value
4 inc_chk $05 3+ Br VARIAB - Increment variable and branch if greater than value
5 jin $06 3+ Br - - Jump if object a is a direct child of object b
6 test $07 3+ Br - - Branch if all bits in bitmap are set in flags
7 or $08 3+ St - - Bitwise OR
8 and $09 3+ St - - Bitwise AND
9 test_attr $0A 3+ Br - - Branch if object has attribute
10 set_attr $0B 3+ - - - Set attribute on object
11 clear_attr $0C 3+ - - - Clear attribute on object
12 store $0D 3+ - VARIAB - Store value into variable
13 insert_obj $0E 3+ - - - Move object to become first child of destination
14 loadw $0F 3+ St - - Load word from array: array-->index
15 loadb $10 3+ St - - Load byte from array: array->index
16 get_prop $11 3+ St - - Get property value of object
17 get_prop_addr $12 3+ St - - Get byte address of property data
18 get_next_prop $13 3+ St - - Get number of next property after given one
19 add $14 3+ St - - Signed addition
20 sub $15 3+ St - - Signed subtraction
21 mul $16 3+ St - - Signed multiplication
22 div $17 3+ St - - Signed integer division
23 mod $18 3+ St - - Signed remainder
24 call_2s $19 4+ St CALL - Call routine with 1 argument, store result
25 call_2n $1A 5+ - CALL - Call routine with 1 argument, discard result
26 set_colour $1B 5+ - - 6 Set foreground and background colors (sets Flags 2, bit 6)
27 throw $1C 5+ - - - Throw a value to a catch frame

27.8.2 1OP Opcodes

# Name Code Ver Flags Rules Description
0 jz $00 3+ Br - Branch if value is zero
1 get_sibling $01 3+ St+Br - Get sibling of object; branch if it exists
2 get_child $02 3+ St+Br - Get first child of object; branch if it exists
3 get_parent $03 3+ St - Get parent of object
4 get_prop_len $04 3+ St - Get length of property data in bytes
5 inc $05 3+ - VARIAB Increment variable
6 dec $06 3+ - VARIAB Decrement variable
7 print_addr $07 3+ - - Print string at byte address
8 call_1s $08 4+ St CALL Call routine with 0 arguments, store result
9 remove_obj $09 3+ - - Remove object from object tree
10 print_obj $0A 3+ - - Print short name of object
11 ret $0B 3+ Rf - Return value from current routine
12 jump $0C 3+ Rf LABEL Unconditional jump to label
13 print_paddr $0D 3+ - - Print string at packed address
14 load $0E 3+ St VARIAB Load value of variable
15 not / call_1n $0F 3-4 / 5+ St / - - / CALL Bitwise NOT (v3-4); call routine, discarding the result (v5+)

Note: opcode $0F in the 1OP range is not (with store) in versions 3 and 4, and call_1n from version 5 onwards. From version 5, not itself becomes a VAR instruction at $38.

27.8.3 0OP Opcodes

# Name Code Ver Flags Rules Description
0 rtrue $00 3+ Rf - Return true (1) from current routine
1 rfalse $01 3+ Rf - Return false (0) from current routine
2 print $02 3+ - TEXT Print inline Z-encoded string
3 print_ret $03 3+ Rf TEXT Print inline string, print newline, return true
4 nop $04 3+ - - No operation
5 save $05 3 Br - Save game state (v3 only; changes in v4+)
6 restore $06 3 Br - Restore game state (v3 only; changes in v4+)
7 restart $07 3+ - - Restart the game
8 ret_popped $08 3+ Rf - Return top-of-stack value
9 pop / catch $09 3-4 / 5+ - / St - Discard top-of-stack value (v3-4); catch a stack frame (v5+)
10 quit $0A 3+ Rf - Terminate the program
11 new_line $0B 3+ - - Print a newline character
12 show_status $0C 3 - - Update the status line (v3 only)
13 verify $0D 3+ Br - Verify the story file checksum
15 piracy $0F 5+ Br - Branch if story file is genuine (always branches in practice)

Note: opcode $09 is pop in versions 3-4 and catch (with store) from version 5 onwards. save and restore at $05/$06 are 0OP instructions in versions 3 and 4 (branch form in V3, store form in V4); they move to EXT form in version 5 and later (see §27.3.2).

27.8.4 VAR Opcodes

# Name Code Ver Form Flags Rules F2 Description
0 call / call_vs $20 3+ / 4+ VAR St CALL - Call routine with 0-3 arguments, storing the result
1 storew $21 3+ VAR - - - Store word into array: array-->index = value
2 storeb $22 3+ VAR - - - Store byte into array: array->index = value
3 put_prop $23 3+ VAR - - - Set property value of object
4 read $24 3+ VAR - (v3) / St (v4+) - - Read a line of input (v3 "sread" form: no store)
5 print_char $25 3+ VAR - - - Print ZSCII character
6 print_num $26 3+ VAR - - - Print signed number in decimal
7 random $27 3+ VAR St - - Generate random number, or seed the generator
8 push $28 3+ VAR - - - Push value onto the stack
9 pull $29 3+ VAR - (v3-5) / St (v6) VARIAB (v3-5) - Pull value from stack into variable (v3-5; changes in v6)
10 split_window $2A 3+ VAR - - - Split screen into upper and lower windows
11 set_window $2B 3+ VAR - - - Select output window
12 call_vs2 $2C 4+ VAR_LONG St CALL - Call routine with 0-7 arguments, store result (VAR_LONG)
13 erase_window $2D 4+ VAR - - - Erase a window
14 erase_line $2E 4+ VAR - - - Erase to end of line
15 set_cursor $2F 4+ VAR - - - Set cursor position
16 get_cursor $30 4+ VAR - - - Get cursor position into a table
17 set_text_style $31 4+ VAR - - - Set text style (bold, italic, fixed-width, etc.)
18 buffer_mode $32 4+ VAR - - - Enable or disable output buffering
19 output_stream $33 3+ VAR - - - Enable or disable an output stream
20 input_stream $34 3+ VAR - - - Select an input stream
21 sound_effect $35 3+ VAR - - 7 Play or stop a sound effect (sets Flags 2, bit 7)
22 read_char $36 4+ VAR St - - Read a single keypress
23 scan_table $37 4+ VAR St+Br - - Search a table for a value
24 not $38 5+ VAR St - - Bitwise NOT (v5+ VAR form)
25 call_vn $39 5+ VAR - CALL - Call routine with 0-3 arguments, discard result
26 call_vn2 $3A 5+ VAR_LONG - CALL - Call routine with 0-7 arguments, discard result (VAR_LONG)
27 tokenise $3B 5+ VAR - - - Tokenise text buffer into parse buffer
28 encode_text $3C 5+ VAR - - - Encode text to dictionary form
29 copy_table $3D 5+ VAR - - - Copy or zero a table
30 print_table $3E 5+ VAR - - - Print a rectangular area of text
31 check_arg_count $3F 5+ VAR Br - - Branch if argument n was provided to current routine

Note: the input opcode listed here as read (VAR $24) is the compiler's internal table name. The mnemonic actually typed after @ is sread in version 3 (no store) and aread in version 4 and later (storing); read itself is not accepted as an @ opcode name.

27.8.5 EXT Opcodes

# Name Code Ver Flags F2 Description
0 save $00 5+ St - Save game state (v5+ EXT form)
1 restore $01 5+ St - Restore game state (v5+ EXT form)
2 log_shift $02 5+ St - Logical shift (positive = left, negative = right)
3 art_shift $03 5+ St - Arithmetic shift (sign-extending right shift)
4 set_font $04 5+ St - Set font and return previous font number
5 draw_picture $05 6 - 3 Draw a picture resource
6 picture_data $06 6 Br 3 Get picture dimensions; branch if picture exists
7 erase_picture $07 6 - 3 Erase a picture area
8 set_margins $08 6 - - Set left and right margins
9 save_undo $09 5+ St 4 Save undo state (sets Flags 2, bit 4)
10 restore_undo $0A 5+ St 4 Restore undo state (sets Flags 2, bit 4)
11 print_unicode $0B 5+ (ZSpec 1.0) - - Print a Unicode character
12 check_unicode $0C 5+ (ZSpec 1.0) St - Check if a Unicode character can be printed and read
13 set_true_colour $0D 5+ (ZSpec 1.1) - - Set foreground and background using true-colour values (v5+)
16 move_window $10 6 - - Move a window
17 window_size $11 6 - - Resize a window
18 window_style $12 6 - - Set window style flags
19 get_wind_prop $13 6 St - Get a window property
20 scroll_window $14 6 - - Scroll a window
21 pop_stack $15 6 - - Pop values from a stack
22 read_mouse $16 6 - 5 Read mouse position and buttons
23 mouse_window $17 6 - 5 Set window for mouse input
24 push_stack $18 6 Br - Push a value onto a stack; branch on success
25 put_wind_prop $19 6 - - Set a window property
26 print_form $1A 6 - - Print a formatted table
27 make_menu $1B 6 Br 8 Create a menu; branch on success
28 picture_table $1C 6 - 3 Preload picture resources
29 buffer_screen $1D 6 (ZSpec 1.1) St - Control screen buffering

27.8.6 Table Legend

The columns in the reference tables above are:

  • #: Local row counter within each table (for cross-referencing within the table); it is not the internal *_zc opcode index from the compiler's header.h
  • Name: The assembly language name used with the @ prefix
  • Code: The opcode number (hexadecimal) within its category
  • Ver: The Z-machine version(s) where this opcode is available
  • Form: The operand count category (if different from the table's default)
  • Flags: St = stores result, Br = branches, Rf = never continues (return/jump), St+Br = stores and branches
  • Rules: VARIAB = first operand is a variable number, CALL = first operand is a routine packed address, TEXT = inline Z-encoded string, LABEL = label offset
  • F2: If non-zero, the Flags 2 header bit is set when this opcode is assembled