|
|
Interrupt Handlers
The 80x86 processor has somthing called interrupts, whitch is just what it says-
an interruption of the work beeing done by the CPU. It means that the CPU is diverted
for a moment to do some other work. When the CPU is running a program code and an interrupt
occurs the CPU will jump to this other code section in the memory and run that code, and when
it is done, it will return to the exact same point where it was from the begining. The programming
code executed during the interrupt is called an interrupt handler or an interrupt service routine (ISR).
The interrupts can be detected in several ways. First there is the non-maskable interrupt(NMI) pin, where a
device can send an electrical signal. An other way is to send an electric signal to the CPUs INTR. This is
done by a programmable interrupt controller(PIC), a chip that handles non-catostrophic interrupts. The Intel
8259A is such a chip. The third way is to pull a software or internal interrupt. This is the type that occurs
when the CPU executs a INT instrucion, but it can also occur when and INTO,DIV or IDIV instruction is executed.
An example of an software interrupt instuction:
mov ax,04c00h
int 21h
This two instructions can be found in most executive dos-programs, because it tells dos that the programm that
you wrote is done, and dos can go back from running your program.
The interrupts that are signaled through the NMI or INTR pins are hareware, or external interrupts.
Every time you press a key on your keyboard and interupt is generated by the hardware of the keyboard.
If you use the DIV or IDIV instructions and the divisor is a zero then an software interrupt will occur.
Or if and operation results the overflow flag(OF) to be set it will result in running a software interrupt.
These two software interrupts are the same as using the INT 0 (DIVIDE BY ZERO) and INT 4 (OVERFLOW).
When an interrupt occurs the CPU will push the flags to the stack, it will clear the interrupt and trap flags and
push the adress from where the interrupt was called. The next step for the CPU is to locate the address for the
specific interrupt that has occurd. This is done by looking in the table at address 0000:0000 in the memory
(more about this later). Then the CPU executes the interrupt code, it will pop the address back and it will pop
the flags back.
Stepps during an interrupt
1. Push flags
2. Clear the Trap Flag and the Interrupt Flag
3. Push the current address (from CS:IP)
4. Get the address to the Interrupt Handler from the vector table at 0000:0000
(into CS:IP)
5. Execute the interrupt handler
6. Pop the address back (into CS:IP)
7. Pop the flags
The PC can have 256 diffrent interrupt vectors and these are located at the 1Kb of memory(starts at address 0000:0000).
For instance, lets say that the dos interrupt 21h was executed, the CPU will calculate the memory address to the Interupt Handler.
This is done by taking the interrupt number 21h and multiply it by 4, since each entry in the Vector Table is two words (2*16 bits)
and concists in an Adress and an Segment. If we use the dos interrupt 21h, we will find the address at 0000:21h*4 whitch is 0000h:0084h.
We can change these addresses in memory and in such way have our own code that can be used by other programs in DOS environment.
To change these vectors we can use DOS functions or we can change the vectors directly.
Changing the Vectors directly
push es ds ;Save es and ds in the stack
mov ax,cs
mov ds,ax
mov ax,0
mov es,ax ;Segment 0
mov bx,09h*4 ;Keyboard interrupt*4 to locate the segment and address
mov ax,offset newint ;Get new offset
cli ;Disable the interrupt
mov es:[bx],ax
mov ax,seg newint ;Get new segment
inc bx
inc bx
mov es:[bx],ax ;Setting offset:segment to vector table
sti ;Enable interrupt
pop ds es
Changing the Vectors by using DOS
push ds ;Save ds in stack
mov ax,cs
mov ds,ax ;Setting ds to code segment
mov dx,offset newint ;Offset to new interrupt
mov al,09h ;Keyboard interrupt
mov ah,25h ;dos function
int 21h ;dos interrupt
pop ds ;getting ds back
|
|
|