问题
I've been trying to write TSR (Terminate-Stay-Resident) programs (in general) in Assembly (16-bit) for MS-DOS. I've read through a Wikipedia page on TSR and also a page on using it specifically in DOS (but it seems to be teaching it in C and not Assembly directly). I've looked at a site with tons of DOS interrupt documentation and find this one, this one, and another most relevant to TSR programs. I can't post all of the links because as a new user I can have up to 2 hyperlinks on a post.
So, I've tried writing a (seemingly) very simple TSR program in real mode flat model (.COM file format) in NASM. Here's the code:
[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
When I assemble this and execute it inside DOS, instead of returning back to the DOS prompt it hangs the system (no activity occurs except the hardware cursor just blinks below the last prompt). I guess memory garbage might be executing but you get the point.
Could anybody please help to either figure out what the problem with this code is and / or offer general advice for coding TSR's in DOS? Thanks in advance, any help is very much appreciated!
回答1:
I figured it out. After looking through a couple more sources, I discovered that this code:
push WORD [v21HandlerSegment] ; Push the far address of the original
push WORD [v21HandlerOffset] ; INT 21h handler onto the stack
needs to be something like this:
push WORD [CS:v21HandlerSegment] ; Push the far address of the original
push WORD [CS:v21HandlerOffset] ; INT 21h handler onto the stack
because those memory references are referencing from the data segment, which isn't set up from the caller of the TSR. So basically I was referencing data from something else's data block...
This can also be accomplished by putting CS in DS (and then putting DS's original value back) like this:
push DS
push CS
pop DS
; Memory references....
pop DS
回答2:
You need to use a
cs:
segment override to access the TSR's data from within a general-purpose interrupt handler, because theds
value is an arbitrary user's register then.You don't need to push the next handler's address onto the stack then jump with a
retf
. It is simpler to dojmp far [cs:...]
(and that has a shorter encoding). But your method works fine too.You can put your initialisation handling (not needed in the resident installed handler) at the end of your program image. This is a trivial optimisation of the TSR's size.
To calculate the size of your resident process, use NASM labels. To allow shift (or divide) operations needed to figure out a length in paragraphs, only use deltas of labels. A delta (difference) is a scalar value to NASM, so can be used in calculations. A (non-struc) label alone is not a scalar.
Here's an example using all of these:
cpu 8086
bits 16
org 256
start:
jmp init
align 4
int21old:
dd 0
int21handler:
jmp far [cs:int21old]
end_of_resident:
init:
mov ax, 3521h
int 21h
mov word [int21old + 2], es
mov word [int21old], bx
mov ax, 2521h
mov dx, int21handler
int 21h
mov ax, 3100h
mov dx, (end_of_resident - start + 256 + 15) >> 4
int 21h
The size calculation computes the delta of two labels, adds in 256 for the process's PSP (same as the org 256
), adds 15 to make the shift-division round up, then shifts down into an amount of paragraphs.
来源:https://stackoverflow.com/questions/6854117/help-writing-tsr-programs-in-nasm-assembly-for-dos