Assembly language Writing DOS memory-resident program (3)

Source: Internet
Author: User
Three interrupt vectors
3.1 IBM PC-supplied interrupts
There are two basic forms of disruption to the IBM pc. If the interrupts caused by peripherals are called hardware interrupts (hardware interrupt), peripherals such as keyboards, disk drives, and clocks can generate hardware interrupts. The interrupt signal generated by the peripherals is connected to the interrupt controller, The interrupt controllers can prioritize them based on their importance, so that the CPU handles these hardware signals effectively. Another interruption is software outages (Software interrupt), software outages are also called traps (trap), It is generated by the software being executed. Although the package interrupts are handled in exactly the same way as the hardware interrupts, software interrupts are typically expected to perform the services provided by the operating system.
Table 3.1 is the interrupt provided by the IBM PC, which is arranged according to the interrupt number and interrupt vector (Interrupt vector).
The user of an IBM PC or program that writes an application is rarely directly exposed to a hardware outage, unless you are using some special hardware, or require more stringent requirements, the most frequently modified hardware interrupts are interrupts (9H), especially text-editing programs. Generally speaking, Only the hardware designer base is a system programmer who notices all the hardware interrupts, and the designer who writes the memory-resident program uses only part of the hardware interrupts, especially the keyboard interrupts and the timer interrupt.
Conversely, software outages are important to anyone who writes assembler, even to people who write high-level language programs. Software interrupts are the interface through which applications enter the IBM PC operating system, through which applications can perform the required system services.
One of the most important software interrupts, but also the most commonly used by assembly language programmers to use is a DOS INT 21H. This interrupt is a software outage that performs DOS system calls, allowing the application to perform any DOS operation.
The next most useful software outage is the interrupt provided by the Rom-bios (basic input-output system). These software interrupts are low-level services provided by IBM PCs, such as keyboard input, display output, and input and output from the disk machine.
3.2 Method of keyboard input
The following is an example of an IBM PC reading characters from the keyboard to illustrate how interrupts work. When the IBM PC reads characters from the keyboard, it uses two different forms of interrupts, that is, hardware interrupts and software outages. When the user knocks a key from the keyboard, a signal is sent from the keyboard line. This signal will cause a hardware outage, This triggers low-level keyboard interrupt handlers to begin execution. The interrupt handler immediately reads the user-typed character from the keyboard's hardware and then puts it in a queue, and if the queue fills up, the keyboard interrupt handler makes the IBM PC beep. Keyboard interrupt Handler After these things are done, It gives control back to the previously interrupted procedure. If a program wants to read a character from the keyboard, it emits the appropriate software interrupt signal, and then the corresponding interrupt handler checks the keyboard queue and returns the first character in the queue.
The keyboard inputs described above are commonly used in interrupt-driven systems. This and practice separates the applications that actually need to be entered into the processing part that actually performs the input. This approach can also be used in other different forms of input and output peripherals.
3.3 Change the input vector
The interrupt vector is stored in the 400H byte at the front of the IBM PC. The length of each vector is four bytes, and the four bytes hold the address value that the interrupt handler executes. The first two bytes contain the offset portion of the address value, and the two bytes that follow contain the segment ( Segment) section.
There are two ways to modify the interrupt vector. You can set the address value of the interrupt vector directly or use the system call provided by DOS to set the address value of the interrupt vector.
3.3.1 Set interrupt vector directly
Because the interrupt vector is the location where the address value is stored, we can directly store the address in the storage location. Here is a small example:
MOV ax,0
MOV Es,ax
mov Word ptr es:24,offset keyboard
mov Word ptr es:26,seg keyboard
In many cases, the above programs can be executed correctly. But if the above program is executing with a sudden knock on a key, it may be problematic, and the worst is happening: The third MOV has been executed, and the fourth MOV has not yet been executed. If you knock down any key at this point, The keyboard interrupt vectors have no meaning and cause the entire system to panic. So we can set the interrupt vector to invalidate the interrupt, for example:
MOV ax,0
MOV Es,ax
Cli
mov Word ptr es:24,offset keyboard
mov Word ptr es:26,seg keyboard
The above practice can be performed correctly in most cases. However, the CLI directive cannot stop NMI interrupts (unshielded interrupts), so there is no way to do this if a nmi interrupt occurs. The following approach is more complex, but it works for all interrupts, including the NMI interrupt:
mov Word ptr kbd-ptr[0],offset keyboard
mov Word ptr kbd-ptr[2],seg keyboard
MOV di,0; Use Di to Set ES to zero
MOV es,di; Set ES to destination segment
MOV di,24; Set DI to destination offset
mov si,offset kbdptr; set Si to source offset
MOV cx,2; Set Word count to 2
CLD; Set direction to forward
CLI;D isable interrupts
Rep MOVSW; Copy the new vector
STI; Enable interrupts
KBDPTR DD?
In the above program, KBDPTR is a two-byte (WORD) pointer (pointer) that contains the starting values of the keyboard interrupt handler. Rep This instruction repeats the execution of the MOVSW according to the number of times the register CX is set, and the entire instruction is like a single instruction. NMI interrupts cannot occur in a complete instruction. Because the address value move operation can be included in a single instruction, you can exempt from any interruption of interference.
3.3.2 Use DOS to set interrupt vectors
Because it takes some skill to safely set the interrupt vector, DOS provides a special service to help program personnel safely set the interrupt vector, and if you use only the service provided by DOS to set the interrupt vector, then there is no need to worry about the errors described earlier. DOS also provides a service that reads interrupt vectors. Because the contents of the read interrupt vector do not modify the state of the system, it is also safe to write directly to the reader. But if you want to read the interrupt vector directly, you must compute the location of the interrupt vector. DOS has already provided this service.
Using the system call provided by DOS to read the contents of the interrupt vector, you must take advantage of the function 35H (read interrupt vector) in int 21H to compute the address of the interrupt vector, and then return the contents. Here is an example:
Old_keyboard_io DD?
MOV al,16h
MOV ah,35h
int 21h
mov word ptr old_keyboard_io,bx; Offset of Interrupt Handler
mov word ptr old_keyboard_io,es; Segment of Interrupt Handler
Use DOS to set the interrupt vector example:
New_keyboard_io DD?
mov word ptr new_keyboard_io,bx; Offset of Interrupt Handler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.