Decoding status and unexpected values
Script virtual machine · Values passed on to other routines · The levels
In brief
Every command has a documented layout, and a decoder that follows the scripts from their entry points reaches 99.6% of the script bytes of the 16 original levels with no unknown layout. The interpreter checks almost nothing: offsets, blocks, slots and counts outside their ranges read or write whatever lies there. This page gives the rules an editor or an interpreter needs for such values.
Decoding status
Every command has a documented operand layout, and a reachability decoder
applies them to the 16 reference LEVELn.INF files of the DOS release. It
starts from each distinct script offset of the cell bindings and follows jumps,
calls and conditional branches; it never scans for command bytes.1
| Result over the 16 reference files | Value |
|---|---|
| Distinct script references used as entry points | 1047 |
| Decoded instructions | 10131 |
| Script bytes (excluding the message table) | 65583 |
| Script bytes reached | 65328 (99.6%) |
| Unknown layouts, truncated operands, overlapping instructions, targets outside the script area | 0 |
| Commands that never occur in reachable scripts | SCRIPT_MOVE_INVENTORY_ITEM_TO_BLOCK, SCRIPT_MODIFY_CHARACTER_HP, SCRIPT_AWARD_EXPERIENCE, SCRIPT_IDENTIFY_ITEMS |
The 255 unreached bytes are 232 bytes of dead code after level changes (which
stop the run; the next bytes are mostly a SCRIPT_RETURN or a SCRIPT_JUMP),
5 bytes of repeated returns, and two unreferenced nine-byte routines at offset
2 of levels 12 and 14, each a SCRIPT_DAMAGE_CHARACTERS on all characters
followed by SCRIPT_RETURN, which nothing executes (see
how a run starts). These results apply to the DOS reference
files only.
Unexpected values
The interpreter validates almost nothing. The reference scripts use no value outside the documented sets, but an editor or an interpreter that must accept any script needs the following rules.
Positions. No script offset is checked: not a binding's offset, a jump,
call or condition target, nor the end of the buffer while operands are read.
Offsets are added to the buffer's start in 16-bit arithmetic, so a large one
wraps round within 64 KiB of memory; past the 7,508 bytes of the buffer lies
other game data. A byte below 0xe2 where a command is expected is skipped,
one byte at a time.
Blocks. No command checks a block index. Block b is the 10-byte entry b of the block table, which has 1,024 entries; a larger index, or a word read as negative, addresses other memory, the product by 10 being kept to 16 bits.
Party slots. The commands that take a character slot treat −1 as the whole party and pass any other value on without a range check (character routines).
Selectors and other operands, per command (lengths include the command byte):
| Command | Unknown selector | Other unchecked operands |
|---|---|---|
SCRIPT_SET | 2 bytes, no effect | side is added to the block's entry as a signed byte: values outside 0–3 write another byte of the entry or of a nearby entry, including a script offset. direction is stored whole as the party's facing. |
SCRIPT_TOGGLE | 2 bytes, no effect | side as for SCRIPT_SET, read and written. |
SCRIPT_MOVE_PARTY_OR_OBJECT | 6 bytes; the two words are ignored, but the view is marked for redrawing | Blocks and levels; see the command page. |
SCRIPT_PRINT_MESSAGE | — | The string index; see the command page, and its anomaly when no character qualifies. |
SCRIPT_SET_FLAGS | 2 bytes, no effect | Bit numbers and the monster slot; see bit numbers. |
SCRIPT_CLEAR_FLAGS | 3 bytes, no effect: the byte after the selector is consumed, also for SCRIPT_FLAG_TARGET_MONSTER | Bit numbers as for SCRIPT_SET_FLAGS. |
SCRIPT_LOAD_LEVEL_OR_MONSTERS | Every value other than SCRIPT_LOAD_LEVEL selects the 17-byte resource form | loader_index picks one of three routines without a range check: 3 and above call the command handlers that follow the table (3 is SCRIPT_SET's), negative values the entries before it, the first two of which are not routine addresses. |
SCRIPT_AWARD_EXPERIENCE | 2 bytes, no effect | — |
SCRIPT_CHANGE_DIRECTION | 3 bytes, no effect | None: the turn is reduced to 0–3. |
SCRIPT_SPECIAL_EVENT | Events above 6, the word read as unsigned: 3 bytes, no effect | — |
SCRIPT_DIALOGUE | 2 bytes, no effect | Picture frame and texts; see the command page. |
SCRIPT_DELETE_ITEM | None: any other byte is an item type | The block. |
SCRIPT_JUMP_UNLESS | Unknown tokens are skipped as single bytes | The expression stack has no bounds. |
SCRIPT_CALL | — | A call made with ten positions saved is skipped. |
SCRIPT_DELAY | — | The tick count is signed; see the command page. |
The other commands have no selector, or their pages describe what other selector values do: Run a sequence, Launch an object, Create an item.2
Bit numbers. A level or global flag bit is one bit of a 32-bit mask, built by a shift whose count is the operand read as an unsigned byte, so a negative operand counts as 128 to 255. Counts 0–31 select one bit and 32–47 select none, so setting or clearing has no effect. Larger counts select no bit on an 8086 processor; later processors reduce a shift count modulo 32, and some of these counts then select one of bits 16–31. A monster flag bit is built the same way in a single byte: counts 0–7 select a bit, 8–31 none, and larger counts depend on the processor in the same way. The monster slot is multiplied by 30 without a range check.3
Sources
Locations refer to the game executable reference; comparative sources are listed in the technical references.