The instruction set is fully recovered. It has 88 opcodes, and the disassembler in
this repository decodes 334 of 334 classes in HACK.RES (83 399 instructions) and
17 of 17 in OPEN.RES, with no failures.
The full opcode table is in reference/opcodes.md.
Not by guessing at the bytecode — by reading the interpreter.
AESOP.EXE is a 16-bit MZ executable with 1 223 relocations. Sorting them and looking
for runs of stride-4 entries turned up exactly one run of 165 entries, at image
offset 0x1B2FA: the table of far pointers to the 165 kernel routines, matching the
kernel dictionary in the archive one for one. That confirmed the analysis was pointing
at the right binary, and gave the address of every native routine.
The bytecode dispatcher fell out of a scan for near-pointer tables. At image offset
0x11591 sits a 256-entry table of 16-bit code offsets: the first 88 are distinct
and ascending, the remaining 168 all hold the same value. That value is the Invalid
AESOP opcode handler. The table is addressed by this instruction, which appears at the
end of every single handler:
b7 00 mov bh, 0
8a 1c mov bl, [si] ; fetch the next opcode
46 inc si ; advance the program counter
03 db add bx, bx
2e ff a7 41 06 jmp word ptr cs:[bx+0641h]The 0641h displacement pins the interpreter's code segment to load-relative 0x10F5.
Dispatch is threaded: there is no central loop, each handler jumps straight to the
next instruction's handler.
From there, disassembling all 88 handlers with a 16-bit disassembler gives the operand
size (how far each handler advances SI) and the exact semantics of every opcode. The
result was then validated against the archive — see 8.6.
A 32-bit stack machine, with segment registers doing the work of a memory model:
| register | role |
|---|---|
DS:SI |
program counter, inside the class's code resource |
ES:DI |
operand stack; one slot is 4 bytes, the stack grows downwards |
[BP-0Ah] |
frame pointer, an offset into the same stack segment |
[BP-0Ch] / [BP-06h] |
base and segment of the current object's instance data |
[BP-0Eh] / [BP-04h] |
base and segment of the class's import vector |
Values are 32-bit throughout — even a byte load is sign-extended into a full slot.
DS is reloaded through a double indirection (mov ds,[bp-2] then mov ds,[0]) every
time it changes, because resources move: what is stored is a handle, and the segment is
read out of a lock table.
Four address spaces are directly reachable, and the opcode families mirror them exactly:
| suffix | space | addressed as |
|---|---|---|
.s |
the class's own static data, inside the code resource | imm16 + TOS |
.l |
locals and arguments, in the current stack frame | FP - imm16 |
.v |
the current object's instance variables | self + imm16 |
.o |
another object's instance variables, through an import slot | object(TOS) + slot |
Each family comes in byte / word / dword, load / store, plain / indexed and address-of forms. That regularity is why the whole 88-opcode set fits on one page.
The one thing that is easy to get wrong: push (0x04) is the only general
allocator. Almost every load overwrites the top slot in place instead of pushing.
So the compiler emits a push and then a load that fills it:
1d 08 ld.b 8 ; TOS = 8 (overwrites)
04 push ; new slot
1d 01 ld.b 1 ; TOS = 1
10 or ; pops one: 8 | 1
04 push
1d 10 ld.b 16 ; TOS = 16
10 or ; 9 | 16 = 25
56 return
Read ld.b as push and you compute 16 instead of 25.
Every message handler and every local subroutine starts with a 2-byte little-endian
word giving its frame size in bytes; execution begins at entry + 2. The call
opcode reads that word to allocate the frame:
0FC6 ad lodsw ; target
0FC7 56 57 push si / push di ; save PC and stack pointer
0FC9 ff76f6 push [bp-0ah] ; save frame pointer
0FCE 897ef6 mov [bp-0ah], di ; new frame
0FD8 2b3c sub di, [si] ; allocate frame-size bytesThe export table's M:<id> values point at the frame word, not at the first
instruction — which is why a class with no static data has its first handler at
offset 14, immediately after the 14-byte class header.
Compilers leave a fingerprint here: nearly every handler ends with two consecutive
return instructions, one from the explicit return statement and one from the
implicit fall-through epilogue.
Three different call mechanisms, three opcodes:
ld.imp <slot> ; TOS = far pointer from the class's import vector
push / ld.b ... ; arguments
callk <n> ; far-call it with n arguments
callk (0x21) reaches the engine: the import slot was resolved at load time from the
C: entries in the class's .IMPT, so the disassembler can print the kernel routine's
name.
send <n>, <msg16> (0x22) is send_message: it pops n arguments and a receiver and
enters the interpreter's dispatcher. pass <n> (0x23) is pass_message — re-dispatch
the message currently being handled to the parent class, which is how AESOP does
super.
Both are opcodes rather than kernel calls, which is exactly why they appear in the archive's kernel dictionary with an empty vector.
The disassembler does a recursive descent over the control-flow graph starting from the
handler entry points, following branches, switch tables and call targets. Anything
never reached is data — which is how inline string literals are identified: the compiler
plants them in the middle of the code and jumps over them.
54 33 03 ptr.sd 0x0333 ; push a pointer to the bytes that follow
02 36 03 jmp 0x0336 ; step over them
25 58 00 ; "%X"
Running that over both archives:
HACK.RES: 334/334 classes decode cleanly, 83399 instructions
OPEN.RES: 17/17 classes decode cleanly, 5014 instructions
Every instruction boundary lands where the export tables and jump targets say it should. An instruction set that were wrong anywhere would desynchronise within a few dozen bytes and fail on hundreds of classes — a linear-sweep version of the same table, without control-flow following, already reached 331 of 334.
scrying glass is an item with a limited number of uses. This is its complete
implementation, disassembled and annotated automatically:
; --- message 0 (create), frame 2 bytes
0010 pass 0 args -> parent
0012 ld.imp 0 ; kernel rnd
0015 push
0016 ld.b 3
0018 push
0019 ld.b 3
001B callk 2 args
001D push
001E ld.b 3
0020 add
0021 stb.v 0 ; B charges
0024 return
; --- message 118 (use), frame 2 bytes
0027 ld.w 2005
002A push
002B ld.b 0
002D push
002E ld.b 12
0030 push
0031 ldd.l 0
0034 send 3 args, message 122 (cast spell)
0038 ldb.v 0 ; B charges
003B dec
003C stb.v 0 ; B charges
003F push
0040 ld.b 0
0042 eq
0043 bz 0x004D
0046 ldw.l 2
0049 send 0 args, message 91 (consume)
004D return
which is, in the original source, something very close to:
create() { pass_message(); charges = rnd(3, 3) + 3; }
use() { cast_spell(2005, 0, 12, self);
if (--charges == 0) send(consume); }298 of the 334 classes implement message 89, report. Now that the bytecode reads, it
is obvious why: report is a property query, a single switch on its argument, and
it is how every item, monster and dungeon feature describes itself.
leather boots in full — 143 bytes, one handler, no imports, the smallest complete
program in the archive:
0010 ldd.l 0 ; the query code
0013 jmp 0x0047
...
0047 switch 11 cases
case 0 -> return 1
case 1 -> return 0
case 3 -> return 1083
case 2106 -> return 0
case 3000 -> return 0
case 2003 -> return 2000
case 2000 -> return 39
case 2001 -> return 643
case 2005 -> return -1
case 2004 -> return 8|1|16
case 6 -> return -1
default -> return <query>
Resolving the returned numbers against the archive's own name dictionary decodes the protocol:
- query 3 returns resource 1083 = the string
"Leather boots". - query 2001 returns resource 643 = the bitmap
"Floor shape 18"— the sprite drawn when the item is lying on the dungeon floor. - query 2004 returns a bit set, assembled inline as
8 | 1 | 16.
And the identification mechanic falls straight out of comparing siblings. drow boots
answers query 3 with 1083, "Leather boots" — the same generic name — but query
2106 with 1084, "Drow boots". leather boots answers query 2106 with 0. So
query 3 is the name you see before identifying an item and query 2106 is its true
name, with a mundane item simply having none.
The query numbers themselves come from the headers the archive still lists as build
dependencies (shared.h, names.h, magic.h). Mapping the rest of them is the natural
next step; the values are consistent across all 219 item classes, so a single pass that
correlates each query's return value against the resource dictionary should name most of
them.
python tools/aesop_disasm.py opcodes
python tools/aesop_disasm.py verify game/HACK.RES
python tools/aesop_disasm.py class game/HACK.RES "scrying glass"
python tools/aesop_disasm.py list game/HACK.RES
Previous: 7. Runtime files · Next: 9. Open questions