News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Interrupt Hooking in MS DOS

Started by Shintaro, August 10, 2021, 01:43:05 PM

Previous topic - Next topic

Shintaro

Hi,

I am trying to find a simple example of hooking an interrupt in MS DOS.
Does anyone have any pointers to an article or even sample code.

Regards.
Andrew
"Wyrd bið ful āræd. Fate is inexorable."

FORTRANS

Hi,

   Here is some code from a program I wrote.  A game
that I thought would need a faster response to any inputs.
Notes reference books by Ray Duncan and The Waite
Group.  A lot of code was simplified or edited out.

; - - - - - -
DATASG  SEGMENT PUBLIC
; Interrupt Handlers
OldInt09 DW     2 DUP (?)
KeyFlag DB      0
OldInt1B DW     2 DUP (?)
OldInt1C DW     2 DUP (?)

; - - - - - -
; Interrupt Handlers
EekMsg1 DB      13,10,'Inconsistentcy detected when restoring Int 1C BIOS timer vector! '
        DB      13,10,'Please reboot now! $'
EekMsg2 DB      13,10,'Inconsistentcy detected when restoring Int 9 keyboard vector! '
        DB      13,10,'Please reboot now! $'

DATASG  ENDS
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CODE    SEGMENT PUBLIC
        ASSUME  CS:CODE,DS:DATASG,ES:NOTHING,SS:STCKSG

START   PROC FAR        ; PROC needed for RET typing
        MOV     AX,SEG DATASG
        MOV     DS,AX
; Start up of program...

; - - - - -
; Set up the keyboard/timer IRQ handlers
        CALL IntSetup   ; Setup interrupt handlers

; - - - - -
; Restore IRQ handlers and terminate
        JMP     Quit

START   ENDP

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IntSetup PROC NEAR
;( Most code left out to simplify. )

; SCALL is a macro to generate DOS Int 21H.
; GIVEC is function 35H
; SIVEC is function 25H
; OUTSTR is function 09H
; EXIT is function 4CH

;       Get old int 1CH
        MOV     AL,1CH                  ; Function 35 interrupt 1C
        SCALL   GIVEC                   ; Get Interrupt VECtor
        MOV     [OldInt1C],BX           ; Int 1C is BIOS clock tick
        MOV     [OldInt1C+2],ES         ; Returns in ES:BX

;       Store new int 1C
        MOV     DX,OFFSET NewInt1C      ; Set clock tick
        PUSH    DS                      ; DS:DX points to new handler
        PUSH    CS
        POP     DS
        MOV     AL,1CH                  ; to point to NewInt1c
        SCALL   SIVEC                   ; Set Interrupt VECtor
        POP     DS

;       Get old int 09
        MOV     AL,09                   ; interrupt 09
        SCALL   GIVEC                   ; Get Interrupt VECtor
        MOV     [OldInt09],BX           ; Int 9 is keyboard
        MOV     [OldInt09+2],ES         ; Returns in ES:BX

;       Store new int 09
        MOV     DX,OFFSET NewInt09      ; point DS:DX to NewInt09
        PUSH    DS
        PUSH    CS
        POP     DS
        MOV     AL,9                    ; Set keyboard handler
        SCALL   SIVEC                   ; Set Interrupt VECtor
        POP     DS

        RET

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;       Close down
;       **********
;( Most code left out to simplify. )

Quit:
;       Get current int 1CH
        MOV     AL,1CH                  ; Function 35 interrupt 1C
        SCALL   GIVEC                   ; Get Interrupt VECtor
        MOV     AX,OFFSET NewInt1C
        CMP     AX,BX
        JZ      Q1
        MOV     DX,OFFSET EekMsg1
        SCALL   OUTSTR
        JMP     Q2
Q1:
        MOV     AX,ES
        MOV     BX,CS
        CMP     BX,AX
        JZ      Q2
        MOV     DX,OFFSET EekMsg1
        SCALL   OUTSTR
Q2:

        MOV     DX,[OldInt1C]           ; restore old
        MOV     BX,[OldInt1C+2]         ; interrupt 1C vector
        PUSH    DS
        MOV     DS,BX
        MOV     AL,1CH
        SCALL   SIVEC
        POP     DS

