From Duncan Drysdale · 1996-04-05 16:03 · #69242 in DRIVING
Hi Graham,
You:Graham Williamson Dared write to All<G>
About: TSR`s
On 25-Mar-96 21:02:09
GW> Hi Everybody!
GW> I have been trying to work out how to make a tsr in Assembly language.
GW> Unfortunatly my attempts have failed miserably. Could somebody out there
GW> inform
GW> me of how to do this and possibly send me a small example if needed?
No bother....( Dont read any further if you are not using 80x86 PC..<G>)
Ok here is the thinkng behind it, this may be a bit patronising, if so
them i', sorry..
TSR's usualy are based round interrupts. You execute some code in
memory, hook an interrupt and then terminate and stay resident. Let
me explain....
Interrupts. The main one that you can hook into is Int 0x1c which is
the user interrupt. It is called 18.2 times per second from the
hardware timer tick interrupt, 0x8. As is say 0x1c is the main one,
if you plan on being nice that that is, otherwise just hit 0x8, its
up to you. The next thing you have to think about is wether or not
you are going to call the old interrupt handler? That can be tricky.
If you hook timer itck ( 0x8 ), dos uses this to update the system
time and the likes, so it would be better of you called the old
interrupt. If you use 0x1c then it is completley up to you what
you do, if its a commercial app then i would go on and call the
old handler, if its a game and you want to be nasty, and know exactly
what code is being executed, stick with an IRET ( Interrupt return, more
follows ). Books suggest that you only set a flag from 0x1c and
hook the main body into the Dosok chaing ( 0x28 ) which is called
during times when it is safe to perform certain dos functions such
as disk access and the likes. These times usually occur when Dos is
waiting for input.
Code.
This is the easy part. One you have decided which interrupt and
wheter you will be nice or nasty you can use certain dos functions
to get and set Interrupt vectors. Although you could poke the
addresses of you new handlers into memory directly it is never a
good idea, for many reasons. One being that the interrupt vector
that you are updating could be called when you are only half
way through updating it, thus the pointer would point to usless
code, and probly a systemm crash. Therefor use the system functions
0x25 and 0x35, Set and Get interrupt vectors respectivly. The registers
that require setting are as follows...
Int 0x21 Function 0x25 : Set interrupt vector
Calling
ah = 0x25
al = Interrupt number ( in our case lets say 0x1c ? )
DS:DX = Seg:Offset of the new routine.
Return
None
Int 0x21 Function 0x35 : Get interrupt vector
Calling
ah = 0x35
al = Interrupt number ( in our case lets say 0x1c ? )
Return
ES:BX = Pointer to interrupt handler
Terminate And Stay Resident.
This is a DOS call that is used to quit a program and leave the memory
that was used by the program reserved. That is NOT return it to the
transiant memory area of Dos. For the purposes of our little example
we will be using a com file so we firstly have to shrink memory
allocated as COM files have all avaliable resources asigned to them at
start up....
Pseudocode....
I've decided not to post menemonic for menemonic the exact routine, but
the pseudocode will show you what to be getting up to...BTW its a mixture.
org 100h;
jmp start
mynew1c:
jmp cont mynewic
oldoff dw 0
oldseg dw 0
mynew1c:
; Do all of the code you want to in here.
; To start with just update the byte at location 0xb800:0, just to
; get the feel for things..
; If you are going to call the old routine you will need this
jmp far dword [ cs:oldoff ]
; Otherwise just use this
iret ; Interrupt return. just like a RETF but pops the flags.
lenofhandler EQU $ + 0100h
start:
Get Interrupt vector 0x1c ( Do that dos call thing... )
Store es into oldseg
Store bx into oldoff
Set interrupt vector 0x1c ( Do that dos call thing... )
*Shrink memory*
*Terminate and stay resident*
LENOFCODE equ $ + 0100h
Shrink Memory
-------------
; At the end of the com file have an EQU that is
; LENOFCODE equ $ + 0x100
push cs
pop es ; Set the es to the current cs, just in
; case.
mov bx,LENOFCODE
add bx,16 ; Add a paragraph
shr bx,4 ; Divide the length by 16
mov ax,04a00h
int 021h ; Modify memory allocation
; Check here for the carry flag if you want, it indicates an error.
ret
Terminate and stay resident
---------------------------
mov ah,031h ; Function ( TSR )
mov al,0 ; Return code
mov bx,lenofhandler ; Lenght of handler code
add bx,16 ; Add a paragraph
shr bx,16 ; Convert to paragrphs
mov dx,bx ; Set the call register
int 021h ; Quit out please
-------------------------------------------------------------
Hope all of that mumbling is of some help you you, i'll post
the exact code for you if you dont have and luck.
BTW a paragraph is 16 bytes. Took me ages to work that one out....
Regards,
Duncan
Co-sysop: Blue Lagoon BBS (0141-644-1688, 2 Line Ring Down )
--- Terminate 3.00/Pro
* Origin: The NEW Terminate will -=> FAX <=- almost anything! (2:259/68.2)