Assembly language - within interrupt

Interruption of the means that the CPU is not followed (instruction just executed) to perform down, but turn to deal with this particular information, but this particular information we can call interrupt information. Interrupt information is to require some sort of CPU processing immediately, the treatments to be carried out to provide notification information necessary parameters.

Within the interrupt generation

For 8086CPU, when the internal CPU has the following happens, generated in response to the interrupt message .

  • Divide error, for example, divide execution command generating div overflow;
  • Single-step execution;
  • Instructions into execution;
  • Int instruction execution.

And interrupt information must contain the interrupt type code data to the terminal identification information sources. Interrupt type code is a byte of data, can represent 256 variety of sources interrupt information.
8086CPU terminal type code as follows.

  • Divide error: 0
  • Single-step: 1
  • Instruction execution into: 4
  • Instruction execution int, int format of the instruction is n, the number n of the instruction immediately byte is supplied to the CPU interrupt type code.

Interrupt handler

We write a program to handle the interrupt information is called an interrupt handler .
To execute the interrupt handler how to get handlers from eight terminal type code segment address and offset address?

Interrupt vector table

  • Interrupt vector is the interrupt handler entry address.
  • Interrupt vector table is a list of interrupt handler entry address.

Interrupt vector table in memory 0000: 03FF storage unit 1024, where the stored interruption information source 256 corresponding to the interrupt handler entry, the entry address includes a segment address and offset address, a: 0000-0000 entry occupies two bytes, the high address word segment stored address, low address word stored offset address. Just know CPU interrupt type code, it can be used as an interrupt to the interrupt type code entry number scale, to locate the corresponding table entry, to obtain entry address of the interrupt handler.

Interrupting the process

The process is interrupted by an interrupt process interrupt vector to find the type code, and use it to set the CS and IP.
Here is 8086CPU after receiving interruption information, caused by interrupting the process.

  • Get interrupt type code (from interrupt information);
  • Push the value of the flag register (as in the process to change the value of the interrupt flag register, the first saved in the stack);
  • Setting a first flag register 8 9 IF and TF is 0;
  • CS contents of the stack;
  • Content IP stack;
  • Interrupt type code from memory addresses 4 and interrupt type code entry address two sub-units 4 + 2 reads the interrupt handler provided IP and CS.

More concise expression interrupt process, as follows:

取得中断类型码 N;
pushf
TF=0,IF=0
push CS
push IP
(ip)=(N*4),(CS)=(N*4+2)

Interrupt handler and instructions iret

Interrupt handler is quite similar to the preparation methods and routines, the following conventional steps:

  • Save registers used;
  • Interrupt;
  • Restore used registers;
  • Returns with iret instruction.

iret function instruction assembler syntax description is:

pop ip
pop cs
popf

iret usually done automatically and hardware interrupt process used in conjunction. During the interruption, and the order of the register stack iret correspond exactly.

Write interrupt processing 0

;因为除法溢出随时可能发生,CPU随时都可能将CS:IP只想程序的入口,执行程序,所以我们将程序放入内存中。
;因为系统要处理的中断事件远没有达到256个,所以在中断向量表中,有许多单元是空的。

assume cs:code

code segment
start:

;安装:将中断处理程序代码送入中断向量表中。
    mov ax,cs
    mov ds,ax
    mov si,offset do0   ;ds:si指向源地址

    mov ax,0
    mov es,ax
    mov di,200h         ;es:di指向目的地址
    mov cx,offset do0end-offset do0 ;设置cx为传输长度 ;-是编译器识别的运算符号,编译器可以用它来进行两个常数的减法。
    cld     ;设置传输方向为正
    rep movsb
    
;设置中断向量:将do0的入口地址,写入中断向量表的0号表项中,使do0为0号中断的中断处理程序
    mov ax,0
    mov es,ax
    mov word ptr es:[0*4],200h
    mov word ptr es:[0*4+2],0

    mov ax,4c00h
    int 21h
    
;中断处理程序代码
    do0: jmp short do0start 
        db "overflow!"  ;放在data段执行完后内存被系统释放数据可能被覆盖

    do0start:
        mov ax,cs
        mov ds,ax
        mov si,202h     ;ds:si指向字符串,复制到0:200处
        
        mov ax,0b800h
        mov es,ax
        mov di,12*160+36*2  ;显示位置

        mov cx,9
        s:
            mov al,[si]
            mov es:[di],al
            inc si
            add di,2
            loop s
        mov ax,4c00h
        int 21h
    do0end:nop
code ends
end start

Single-step interrupt

Basically, the CPU after completion of execution of an instruction, the flag register if the monitored TF bit is 1, the single-step interrupt is generated, the interrupt process is initiated. Single-step interrupt is an interrupt type code 1, it raises the interrupt process is as follows.

  1. Trap flag TF (trap flag): single step operation for debugging;
  2. TF = 1, a trap after each instruction executed, controls the computer system;
  3. TF = 0 when the CPU is working properly.
  4. IF interrupt flag (interrupt flag);
  5. IF = 1 allows the CPU maskable interrupt request;
  6. IF = 0 Close interrupted.
  • Get interrupt type code 1;
  • Flag register stack, TF, IF is set to 0;
  • CS, IP stack;
  • (IP)=(14),(CS)=(14+2)。

As described above, if TF = 1, then execute a good instructions, CPU 1 will turn to execute the interrupt handler.

Guess you like

Origin www.cnblogs.com/chengmf/p/12499339.html