;       Get current int 09
        MOV     AL,09                   ; Function 35 interrupt 09
        SCALL   GIVEC                   ; Get Interrupt VECtor
        MOV     AX,OFFSET NewInt09
        CMP     AX,BX
        JZ      Q3
        MOV     DX,OFFSET EekMsg2
        SCALL   OUTSTR
        JMP     Q4
Q3:
        MOV     AX,ES
        MOV     BX,CS
        CMP     BX,AX
        JZ      Q4
        MOV     DX,OFFSET EekMsg2
        SCALL   OUTSTR
Q4:

        MOV     DX,[OldInt09]           ; restore old
        MOV     BX,[OldInt09+2]         ; interrupt 09 vector
        PUSH    DS
        MOV     DS,BX
        MOV     AL,9
        SCALL   SIVEC
        POP     DS

;       Enter dos
        SCALL   EXIT

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Handle Int 1CH, BIOS timer, 11/12 February 2001, SRN

NewInt1C        PROC    FAR
        PUSH    AX
        PUSH    DX
        PUSH    DS

        MOV     AX,SEG DATASG           ; for real, _before_ you try to
        MOV     DS,AX                   ; call OldInt1C

        PUSHF
        CALL    DWORD PTR OldInt1C

        CMP     [Pause],0
        JNE     NI1C_2

        CMP     [TimerFlag],0           ; Flag already set?
        JNE     NI1C_2
        MOV     [TimerFlag],1

        DEC     [PieceTime]
        JNS     NI1C_1                  ; Positive or zero?
        MOV     [PieceTime],0           ; Ensure not negative
NI1C_1:
        DEC     [LevelTime]
        JNS     NI1C_2                  ; Positive or zero?
        MOV     [LevelTime],0
NI1C_2:
        POP     DS
        POP     DX
        POP     AX

        IRET
NewInt1C        ENDP

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Interrupt handler for Int 9, BIOS Keyboard, 11 February 2001

NewInt09 PROC   FAR

        PUSH    DS
        PUSH    AX
        MOV     AX,SEG DATASG
        MOV     DS,AX
        PUSHF
        CALL    DWORD PTR OldInt09
        MOV     [KeyFlag],1     ; Indicate that a key is available.
        POP     AX
        POP     DS

        IRET

NewInt09 ENDP

CODE    ENDS
        END     START


HTH,

Steve N.

FORTRANS

Hi,

   Did this code prove to be useful?

Steve

Shintaro

Thanks Steve for that.

My sincere apologies for not responding. This last year has been one huge cluster F..... for me.
When I was looking at your code I realised that I did not understand some fundamental information on the Flat memory model.

I'll probably need more help, as I thought I knew more about assembly. but it appears not.

"Wyrd bið ful āræd. Fate is inexorable."

Gunther

Shintaro,

Quote from: Shintaro on January 02, 2022, 07:45:13 PM
When I was looking at your code I realised that I did not understand some fundamental information on the Flat memory model.

But Steve's code is for Real Mode DOS and has nothing to do with the Flat Memory Model. What's really going on?

You have to know the facts before you can distort them.

Shintaro

Simple mate.
I overestimated what I thought I knew. So it is easier to go back to the start, and RTFM, before wasting anyone's time any further.
For example, I thought I had a basic idea of programming asm in DOS, but starting to read Andrew Schulmans book (Undocumented DOS), it's clear I have no idea.But I do appreciate your replies.
"Wyrd bið ful āræd. Fate is inexorable."

FORTRANS

Hi,

   While I consider "Undocumented DOS" an excellent
book, it's not what I would recommend for a beginner
trying to learn assembly.  "The Art of 64-Bit Assembly"
by Randall Hyde" is mentioned in The Colosseum sub-
forum might be better.  His earlier books were good.

   All of my collection of books on assembly language
are old and most address 16-bit real mode programs.
If you are still interested, I can list some of them here.

Regards,

Steve N.

avcaballero

If I don't remember wrong, for hooking a dos-interruption, you must use int 21h with AH = 25h and AL = number of interruption to be hooked, DX = offset of the new ISR. An easy example is that for a watch tsr (in the upper right corner, in this case). In this case, the subroutine "Reloj" write the watch (our job) and at its end call the old Int 8h, so this keeps doing its job.

