Lecture 13
Lecture 13
Interrupts
• Interrupts are events that occurred outside the processor and the
processor must be informed about them.
• If you look at IVT in AFD you can see that some ISR
are in ROM BIOS some are in DOS Kernel
• The actual addresses in this table vary from one machine to another
INT Instruction
• Interrupt are invoked using INT instruction
• The INT (call to interrupt procedure) instruction calls a ISR
• The syntax is
• INT <number>
• where number is an integer in the range 0 to FF hexadecimal
• Parameters are passed to int in register, not on stack. Output is also returned in registers
• For example
• Mov ax, 0x4C00
• Int 21h
• Hardware interrupts are invoked implicitly, some software interrupts are invoked implicitly (eg int 0) and some are invoked
explicitly (e.g. int 21h). So you will never write int 0 in your code
IRET
• Like ret is paired with call to return from subroutine, IRET (interrupt
ret) is paired with INT to return from ISR
Interrupt Vectoring
• The CPU processes the INT instruction using the interrupt vector table
• Steps taken by the CPU when the INT n instruction is invoked by a program:
1. The operand of the INT instruction is multiplied by 4 to locate the matching interrupt vector table entry.
2. The CPU pushes the flags and a current CS and IP (return address) on the stack and disables hardware
interrupts (by clearing interrupt flag and trap flag )
3. The CPU Loads segment of interrupt n (n*4 )from IVT to CS and Offset to IP (n*4+2)
4. The interrupt handler at new segment and offset executes until it reaches an IRET (interrupt return) instruction
5. The IRET instruction pops the flags and the return address off the stack, causing the processor to resume
execution immediately following the INT n instruction in the calling program.
The operation of INT can be written as:
1. Pushf ; push flag
2. cli
3. clt
4. Push cs
5. Push ip
6. ip = [0:N*4]
7. cs = [0:N*4+2]
The operation of IRET can be written as:
1. pop ip
2. Pop cs
3. Popf ;pop flag
SOFTWARE INTERRUPTS
Software Interrupts
• A software interrupt is requested by the processor itself upon executing particular instructions or when certain
conditions are met. (they do not exactly fit the definition of interrupts given in slide 2 )
• Every software interrupt signal is associated with a particular interrupt handler (or ISR).
• As soon as the interrupt vector changes, that interrupt will be routed to the new handler
• For example
• If vector of int 0 is F000h:61AAh
• Then on int 0 CS will change to F000h and IP to 61AAh and program will start running from instruction at
F000h:61AAh after int 0
• If we change the vector of int 0 to 1957h:0110h then on int 0 program will run from 1957h:0110h after int 0
• Example 8.1 in BH shows how to hook int 0 such that every time this interrupt occurs “'You divided
something by zero.” message is shown on screen. Its show version is given on next slide
HOOKING AN INTERRUPT Example
Things to Note in Example
• Int 0 pushs flags, CS and IP on stack
• IP is of Div bx, not of next instruction
• Program will run infinitely as after iret we will return to DIV bx
• This is not the case in every interrupt
Template to hook Software interrupt n
newISR:
Store register
Body
Restore register
Iret
Start:
xor ax, ax
mov es, ax ; load zero in es
mov word [es:n*4], newISR; store offset at n*4
mov [es:n*4+2], cs ; store segment at n*4+2
BIOS AND DOS INTERRUPTS
• In IBM PC there are certain interrupts designated for user programs to
communicate with system software to access various standard
services like access to the floppy drive, hard drive, vga, clock etc
• This basic interface to the hardware is called BIOS (basic input output
services)
BIOS AND DOS DATA AREA
• The BIOS data area, partially shown
in Table, contains system data used
by the ROM BIOS service routines.
• Al is the write
mode in int 10h
service 13.
• 0th bitrepresent
if cursor should
be updated or
not
• 1st bit represents
if string contains
the settings or
not.
Example int 10h function 02h
mov ah,2
mov dh,10 ; row 10
mov dl,20 ; column 20
mov bh,0 ; video page 0
int 10h
Keyboard Input with INT 16h
• Int 16h is a software interrupt provided by bios to handle keyboard
• Mov ah, 0
• Int 16h;
Print string and keyboard wait using BIOS
services
• Example 8.3 in book.
• Also see code given in comments
Scan codes of keys
Hardware Interrupts
Hardware Interrupts
• A hardware interrupt request (IRQ) is an electronic signal issued by an external (to the
processor) hardware device, to communicate that it needs attention from the operating
system (OS) or, if there is no OS, from the "bare-metal" program running on the CPU.
• Such external devices may be part of the computer (e.g., disk controller) or they may be
external peripherals. For example, pressing a keyboard key or moving the mouse triggers
hardware interrupts that cause the processor to read the keystroke or mouse position
• There are many devices generating interrupts and there is only one
pin going inside the processor and one pin cannot be technically
derived by more than one source
• It assigns priorities to its eight input pins from 0 to 7 so that if more than one interrupt comes at the
same times
• 0 has the highest priority and 7 has the lowest
• A lower-level interrupt cannot interrupt a higher-level one still in progress.
• The highest priority one is forwarded and the rest are held till that is serviced.
• The rest are forwarded one by one according to priority after the highest priority one is completed
• The original IBM XT computer had one PIC so there were 8 possible interrupt sources. However IBM
AT and later computers have two PIC totaling 16 possible interrupt sources.
IRQ
• The eight input signals to the PIC are called Interrupt Requests (IRQ).
• The eight lines are called IRQ 0 to IRQ 7
• Each IRQ is mapped to a specific interrupt in the system, as shown in table
IRQ Interrupt Description
0 8h System timer (18.2 times/second)
1 9h Keyboard
2 0Ah Programmable Interrupt Controller
• The actual mechanism fetches one instruction from the PIC whenever the INT pin
is signaled instead of the memory.
• We can program the PIC to generate a different set of interrupts on the same
interrupt requests.
• Figure in slide 28 showed then when int 9h is invoked scan code and asci of pressed key are moved to buffer.
• For each key the scan code comes twice, once for the key press and once for the key release. Both are scan
codes and differ in one bit only.
• The lower seven bits contain the key number while the most significant bit is clear in the press code and set in the release
code.
• You can read the asci and scan code of pressed key using keyboard port
• in al, 0x60, this will read scan code in al
• In ax, 0x60; this will read asci in ah and scan code in al
How Keyboard Works
• If we press Shift-A resulting in a capital A on the screen, the controller has sent
the press code of Shift, the press code of A, the release code of A, the release
code of Shift and the interrupt handler has understood that this sequence
should result in the ASCII code of ‘A’.
• The ‘A’ key always produces the same scan code whether or not shift is pressed.
It is the interrupt handler’s job to remember that the press code of Shift has
come and release code has not yet come and therefore to change the meaning
of the following key presses. Even the caps lock key works the same way.
• You can hook int 9h and change what happens when a key is pressed
Hooking an interrupt to show scancode at every int9