Skip to main content

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 0x10142 saves the vectors of interrupts 0, 4, 5 and 6 and makes 0x10139 the handler of interrupt 0, which the processor raises on a division error. 0x10139 writes the 14 characters at DS:0x2f, "Divide error" and a line end (0x101f5), and ends the program with exit code 3 through DOS function 0x4c (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: 0x162a3 multiplies the 32-bit seed DS:0x6962 by 0x015a4e35 (long multiply 0x186e5), 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 by 0x16524(0), which reads the date and time (inferred). Only random number in a range and roll dice (0x19298) call 0x162a3.
  • 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. 0x2d329 checks 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, with 0x17ec0, 0x17f22 and 0x1747e)
  • Status: that these routines come from the compiler's run-time library is inferred.
  • Role: 0x180e2 allocates from the far heap that 0x17ff2 releases (same free list and block header, state at cs:0x7eb4–0x7ebe); 0x1823b resizes such a block, calling one or the other for a null block or a zero size. 0x2d0eb(size, flags) asks 0x180e2 for size bytes plus 1, or plus 16 when flags has 0x20. When that fails it calls 0x2e951 with (0, 0xff) and tries again as long as that call returns non-zero, then once more through the handler at DS:0x629a if 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 has 0x20; with 0x10 it fills the block with zeros, 32,000 bytes at a time. It counts the blocks in DS:0x629e. 0x2d21f(block) reads that byte back to find the start and passes it to 0x17ff2. 0x17ff2 releases a far heap block by its segment and does nothing for a null segment (0x17fff). When the block is the last of the heap, 0x17ec0 drops it and lowers the heap top, shrinking the program's DOS memory block (INT 21h function 4Ah at 0x17489); otherwise 0x17f22 marks 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 to 0x2d0eb and slot 4 to 0x2d21f.
  • 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 getRandomNumberRng calls of Screen::crossFadeRegion.
  • Used by: Pixel dissolve

0x17107, 0x1712b — Fill memory​

0x17540, 0x17570, 0x17599, 0x1760d, 0x1767b — String helpers​

0x185ff — 32-bit shift​

0x19275 — Roll dice​