http://abreojosensamblador.epizy.com/Productos/AOE/Codigos/Cap12/RelojCM1.asm

Shintaro

Quote from: FORTRANS on January 03, 2022, 01:37:01 AM
   All of my collection of books on assembly language
are old and most address 16-bit real mode programs.
If you are still interested, I can list some of them here.
Steve,
I have lots of PDF's and a few old books on Assembly Language. But I would be interested what you think is the best.
I understand the instructions, but I think I need to workout how to break the programming problem down to smaller tasks.It seemed a lot easier in C/C++ at Uni, back in the late 90's.
Ok, bit of history, this all started, when I was looking at Novell abends and then Windows BSOD. I could make educated guesses about what caused it, but not why, which would annoy me to no end. You know, follow a process, run this command, look here, etc

One of my personal flaws is, I need to know why and how. I have always had trouble with books making statements but no reference to what they are talking about (maybe because it is assumed knowledge), for example, using EMS and XMS.  I did not know that EMM386 uses memory above the HMA for EMS 64Kb frames. That little bit of information would have useful in the early 90's rather than blindly running commands.
Now that I think about it, I wonder about the usefulness of a Bachelor degree in computing.

I mentioned Andrew's book because the internal structure of DOS was a surprise.
Like the PSP, I didn't know it existed, and that the PSP became the Win 3.x PDB (Process Database, source: windows internals Pietrek P.102) So now that makes sense.Additionally a side note, the AARD code is very interesting.

What might be useful is how you guys see a solution to a programming problem, how you break it down, what not to do because it breaks something else. So set xyz up like this...
For example, when I was upgrading Alcatel telephone systems, I would go through a procedure (beyond a simple backup), which took me maybe 3 mins, that if something went wrong it would save me literally hours of work. It saved me more times than I can count.

Anyway sorry for the long explanation.
I really do appreciate you guys willingness to help.
Cheers.

"Wyrd bið ful āræd. Fate is inexorable."

jj2007

The best way to learn Masm is perhaps to study the folder \Masm32\examples - plenty of good stuff, with lots of comments and explanations.

FORTRANS

Quote from: Shintaro on January 03, 2022, 12:26:16 PM
Steve,
I have lots of PDF's and a few old books on Assembly Language. But I would be interested what you think is the best.
I understand the instructions, but I think I need to workout how to break the programming problem down to smaller tasks.

Hi,

   This is a textbook, and straight forwardly written.  My main use
of it has been as a MASM reference.  Bought remaindered, and probably
is the last assembly book I purchased.

"Structured Assembler Language for Microcomputers", A. R. Kindred, 1991

   For learning to program for MS-DOS, these were among the most useful.

"MS-DOS Developer's Guide", Second Edition, The Waite Group, Second
Edition, 1989.
"Programmer's Guide to PC Video Systems", 2nd Ed, Richard Wilton,
Microsoft Press, 1994.
"Assembly Language Programming for the IBM-PC", David J. Bradly,
Prentice-Hall Inc., 1984.

   All three break up their programs into a main program that calls a
subroutine to accomplish a task.

   This I got late in the game, so I can't comment on how good it is
for learning, but has a lot of information.

"Advanced MS-DOS", Ray Duncan, Microsoft Press, 1986.

   The following are used as references mostly.

"MS-DOS Programmer's Reference", Version 5, Microsoft Press, 1991.
"The Undocumented PC", Second Edition, Frank van Gilluwe, Addison
Wesley, 1994.
"Undocumented DOS", Second Edition, Andrew Schulman et. al., Addison
Wesley, 1994.
   "Programmer's Guide to the EGA, VGA, and Super VGA Cards", Third
Edition, Richard F. Ferraro, Addison Wesley, 1994.

   I never used the following, but it has been used by many
and has a sub-forum here.

"Assembly Language for the IBM-PC", Kip Irvine, MacMillan.

Regards,

Steve

Edit:   Perhaps I should have said that they write a routine to
accomplish a task in the form of a subroutine that you can use
in your programs.
SRN

HSE

Also very complete:

Abel, Peter. IBM-PC Assembly Language and Programming. Prentice-Hall

I have third edition from 1996 (translated) but there is a fourth edition in 1998.
Equations in Assembly: SmplMath