Utility routines
Routines · Data · Address index
Memory, random numbers and dice, strings, files and disks, the overlay manager and the run-time library. The routines page explains how the entries are written.
0x10142, 0x10139 — Divide-error handler
- Range:
0x10142–0x10184;0x10139–0x10212 - Role: At start-up
0x10142saves the vectors of interrupts 0, 4, 5 and 6 and makes0x10139the handler of interrupt 0, which the processor raises on a division error.0x10139writes the 14 characters atDS:0x2f, "Divide error" and a line end (0x101f5), and ends the program with exit code 3 through DOS function0x4c(0x1020b,0x10127–0x10137). Nothing else is undone on that path: the handlers the game installed, such as the sound driver's timer handler, stay in place. - Used by: PC speaker and Tandy sounds (SND)
0x162a3, 0x16292 — Random generator
- Range:
0x162a3–0x162c8,0x16292–0x162a2 - Role:
0x162a3multiplies the 32-bit seedDS:0x6962by0x015a4e35(long multiply0x186e5), adds 1, stores it and returns its bits 16–30, a value from 0 to 32,767.0x16292(value)sets the seed to value (high word 0). The seed is 1 in the file image; the start-up sets it once (0x315c0) from the value returned by0x16524(0), which reads the date and time (inferred). Only random number in a range and roll dice (0x19298) call0x162a3. - Used by: Random number in a range
0x2d329, 0x2d274 — Largest free block, resize a block
- Ranges:
0x2d329–0x2d3d8;0x2d274–0x2d328 - Role:
0x2d274(block, size)changes a block's size through the far reallocation.0x2d329checks the far heap (0x182b0); a corrupt heap prints "Ram Free Error! Press any key to exit to DOS." and ends the program. Otherwise it returns the larger of the free core and the largest free block of a heap walk, less 16, or 0. - Used by: Allocate and release memory
0x2d0eb, 0x2d21f, 0x17ff2 — Allocate and release memory
- Range:
0x2d0eb–0x2d21e,0x2d21f–0x2d27a;0x17ff2(examined:0x17ff2–0x18012, with0x17ec0,0x17f22and0x1747e) - Status: that these routines come from the compiler's run-time library is inferred.
- Role:
0x180e2allocates from the far heap that0x17ff2releases (same free list and block header, state atcs:0x7eb4–0x7ebe);0x1823bresizes such a block, calling one or the other for a null block or a zero size.0x2d0eb(size, flags)asks0x180e2for size bytes plus 1, or plus 16 when flags has0x20. When that fails it calls0x2e951with (0,0xff) and tries again as long as that call returns non-zero, then once more through the handler atDS:0x629aif one is set; it returns a null pointer if all fail. It writes flags in the byte before the block it returns, which starts on a paragraph boundary when flags has0x20; with0x10it fills the block with zeros, 32,000 bytes at a time. It counts the blocks inDS:0x629e.0x2d21f(block)reads that byte back to find the start and passes it to0x17ff2.0x17ff2releases a far heap block by its segment and does nothing for a null segment (0x17fff). When the block is the last of the heap,0x17ec0drops it and lowers the heap top, shrinking the program's DOS memory block (INT 21h function4Ahat0x17489); otherwise0x17f22marks it free, merges it with free neighbours (0x17f42–0x17f8f) and links it into the free list. Game slot 3 of the driver function table points to0x2d0eband slot 4 to0x2d21f. - Used by: Write FINAL.SAV, Release the creation images
0x30644 — Random number in a range
- Range:
0x30644–0x3068f - Role:
0x30644(a, b)puts its two arguments in order (low, high), draws a value through the random generator, scales it to the size of the range with long-arithmetic helpers and draws again until the result does not exceed high; it returns low plus that value, so a number from low to high inclusive. - Comparative source: REF-SCUMM-012,
the
getRandomNumberRngcalls ofScreen::crossFadeRegion. - Used by: Pixel dissolve
0x17107, 0x1712b — Fill memory
- Ranges:
0x17107–0x1712a,0x1712b–0x17149 - Role: Fill a byte range with a value.
- Used by: Monster slot › From the INF record to the slot, Monster slot › In brief
0x17540, 0x17570, 0x17599, 0x1760d, 0x1767b — String helpers
- Role: Compare, copy, measure, search for a substring and convert to upper case.
- Used by: Dialogue screen › Displaying a text, Dialogue screen › Drawing a picture, Item properties in conditions › Name tests, Initial items (ITEM.DAT)
0x185ff — 32-bit shift
- Range:
0x185ff–0x1861c - Role: Computes
1 << bitas a 32-bit value for the script flag handlers. - Used by: Flag operation targets, Decoding status and unexpected values › Unexpected values
0x19275 — Roll dice
- Range:
0x19275–0x192e0 - Role:
0x19275(count, sides, bonus), three signed bytes, returns the sum of count random values from 1 to sides, plus bonus; with a count or sides of 0 or less, only bonus. Each value is 1 + (n × sides) / 32,768, rounded down, where n is the next number of the random generator (0x19298–0x192b3). The test that follows, which takes sides off a larger value, cannot succeed (0x192ba). - Used by: Damage to a monster › Weapon damage, Condition expression tokens › Operand layouts, Damage characters › Behavior, Item type entry › Damage profiles, Melee attacks › The attack sequence, Move an inventory item to a block › Behavior, Resting › A step of rest, Resting: special cases › Anomalies while resting, Place a monster › Initializing the monster, Print a message › Behavior, Level data (INF), Level 3: reference tables › Remarks and false walls
0x19833, 0x315f0, 0x15a8d — Exit the program
-
Ranges:
0x19833–0x19846,0x315f0–0x31669,0x15a8d–0x15ab1 -
Role:
0x19833(code)calls the shutdown routine0x315f0, which stops the sound driver, removes the mouse handler, checks that no file slot is in use and restores the drive whenDS:0x638eis set (0x2ef6d), restores the timer and keyboard vectors, calls the far pointerDS:0x6282, restores the BIOS video mode if it changed, prints "Memory Corrupt!!!" when the far heap is damaged (0x3164c) and restores INT 3Fh (0x31660), then0x15a8d(code), the Cexit: it runs the registered exit handlers and ends the program (DOS INT 21h function4Ch). Other callers make the same two calls inline. Exits reached in play: the camp menu after "exit the game?" (0x449ad–0x449b5), the defeat sequence (0x1ffb4,0x1ffe8), the end of the game in kill a monster (0x1abe8), the map compression failure in pack a level record (0x4918e), the start menu (0x47163), the copy-protection check (0x4743d), and error paths with code 5: lack of memory (0x2f3ba,0x2d35f), a fatal disk error (0x2a570), an invalid file mode (0x2df05). The other calls, which complete the 21 calls of0x15a8din the file (one of them inside0x19833):0x10102, the run-time start-up: the program's normal end, with the value that the main routine returns as exit code;0x18962, at start-up whenEOSPREFS.DATis missing: "Please run SETUP before running The Legend of Darkmoon." and code 0, before anything is installed;- code 5 after
0x315f0: "Cannot open %s - exiting" and "IO error with %s - exiting" (0x2a648,0x2a677,DS:0x6148,DS:0x6163); "A memory allocation error has occured - exiting" (0x2f3de,DS:0x6231); a file error reported through the message handler atDS:0x6294(0x2dddd); and the display set-up when its driver or font cannot be loaded (0x31976,0x319ae,0x31a2e, the last two namingSTART.EXEthroughDS:0x1cc8); - code 0 after
0x315f0, in the decoration loader of overlay 250: "Error trying to make decoration %d" with its rectangle (0x47815,DS:0x1266), "Too many decoration shapes loaded!" when the shape count passes 300 (0x478a8,DS:0x129f) and "Too many decoration types loaded!" when the type count passes 60 (0x478cb,DS:0x12c3).
None saves anything.
-
Used by: Run a sequence › Protection check, The game loop › How the game ends, Loading, failures and the end of the game › When saving or loading fails, Loading, failures and the end of the game › The end of the game, Saving and restoring
0x1eaa9 — Nth string of a list
- Address:
0x1eaa9(file offset0x11ca9) - Role: Returns the nth null-terminated string of a list, n a 16-bit
count; 65,535 (−1) gives a null pointer. A negative index from a signed
value skips 65,536 + n strings, its pointer wrapping around the 64 KB
segment of the list: a long scan that ends on some string of that segment.
Ring, potion and wand names sign-extend the item value (
0x1d327,0x1d349,0x1d371). Messages and dialogue use it to look up script texts. - Used by: Dialogue screen › Displaying a text, Learning and memorizing spells › Learning from scrolls, Print a message › The message table, Scripted characters, flags and messages › Messages, dialogue, sound and timing
0x2dde7, 0x2e0e6, 0x2e12e, 0x2e2be, 0x2e430, 0x2e7d9, 0x2e84f, 0x30690 — File helpers
-
Role: Open, close, read (DOS INT 21h function
3Fh) and write a file;0x2e430seeks, stopping with message 7 on a free slot and 8 on an illegal origin;0x2e7d9deletes a file;0x2e84fdeletes and creates one;0x30690(name, buffer, size)opens, reads and closes. Overlays reach them through fixed-up far calls. The open helper0x2dde7(name, mode)takes a mode:- 1 opens an existing file for reading. While the file test
does not find it, it shows the disk prompt "Could not find
file" and tries again (
0x2de2f–0x2de3a). - 2 deletes the file (
0x2de71–0x2de88, the only call to the DOS delete wrapper0x1796ais inside it), creates it and opens it for writing. - 3 creates the file only if the file test fails, retrying with the "Could
not create file" prompt on each failure, then opens it for reading and
writing without truncating it (
0x2de90–0x2ded3).
When the open itself fails, the prompt is shown and the open tried again (
0x2dfe9,0x2e002). With all ten handle slots (DS:0xf02c) in use, the program ends with the fatal error "Too many files open." (0x2e0a2). Any other mode shows "Illegal MODE in Open_File." and ends the program with a fatal disk error and exit code 5 (0x2deda–0x2df05). That message gives the routine's name,Open_File, which is probably its original name (inferred). The messages of the other helpers report a close, read, write or seek on an unopened file.0x2e12ereturns the bytes read: fewer than asked on a short file, with no message, and 0 when DOS reports an error (0x2e232–0x2e23c,0x2e2a0–0x2e2b5). A shortFADING.DATthus leaves the rest of its tables as they were.0x2e2bereturns the bytes written, or 0 as soon as DOS writes fewer than asked (0x2e3a8–0x2e3b2,0x2e40e–0x2e41c); the save writer compares each result with the part's size (for example0x49a78–0x49a89) and returns 0 when one differs. - 1 opens an existing file for reading. While the file test
does not find it, it shows the disk prompt "Could not find
file" and tries again (
-
Used by: Level records (LEVELS.TMP) › Lifetime of the file, From files to the game › When a file is missing
0x2e696 — Test whether a file exists
- Range:
0x2e696–0x2e7d8 - Role:
0x2e696(name)returns whether the file can be found, through the file search (0x2e713), which may select another drive. Its error path prints "Internal Error in File_Exists()." (0x2e748), which gives the routine's name,File_Exists, probably its original name (inferred). The table of resident files it consults first (segment0x27d8, 19-byte records) holds a single entry with an empty name, so no file is resident. - Used by: From files to the game › When a file is missing, Read
EOBDATA.SAV
0x2ea8d, 0x2e9eb, 0x2eca6, 0x2ed2c, 0x2eda6, 0x2ef81, 0x2efc4, 0x2e88f, 0x2f1c8, 0x2f21e, 0x2f2e0 — Resident files
- Role: Code that can keep files in memory, driven by the table of
19-byte records in segment
0x27d8: name (far pointer), size at+4, data (far pointer) at+8, a word at+0x0c(a value returned to the disk prompt or, with bit 15, the index of the record that holds the file), read position at+0x0e, flags at+0x12(0x80load at the start, low 6 bits a rank). The table holds a single entry with an empty name, so none of this applies to any file. What takes effect is the start of0x2ea8d, which frees the ten file slots, and the end of0x2e9eb, which on exit shows message 11 of the file errors for a slot still in use.0x2ea8dalso orders the records marked0x80and loads them with0x2efc4.0x2eca6(name)tells whether a file is loaded,0x2ed2c(name)gives its record,0x2eda6reads from memory and advances the position,0x2e88fpicks a loaded file to release,0x2e951releases files until a request can be met, for the allocator, and0x2f21e(name)follows+0x0cto the record that holds the file.0x2f1c8(name)gives a record or −1,0x2ef81(name)loads a file marked0x40, and0x2f2e0looks for a file in the directory of the file that holds it. - Used by:
0x2eeaf — Search the drives for a file
- Range:
0x2eeaf–0x2ef5b, with0x2ef5cand0x2ef6d - Status: that an empty floppy drive ends as a failed open, so that the search moves on to the next drive, is inferred from the critical-error handler.
- Role: Looks for the file in the current directory of the current drive
(
0x2eedc). When it is not there and the drive search is enabled, it tries every other drive from the last drive down toA:, skipping the letters that the drive check0x32667(INT 21h function440Eh) refuses, and returns the drive's number plus 1 with that drive left selected; when the file is nowhere, it selects the original drive again and returns 0 (0x2ef02–0x2ef54).0x2ef5c, called at start-up (0x315a2), saves the drive in use inDS:0xf040;0x2ef6d, called by the shutdown (0x3160b), selects it again. - Used by: From files to the game › When a file is missing, Refresh the slot list, Load or build the fading tables
0x2dd45, 0x2a3d6 — Disk prompt
- Ranges:
0x2dd45–0x2dda8;0x2a3d6–0x2a5f5 - Role:
0x2dd45(kind, name)strips the path from the name, turns it into capitals and calls the far pointerDS:0x62a1, which the main routine sets to0x2a3d6(0x18a42–0x18a4d).0x2a3d6(kind, name)saves the screen area, draws a box and prints message kind with the name; for kinds 0 ("Could not create file '%s' on disk.") and 2 ("Could not find file '%s'.") it adds "Insert the disk containing '%s' in any drive." (DS:0x6118,0x2a50f–0x2a537). It then waits for a key (0x2a53f). Escape prints message 12, "A fatal disk error has occurred. Now returning to DOS.", waits for a key, shuts the game down and exits with code 5 (0x2a547–0x2a570); any other key restores the screen and returns 1, so the caller tries again (0x2a5ed). - Used by: From files to the game › When a file is missing
0x2dda9, 0x2a5f6 — Fatal file error
- Ranges:
0x2dda9–0x2dde6;0x2a5f6–0x2a623 - Role:
0x2dda9(kind, name)calls the far pointerDS:0x6294, set by the main routine to0x2a5f6(0x18a36–0x18a3c), which shows the disk prompt for kind, then message 12, and ends the program with exit code 5. - Used by: From files to the game › When a file is missing
0x33744 — Far memory copy
- Range:
0x33744–0x337b0 - Role: Arguments: a far source, a far destination and a 32-bit count of
bytes. Normalizes both pointers (
0x337c0), copies the low 16 bits of the count as words and a last byte (0x3377a–0x33782), then, once per unit of the high 16 bits, normalizes again and copies 65,536 bytes (0x33783–0x337a5). It preserves every register and returns nothing. - Used by: Monster slot › Copying runtime state
Small helpers
- Role:
0x19578(a, b)swaps two bytes through far pointers, for the spell slot sort (0x20fca).0x1989f(value, min, max, wrap)limits a value: below min it becomes max when wrap is set, min otherwise; above max it becomes min when wrap is set, max otherwise. The spell-book buttons use it with 0, 9 and wrap (0x22b6b).0x308c0does nothing; the start-up calls it with two arguments (0x18c48).0x328f7enables interrupts and0x328f9disables them.0x32466(text)writes a string with DOS function06h; the error paths call it (0x18957,0x2d349,0x2f3a4,0x31393,0x314c6,0x3164c).0x250ab(block)returns 1 when monsters occupy a block.0x28b65is a second copy of point in a rectangle, used by the menus and the two-choice question.0x2f2ccand0x2f2d6clear and set the drive-search switch;0x32632reads the number of drives for it.0x2f3e9(page)returns the address of a work page: offset 0 and the segmentDS:0x62a7holds for the odd page of its pair.0x33df6and0x33e05swap the bytes of a double word and a word, for the big-endian numbers of an IFF file.0x32882removes the mouse handler (INT 33h,AX=0Ch), and0x333e1restores INT 23h and INT 09h; the shutdown calls them (0x315ff,0x31615).0x337b1and0x337c0normalize a far pointer, carrying the high bits of the offset into the segment.0x3404creturns the vertical-retrace bit of port0x3da.
- Used by: Sort a character's spell slots, Spell-book buttons
0x2b7ba–0x2c94a — Overlay manager
- Range:
0x2b7ba–0x2c9f8 - Status: that these routines belong to the compiler's overlay manager is inferred. They are identified only as far as needed to set them aside.
- Role: Load overlays on demand and keep them in memory.
0x2b7baopens the overlay file, looking for the program by its own path (0x2b969,0x2b9a9), and installs theINT 3Fhhandler (0x2b8ea), whose entry is0x2bc95.0x2bd45and the routines around it place an overlay in the buffer and patch the stubs (0x2be34,0x2be52). Others extend the buffer into memory beyond the conventional 640 KB.0x318e3, which ends inIRET, wrapsINT 3Fh: it closes the program file, opens it again by its full name (DS:0xf108), searching the drives and asking for the disk if needed, and ends the program on failure, then calls the previous handler (DS:0xf1c0). No call reaches0x2bf66and0x2c4c8, which belong to the same code.0x2c66elooks for extended memory (INT 2Fh4300h, INT 15h88h, theVDISKsignature),0x2c81ereserves it,0x2c94amoves blocks (XMS, or INT 15h87h), and0x2c5deand0x2c643use expanded memory (INT 67h). The game's only direct call is at0x317f5, to0x2c256. - Used by: Overlays
Routines with no known reference
- Status: no call, far pointer or address built in code was found for these routines, apart from the calls between them noted below. Whether anything reaches them is unknown.
- Role:
0x1a324does nothing.0x223aband0x223ddmove the selected line of the spell book by a page.0x2b6eatranslates bytes through the table of driver slot 53.0x2d570shows a hidden button.0x2da37,0x2db7a,0x2dbbd,0x2dc00and0x2dc43wrap driver slots that only return, in the form of the shape wrappers.0x24b37is a magic object hit of 12 points with no dice. The spell definitions do not name it.0x2a624and0x2a653print "Cannot open %s - exiting" and "IO error with %s - exiting" and end the program;0x2f391and0x2f3c5do the same for "Insufficient memory error! Press any key to exit to DOS." and "A memory allocation error has occured - exiting".0x2b694(block, block, …)looks for the second block among the offsets ofDS:0x6190from the first.0x2d44c,0x2d53e,0x2d5d5and0x2d6f5toggle, hide, release and press a button, redraw it, and can call its callback.0x31136sets one colour of a palette through the driver, and0x3118afades a palette toward another, in the VGA mode only.0x198d2draws a string and calls0x195f4, which prints the free memory, or "HEAP CORRUPT!! (free memory) = %lu", and can wait for a key. Both look like debugging aids (inferred).0x19521draws a bevelled rectangle with four lines;0x196eadraws a number with a black shadow.0x19867opens a file, reads it into a buffer and closes it.0x1bb61returns the word at+4of a far record.0x1beb0moves the selected hand slot up or down between 0 and 2.0x1c24estepsDS:0xcb76between 1 and 2. Both call0x1e2b5and0x1e2c6, which do nothing.0x2d7e7inserts a button in a list ordered by its+4word.0x2d8d4tests the clear bit0x08of a button's+0x0e, and0x2d8f4and0x2d90fthe bits0x01and0x02of its+0x2e.0x2dce5returns the DOS handle of a file slot.0x2dd0bcalls the library routine0x16b65on one.0x2f405returns the address of a work page, an odd number rounded down.0x2f4e1looks for a chunk of an IFF file; the picture loader uses0x2f609instead.0x2f86breads a pending key, adds bit0x8000, makes a letter capital and calls the button processor.0x31d6f(value)prints a number through the text output.0x3340a(code)appends an event to the input queue, with the mouse position for the codes41hand42h.0x33c8fcalls0x33c16under the lockDS:0x6610.
- Used by: none known
0x10008–0x187b2 — Run-time library
- Range:
0x10008–0x10250,0x15a5f–0x187b2, apart from the routines with their own entries - Status: that these routines come from the compiler's library is inferred from the string "Borland C++ - Copyright 1991 Borland Intl." and their calling form, and each role from the DOS function it calls or from how the game uses it. They are identified only as far as needed to set them aside.
- Role:
- Start-up and exit:
0x10008(DOS version),0x10029(program memory and environment),0x10107,0x101b2(start-up and exit tables),0x10185(restore vectors),0x101f5(write to the error handle),0x10127(terminate). - DOS functions: date and time
0x15f22,0x15f35,0x1749a,0x174af; files0x16b48,0x16b65(change a size),0x16c2c(close),0x16cc7(length),0x16f28,0x16f8e,0x16f9f(seek),0x171bd(create),0x171d8,0x17336(open),0x17442(read),0x1796a(delete),0x17e69(write); drives and directories0x16b1d,0x16b35,0x16b3b,0x16d08; vectors0x16d95,0x16da4; memory0x1747e; any function,0x16b0d. - Files with buffers:
0x15a5f(close),0x171ea(open),0x17384(read), and the stream routines0x15c30–0x16461,0x1657c,0x1691f–0x16afc,0x1719c,0x1745e. - Formatted output:
0x15f48,0x164a1(format into a buffer),0x164c3,0x1799a,0x179b4–0x179ca,0x17982–0x17992,0x1871d,0x187b2. - Clock:
0x164e4,0x16524,0x16596,0x166c3,0x1769f,0x1788b; waits0x16c4a,0x16c86. - Memory:
0x15ab2–0x15bde(near heap and free core),0x17ec0–0x18242(far heap, with allocation),0x182b0(check),0x183ab–0x184c0(check, fill and walk). - Long and pointer arithmetic:
0x18537,0x186e5(multiply),0x18620(shift),0x1863e–0x1869e,0x186fc(huge pointers). The 32-bit divisions share one body (0x1856b–0x185ec):0x18551gives the signed quotient,0x18558the unsigned quotient and0x1855dthe signed remainder, and0x1854eis the form of0x18551for a near call. A signed quotient rounds toward zero and a signed remainder takes the dividend's sign (0x18590–0x185e6).0x186c9copies a structure: CX bytes, words first, from the far pointer given first to the far pointer given second, and removes both pointers from the stack. - Strings and numbers:
0x168c7,0x168f3(case),0x16d25(environment),0x16fc8–0x17096(numbers to text),0x170c0–0x1714a(memory),0x174c4,0x17503,0x175b8,0x175dc(see also the string helpers). - Errors:
0x16e10,0x16e2b(with the critical-error handler),0x16e8f(with the interrupt call),0x16f51(DOS error number).
- Start-up and exit:
- Used by: Allocate and release memory, File helpers