Compilation of interrupted service programs

Source: Internet
Author: User

Compilation of interrupted service programs

Chen Qingyang  

 

Summary: This article describes how to write, install, and use interrupt service programs in C language. As hardware port read/write operations are involved in writing a hardware interrupt service program, you can directly deal with the hardware, and there are a lot of data (such as hardware port addresses) to be used in the program design process, this makes it much easier for programmers and computer hardware devices to directly program hardware in assembler languages. This article only introduces the preparation of Soft Interrupt programs.Keywords: Soft Interrupt, interrupt vector, interrupt vector table, TSR memory resident, DOS Re-entry, interrupt request, segment address, offset, register, bios, DOS, setvect (), getvect () for General C language enthusiasts, keep (), disable (), enable (), geninterrupt (), int86 (), and interrupt, we should be very familiar with how to use interrupt routines in C. For example, we can use the int86 () function to call the 13h interrupt function to directly operate the physical sector of the disk, you can also use the int86 () function to call the 33h interrupt function to display the mouse and cursor on the screen. In fact, 13h and 33h are just some functions, and the parameters of these functions are passed through the CPU register. The interrupt number is only indirectly directed to the starting memory unit of the function body. It is indirect, that is, the START segment address and offset of the function are calculated by the interrupt number (the following explains how to operate it ). As a result, programmers do not have to spend too much time writing hardware programs. They only need to set parameters in their own programs, you can call the interrupt service program provided by BIOS or DoS, which greatly reduces the difficulty of Program Development and shortens the program development cycle. Since the interrupt is a function, it can be called by the user and written by the user. The first 1024 bytes (offset: 256 h to 003ffh) in the computer memory store interrupt vectors, each of which occupies 4 bytes, the first two bytes store the entry address offset of the interrupt service program, and the last two bytes store the entry segment address of the interrupt program. during use, you only need to transfer them to the Register IP address and CS respectively, the service interruption program can be transferred to implement the call interruption. When an interrupt occurs, the CPU times the interrupt number by 4 and obtains the interrupt vector address in the interrupt vector table. In this way, the IP address and Cs value are obtained and then transferred to the endpoint address of the interrupt service program, call interrupted. This is the basic process of calling the interrupt service program through the interrupt number. When the computer is started, the BIOS fills in the interrupt vector table with the basic interrupt. When dos gets control of the system, it also needs to fill in some interrupt vectors in the table and modify some BIOS interrupt vectors. Some interrupt vectors are reserved by the system for users, such as 60 h to 67h interruptions. Users can write their interrupt service programs into these interrupt vectors. In addition, you can modify and improve the existing interrupt vectors of the system. In the C language, a new function type interrupt is provided to define the interrupt service program. For example, we can write the following interrupt service program:/* Example 1: interrupt Service Program */void interrupt int60 () {puts ("this is an example");} the interrupted function is to display a string. Why not use the printf () function? This involves DOS Re-entry, which will be introduced later. After a simple interrupt service program is written, how can I enter its function entry address in the interrupt vector table so that it can be transferred to the interrupt service program for execution when an interruption occurs? The setvect () and getvect () functions are used here. Setvect () has two parameters: the interrupt number and the function entry address. The function is to install the specified function to the specified interrupt vector. The getvect () function has a parameter: the interrupt number. The return value is the interrupt entry address. Before the installation is interrupted, it is best to use the disable () function to disable the interruption, so as to prevent program running disorder caused by new interruptions during the installation process. After the installation is complete, use Enable () function opening is interrupted to make the program run normally. Now we can enrich the above example:/* Example 2: writing, installing, and using the interrupted service program */# include # Include # Ifdef _ cplusplus # DEFINE _ argu... # else # DEFINE _ argu # endifvoid interrupt int60 (_ argu)/* interrupt service function */{puts ("this is an example ");} void install (void interrupt (* FADD) (_ argu), int num)/* installation interruption */{disable ();/* disable interruption */setvect (Num, FADD);/* Set interrupt */enable ();/* Open interrupt */} void main () {install (int60, 0x60 ); /* install the int60 function to 0x60 interrupt */geninterrupt (0x60 ); /* manual 0x60 interruption */} experienced readers can easily get the execution result of the program: the screen displays "This Is An examp Le !". This section describes how to compile and install an interrupted service program. Next, let's talk about writing and using a memory resident program (TSR. In C, you can use the keep () function to resident the program in the memory. This function has two parameters: Status and size. Size is the length of the resident memory, which can be obtained using size = _ SS + _ sp/16-_ PSP. Of course, this is also an estimation method, not an exact value. After the function is executed, the exit status information is saved in status. For example, for the above example, rewrite "geninterrupt (0x60);" to "keep (0, _ SS + _ sp/16-_ PSP ); "and then execute the program, this program will be resident, and then in any other software or program design, as long as the 60 h interrupt is used, "This is an example!" is displayed on the screen!". To restore the definition of 60 H interruption, you can only restart the computer. As in the above example, it is still not perfect. It does not consider the status of the DOS system environment, whether the program has resident memory, or whether to exit the memory resident. The second problem is easily solved: The execution program reads a function interrupt entry address (for example, the 63h interrupt) from the beginning to determine whether it is null ), if it is null, the address is set to non-empty and then resident memory. If it is not empty, it indicates that the address has resident and exited the program. This step is very important; otherwise, the system will crash due to the excessive memory space occupied by the repeated resident. For the other two questions, I will not explain them here. Interested readers can refer to some related books. In addition, we can also use the hotkey in DOS to call the memory resident program. For example, you can activate the program by pressing CTRL + F11 at any time after the memory of the expected Chinese character system is resident. The dictionary interface appears. The Microcomputer keyboard has a microprocessor chip used to scan and detect the press and release status of each key. Most keys have a scan code to tell the current CPU status. However, some special keys such as printscreen and CTRL + break do not generate scan codes, but directly interrupt them. Because of this, we can point the interrupt number generated by Ctrl + break to the entry address of our own program. After pressing CTRL + break, the system will call our own program for execution, which is actually modifying the interrupt vector of Ctrl + break. For other key activation programs, you can use the scanning code captured by the 9h keyboard interrupt. For example, after executing the following program, return to the DOS system and press Ctrl + break at any time, the background color of the screen turns red. /* Example 3: interrupt service program writing, installation, and usage, memory resident */# include # Include # Ifdef _ cplusplus # DEFINE _ argu... # else # DEFINE _ argu # endifvoid interrupt newint (_ argu);/* function declaration */void install (void interrupt (* FADD) (_ argu ), int num); int main () {install (newint, 0x1b);/* Ctrl + break interrupt number: 1bh */keep (0, _ SS + (_ sp/16) -_ PSP);/* resident program */return 0;} void interrupt newint (_ argu) {textbackground (4 ); /* set the screen background color to red */clrscr ();/* clear the screen */} void install (void interrupt (* FADD) (_ argu), int num) {disable (); Setvect (Num, FADD);/* Set interrupt */enable ();} because the 13h interrupt is a disk interrupt service program provided by the bios, for DOS applications, their disk storage and reading functions are all achieved by calling this medium break. Many dos viruses like to modify the 13h interrupt to destroy the system. For example, modify the 13h interrupt service program to:/* Example 4: virus program pseudocode */void interrupt new13 (_ argu) {If (virus attack condition maturity) {modify the entry parameter to the entry address of the virus program; execute the virus code ;} call the original 13 H interrupt;} as long as any software (such as edit. com. Of course, this will reduce the available memory space and be easily discovered by users. Some "smart" viruses will modify other interrupt vectors so that the memory size reported by the system is in line with the actual situation. Another virus, when it is found that the user traces it through some programs (such as debug. com), it will quietly slide, and its basic principle is still related to the change interruption. The 0-side, 0-column, and 1-sector (side 0 cylinder 0 sector 1) of the hard disk stores important boot information. Once damaged, the computer cannot identify the hard disk. We can write a program to prevent any software (including viruses) from performing "write" operations on this sector. To some extent, we have implemented the "Write protection" function, its basic principle is to modify the interrupt vector of the 13h number and stay in the memory, monitoring every detail of the disk operations of the software (including viruses. Reader's note: this program does not consider the exit of memory resident. If you want to resume the interruption of the 13h number, restart the computer. /* Example 5: Primary boot sector protection. Use Turbo C 2.0 for compilation. mbsp. C */# include # Include # Include # Define stsize 8192 # define psp_env_psp 0x2c # define para (x) (fp_off (x) + 15)> 4) typedef struct {unsigned bp, Di, Si, DS, es, dx, CX, BX, ax, IP, Cs, flags;} interrupt_parameter; void install (void interrupt (* faddress) (), int num ); void interrupt new13 (interrupt_parameter P); int main () {Union regs; struct sregs; unsigned MEM; unsigned far * pointer; char far * stack; printf ("/n < > Version 1.0/n "); If (stack = malloc (stsize) = NULL) {printf (" not enough memory! /N "); exit (1);} If (getvect (0x62 )! = NULL) {printf ("already installed! /N "); exit (1);} install (getvect (0x13), 0x62); install (new13, 0x13 ); pointer = mk_fp (_ PSP, psp_env_psp); freemem (* pointer); segread (& sregs); mem = sregs. DS + para (stack)-_ PSP; setblock (_ PSP, Mem); Keep (0, Mem); Return 0;} void install (void interrupt (* faddress) (), int num) {disable (); setvect (Num, faddress); Enable ();} void interrupt new13 (interrupt_parameter p) {P. ax = _ ax; p. cx = _ CX; p. DX = _ DX; if (_ Ah = 0x03 & _ CH = 0 & _ Cl = 0x01 & _ DH = 0 & _ DL = 0 x 80) return; Enable (); geninterrupt (0x62); Disable (); _ AX = P. ax; _ Cx = P. CX; _ dx = P. DX; return;} Note: before using this program, please:① Use anti-virus software to perform a comprehensive scan of computer boot sectors, memory, and all files to ensure that there is no virus in the computer;② Readers with computer assembly language basics can write a new boot program by themselves, first resident the program in the memory, and then call the original boot program, this allows you to enable the protection function before the virus has control of the system. Finally, we will briefly describe the DOS system re-entry problem. DOS is a single-user, single-task operating system. If the program is interrupted during execution, it may be caused by the failure of the original running environment, which is catastrophic. When an interrupt occurs, the CPU immediately terminates the current program to execute the interrupt service program. If a DOS interrupt call (such as a DOS 21h interrupt) occurs in the interrupt service program, in this way, the global variables of the environment will be overwritten (for example, the PSP segment prefix will be changed to the PSP of the interrupted program being executed), so that the original environment will be damaged and the original program will not be correctly executed. When the call is interrupted and the response is returned, the user gets unexpected results. Therefore, you should avoid calling the DOS system functions when writing interrupt service programs. malloc (), printf (), sprintf () and other functions should not appear in the interrupt service program in C language.

References: Edited by Wang Shiyuan, Tsinghua University Press, 1996.3, China

 

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.