Fundamentals 26 min read

Exploring Lei Jun’s Elegant 1994 DOS TSR Assembly: Full Source and Analysis

This article presents the complete 1994 assembly source of Lei Jun’s RAMinit TSR program, explains its background, key functions such as hot‑key handling, interrupt vector replacement, and memory management, and highlights its limitations to real‑mode DOS environments.

ITPUB
ITPUB
ITPUB
Exploring Lei Jun’s Elegant 1994 DOS TSR Assembly: Full Source and Analysis

The post shares the full 16‑bit assembly source of the RAMinit TSR (Terminate‑and‑Stay‑Resident) utility written by Lei Jun in 1994, originally posted on a Chinese programmer community. The program can remove other TSRs, restore system state, and respond to a configurable hot‑key, making it a compact system‑utility for DOS.

Revision Header and Metadata

; RI.ASM Revision 2.12 [ July 12, 1994 ]
Revision equ 'V2.12 '
; RAMinit Release 2.0
; Copyright (c) 1989-1994 by Yellow Rose Software Co.
; Written by Mr. Leijun
; Function: Press HotKey to remove all TSR program after this program

The header lists the version, release date, copyright, and a brief description of the program’s purpose.

Hot‑Key Detection and Status Flags

HotKey db LeftCtrl or RightCtrl
AuxHotKey db 0 ; auxiliary hot‑key (default is "X")
Flag db '!'
StopFlag db 0

Hot‑key status is stored in HotKey and AuxHotKey. The flag byte indicates whether the TSR is busy handling a hot‑key press.

Interrupt Vector Hooking

SetSelfInt:
    ; Save original INT 9, INT 2F, INT 1C vectors
    mov ax,3509h   ; Get old INT 9 vector
    int 21h
    mov word ptr cs:oldInt9_addr,bx
    mov word ptr cs:oldInt9_addr[2],es
    ; Install new vectors
    mov dx,offset NewInt9
    mov ax,2509h
    int 21h
    ; Repeat for INT 2F and INT 1C

The routine saves the original interrupt vectors for keyboard (INT 9), DOS multiplex (INT 2F), and timer (INT 1C), then installs the program’s own handlers ( NewInt9, newINT2F, newINT1C).

Keyboard Handler Logic

newINT9:
    pushf
    db 9ah ; call far ptr oldint9_addr
    ; ... check busy flag, test hot‑key bits, invoke Beep, etc.
    iret

The new keyboard ISR checks the status flag, evaluates the pressed modifier keys (Ctrl, Alt, Shift), compares them with the configured hot‑key, and if matched, triggers the TSR removal routine.

TSR Removal and Restoration

RemoveTSR:
    ; Disable interrupts, switch stacks, and call cleanup subroutines
    call Init8259
    call RestoreBiosData
    call RestoreMemoryManager
    ; ... restore original interrupt vectors and environment
    int 27h ; terminate TSR

The removal routine restores the 8259 PIC, BIOS data, XMS/EMS status, and all hooked interrupt vectors before terminating the TSR with INT 27h.

Memory Management (XMS/EMS)

XMS_test:
    mov ax,4300h
    int 2fh
    ; detect XMS driver and store entry point in XMS_control
EMS_test:
    mov dx,offset EMMname
    mov ax,3d00h
    int 21h
    ; if EMM driver present, set EMSbit

The program probes for XMS and EMS drivers, saves their status, and can deallocate or lock handles during cleanup.

BIOS and DOS Environment Backup

BackupBiosData:
    mov ax,'--'
    stosw
    ; copy BIOS data area (40:10‑40:A8, 40:49‑40:5D, etc.)
BackupMemoryManager:
    ; save XMS and EMS status structures

Before modifying system state, the TSR backs up BIOS data, COM/LPT ports, LED status, and the DOS environment segment.

Hot‑Key Configuration and User Interaction

SetHotKey:
    ; parse command‑line /S options to set HotKey and AuxHotKey
    ; write configuration into the running executable for persistence

The utility can be invoked with command‑line switches (e.g., /S1c) to define which modifier keys constitute the hot‑key.

Limitations

The program runs only under real‑mode DOS; it aborts with an error message when executed inside Windows DOS boxes or on systems lacking the required XMS/EMS drivers. It also assumes a PC/AT or compatible hardware configuration.

Conclusion

Lei Jun’s RAMinit TSR showcases a compact, well‑commented assembly implementation for managing TSRs via hot‑key control, providing a valuable historical example of low‑level DOS programming and interrupt handling.

Legacy CodeDOSTSRRetro Programming
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.