This blog post mainly introduces the keyboard Interruption Process on x86 and how the application uses the interrupt to read the content of the keyboard buffer.
I. background of this blog
When we watch video in full screen mode, Press ESC to restore or minimize the player. When other software is used, we will
You can set a shortcut to open or close the program ......
Why can an application work according to keyboard instructions? How does it work?
II. Introduction to keyboard interruptions
When we press the key, the keyboard will send the scan code of the Key to the interface circuit of the motherboard, specifically port 60 h.
When a scan code is received for 60 h, the interface sends an interrupt request to the CPU. This is a blocked interrupt. Therefore, when if = 1,
After the CPU executes the current command, it will respond to the interruption. That is, enter int 9 to interrupt the service program. At this time, the program will read
Scan code in 60 h and determine the Input key.
3. The application reads the Keyboard Buffer
You can use the keyboard to control applications in two ways:
1. Read the Keyboard Scan Code directly from Port 60 h, and analyze whether the keyboard is entered as a character, a control character, or a switch character in the application.
2. Use the interrupt int 16 h provided by BIOS.
4. Introduce the program
1. function: When press a, the screen is written in red; then press SHIFT + A and the screen is restored to black. When the program is running, do not press other keys
There is a response.
2. This program uses the BIOS function to call int 16 h to read the keyboard buffer.
3. The screen color is changed by writing data directly to the display.
4. Code:
Int9 equ 09 hdata segmentaddr DW ?,?; The address of the storage system int 9 flag db 0; the program end sign, 1 indicates that the SHIFT + A is pressed, and the program data endscode segmentassume Cs: code, DS: datastart: moV ax, datamov ds, axmov ax, 0mov es, ax; Save the original interrupt address mov ax, ES: [int9 * 4] mov ADDR, axmov ax, ES: [int9*4 + 2] mov ADDR [2], ax; set a new disconnection address; CLI; MoV word ptr es: [int9 * 4], offset ISR; moV word ptr es: [int9*4 + 2], seg isr; STI; add clilea ax, isrmov ES: [int9 * 4], axmov ax, SEG isrmov ES: [int9*4 + 2], axsti; wait for Shift + A, end program back: CMP flag, 1je leavejmp back; recover original interrupt address leave: climov ax, addrmov ES: [int9 * 4], axmov ax, ADDR [2] mov ES: [int9*4 + 2], axstimov ah, 4 Chint 21 h; new interrupt service program ISR proc farpush axpush bxmov ax, datamov ds, ax; call the original int 9 to complete necessary work pushfcall dword ptr addr; call the BIOS int 16 h to read a byte mov Ah in the Keyboard Buffer, 0int 16 h; press acmp Al, 61hje go1; press SHIFT + acmp Al, 41hje go2back1: Pop bxpop axiret; press a to pass the characters in the video memory through BL. The attribute is high-bright red go1: mov BL, 48 hcall far PTR displayjmp back1; press SHIFT + A to restore go2: mov flag, 1mov BL, 0 call far PTR displayjmp back1isr endpdisplay proc farpush cxpush Si; write mov CX, 0b800hmov es, cxmov CX, 2000mov Si, 1 again: mov ES: [Si] to the video memory. bladd Si, 2 loop againpop sipop cxretcode endsend start
5. Experiment results
① Press
② Press SHIFT +
5. the blogger uploaded Wang Shuang's assembly language. Welcome to study and exchange.