我一直在尝试使用16位汇编语言为MS-DOS编写TSR(Terminate-Stay-Resident)程序。我阅读了维基百科关于TSR的页面,以及有关在DOS中使用它的页面(但它似乎是用C而不是直接使用汇编教学)。我查看了一个包含大量DOS中断文档的网站,并找到了这个、这个和另一个与TSR程序最相关的链接。由于我是新用户,因此无法在帖子中发布所有链接,最多只能有2个超链接。
当我在DOS中组装并执行此代码时,它不会返回到DOS提示符,而是挂起系统(除了硬件光标在最后一个提示下方闪烁外没有任何活动)。我想内存垃圾可能正在执行,但您知道要点。
请问有人能帮忙找出此代码的问题和/或提供有关在DOS中编写TSR代码的一般建议吗?非常感谢您的帮助!
所以,我尝试在NASM中以实模式平坦模型(.COM文件格式)编写一个(看起来)非常简单的TSR程序。以下是代码:
[BITS 16]
[ORG 0x0100]
[SECTION .text]
Start:
; Get current interrupt handler for INT 21h
mov AX,3521h ; DOS function 35h GET INTERRUPT VECTOR for interrupt 21h
int 21h ; Call DOS (Current interrupt handler returned in ES:BX)
mov WORD [v21HandlerSegment],ES ; Store the current INT 21h handler segment
mov WORD [v21HandlerOffset],BX ; Store the current INT 21h handler offset
; Write new interrupt handler for INT 21h
mov AX,2521h ; DOS function 25h SET INTERRUPT VECTOR for interrupt 21h
mov DX,TSRStart ; Load DX with the offset address of the start of this TSR program
; DS already contains the segment address, it is the same as CS in this .COM file
int 21h ; Override the INT 21h handler with this TSR program
; The TSR program will be called even when this portion uses INT 21h to terminate and stay resident
mov AX,3100h ; DOS function TSR, return code 00h
mov DX,00FFh ; I don't know how many paragraphs to keep resident, so keep a bunch
int 21h ; Call our own TSR program first, then call DOS
TSRStart:
push WORD [v21HandlerSegment] ; Push the far address of the original
push WORD [v21HandlerOffset] ; INT 21h handler onto the stack
retf ; Jump to it!
[SECTION .data]
v21HandlerSegment dw 0000h
v21HandlerOffset dw 0000h
当我在DOS中组装并执行此代码时,它不会返回到DOS提示符,而是挂起系统(除了硬件光标在最后一个提示下方闪烁外没有任何活动)。我想内存垃圾可能正在执行,但您知道要点。
请问有人能帮忙找出此代码的问题和/或提供有关在DOS中编写TSR代码的一般建议吗?非常感谢您的帮助!