0x19833, 0x315f0, 0x15a8d — Exit the program​

  • Ranges: 0x19833–0x19846, 0x315f0–0x31669, 0x15a8d–0x15ab1

  • Role: 0x19833(code) calls the shutdown routine 0x315f0, which stops the sound driver, removes the mouse handler, checks that no file slot is in use and restores the drive when DS:0x638e is set (0x2ef6d), restores the timer and keyboard vectors, calls the far pointer DS:0x6282, restores the BIOS video mode if it changed, prints "Memory Corrupt!!!" when the far heap is damaged (0x3164c) and restores INT 3Fh (0x31660), then 0x15a8d(code), the C exit: it runs the registered exit handlers and ends the program (DOS INT 21h function 4Ch). 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 of 0x15a8d in the file (one of them inside 0x19833):

    • 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 when EOSPREFS.DAT is 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 at DS:0x6294 (0x2dddd); and the display set-up when its driver or font cannot be loaded (0x31976, 0x319ae, 0x31a2e, the last two naming START.EXE through DS: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​

0x2dde7, 0x2e0e6, 0x2e12e, 0x2e2be, 0x2e430, 0x2e7d9, 0x2e84f, 0x30690 — File helpers​

  • Role: Open, close, read (DOS INT 21h function 3Fh) and write a file; 0x2e430 seeks, stopping with message 7 on a free slot and 8 on an illegal origin; 0x2e7d9 deletes a file; 0x2e84f deletes and creates one; 0x30690(name, buffer, size) opens, reads and closes. Overlays reach them through fixed-up far calls. The open helper 0x2dde7(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 wrapper 0x1796a is 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.

    0x2e12e returns 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 short FADING.DAT thus leaves the rest of its tables as they were. 0x2e2be returns 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 example 0x49a78–0x49a89) and returns 0 when one differs.

  • 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 (segment 0x27d8, 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 (0x80 load 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 of 0x2ea8d, which frees the ten file slots, and the end of 0x2e9eb, which on exit shows message 11 of the file errors for a slot still in use. 0x2ea8d also orders the records marked 0x80 and loads them with 0x2efc4. 0x2eca6(name) tells whether a file is loaded, 0x2ed2c(name) gives its record, 0x2eda6 reads from memory and advances the position, 0x2e88f picks a loaded file to release, 0x2e951 releases files until a request can be met, for the allocator, and 0x2f21e(name) follows +0x0c to the record that holds the file. 0x2f1c8(name) gives a record or −1, 0x2ef81(name) loads a file marked 0x40, and 0x2f2e0 looks for a file in the directory of the file that holds it.
  • Used by:

0x2eeaf — Search the drives for a file​

  • Range: 0x2eeaf–0x2ef5b, with 0x2ef5c and 0x2ef6d
  • 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 to A:, skipping the letters that the drive check 0x32667 (INT 21h function 440Eh) 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 in DS: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 pointer DS:0x62a1, which the main routine sets to 0x2a3d6 (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 pointer DS:0x6294, set by the main routine to 0x2a5f6 (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).
    • 0x308c0 does nothing; the start-up calls it with two arguments (0x18c48).
    • 0x328f7 enables interrupts and 0x328f9 disables them.
    • 0x32466(text) writes a string with DOS function 06h; the error paths call it (0x18957, 0x2d349, 0x2f3a4, 0x31393, 0x314c6, 0x3164c).
    • 0x250ab(block) returns 1 when monsters occupy a block.
    • 0x28b65 is a second copy of point in a rectangle, used by the menus and the two-choice question.
    • 0x2f2cc and 0x2f2d6 clear and set the drive-search switch; 0x32632 reads the number of drives for it.
    • 0x2f3e9(page) returns the address of a work page: offset 0 and the segment DS:0x62a7 holds for the odd page of its pair.
    • 0x33df6 and 0x33e05 swap the bytes of a double word and a word, for the big-endian numbers of an IFF file.
    • 0x32882 removes the mouse handler (INT 33h, AX = 0Ch), and 0x333e1 restores INT 23h and INT 09h; the shutdown calls them (0x315ff, 0x31615).
    • 0x337b1 and 0x337c0 normalize a far pointer, carrying the high bits of the offset into the segment.
    • 0x3404c returns the vertical-retrace bit of port 0x3da.
  • 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. 0x2b7ba opens the overlay file, looking for the program by its own path (0x2b969, 0x2b9a9), and installs the INT 3Fh handler (0x2b8ea), whose entry is 0x2bc95. 0x2bd45 and 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 in IRET, wraps INT 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 reaches 0x2bf66 and 0x2c4c8, which belong to the same code. 0x2c66e looks for extended memory (INT 2Fh 4300h, INT 15h 88h, the VDISK signature), 0x2c81e reserves it, 0x2c94a moves blocks (XMS, or INT 15h 87h), and 0x2c5de and 0x2c643 use expanded memory (INT 67h). The game's only direct call is at 0x317f5, to 0x2c256.
  • 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:
    • 0x1a324 does nothing.
    • 0x223ab and 0x223dd move the selected line of the spell book by a page.
    • 0x2b6ea translates bytes through the table of driver slot 53.
    • 0x2d570 shows a hidden button.
    • 0x2da37, 0x2db7a, 0x2dbbd, 0x2dc00 and 0x2dc43 wrap driver slots that only return, in the form of the shape wrappers.
    • 0x24b37 is a magic object hit of 12 points with no dice. The spell definitions do not name it.
    • 0x2a624 and 0x2a653 print "Cannot open %s - exiting" and "IO error with %s - exiting" and end the program; 0x2f391 and 0x2f3c5 do 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 of DS:0x6190 from the first.
    • 0x2d44c, 0x2d53e, 0x2d5d5 and 0x2d6f5 toggle, hide, release and press a button, redraw it, and can call its callback.
    • 0x31136 sets one colour of a palette through the driver, and 0x3118a fades a palette toward another, in the VGA mode only.
    • 0x198d2 draws a string and calls 0x195f4, which prints the free memory, or "HEAP CORRUPT!! (free memory) = %lu", and can wait for a key. Both look like debugging aids (inferred).
    • 0x19521 draws a bevelled rectangle with four lines; 0x196ea draws a number with a black shadow.
    • 0x19867 opens a file, reads it into a buffer and closes it.
    • 0x1bb61 returns the word at +4 of a far record.
    • 0x1beb0 moves the selected hand slot up or down between 0 and 2. 0x1c24e steps DS:0xcb76 between 1 and 2. Both call 0x1e2b5 and 0x1e2c6, which do nothing.
    • 0x2d7e7 inserts a button in a list ordered by its +4 word. 0x2d8d4 tests the clear bit 0x08 of a button's +0x0e, and 0x2d8f4 and 0x2d90f the bits 0x01 and 0x02 of its +0x2e.
    • 0x2dce5 returns the DOS handle of a file slot. 0x2dd0b calls the library routine 0x16b65 on one.
    • 0x2f405 returns the address of a work page, an odd number rounded down.
    • 0x2f4e1 looks for a chunk of an IFF file; the picture loader uses 0x2f609 instead.
    • 0x2f86b reads a pending key, adds bit 0x8000, 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 codes 41h and 42h.
    • 0x33c8f calls 0x33c16 under the lock DS: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; files 0x16b48, 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 directories 0x16b1d, 0x16b35, 0x16b3b, 0x16d08; vectors 0x16d95, 0x16da4; memory 0x1747e; any function, 0x16b0d.
    • Files with buffers: 0x15a5f (close), 0x171ea (open), 0x17384 (read), and the stream routines 0x15c30–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; waits 0x16c4a, 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): 0x18551 gives the signed quotient, 0x18558 the unsigned quotient and 0x1855d the signed remainder, and 0x1854e is the form of 0x18551 for a near call. A signed quotient rounds toward zero and a signed remainder takes the dividend's sign (0x18590–0x185e6). 0x186c9 copies 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).
  • Used by: Allocate and release memory, File helpers