Demo example of exception handling Vii.

Source: Internet
Author: User
Tags definition error code error handling exception handling

An example is given below for simulating exceptions and demonstrating exception handling. The logical function of the instance is to display a character on the screen that prompts the user to select the exception type by pressing the key, and then simulates the specified exception. This example demonstrates the following: Division fault handling, overflow trap processing, segment failure handling, stack segment error handling, and general protection fault handling; There is also a trap handler that appears as a stand-alone task.

1. Source Program organization and Inventory
To demonstrate a trap handler that appears as a stand-alone task, the instance contains two tasks: Demo tasks and read keyboard tasks. Examples are composed of the following parts:

(1) Global descriptor Tabulation GDT and interrupt descriptor IDT;
(2) Reading keyboard task local description chart, task status section, stack segment and code section, etc.
(3) The local description chart, task status section, stack segment, code segment and data segment of the demo task;
(4) as part of the demo task, the code snippet for trap processing and fault handling program;
(5) as part of the demo task to display the error code section of the process;
(6) Start and end program code snippets and data segments executed in real mode.
When you switch to protected mode, you enter the temporary code snippet. For simplicity, the demo task does not have a privilege level transformation. The demo steps are as follows:

(1) Transfer from the temporary code snippet to the demo code snippet.

(2) Prepare for presentation. Load the LDT selection of the demo task into the LDTR, and fill in TSS, load the task register TR, set up the demo task stack, and set up other data segment registers.

(3) Receive the exception class model to simulate. The Read keyboard task completes this step by invoking the soft interrupt instruction Int. The Read keyboard task ends only after the specified character has been received. The characters received are 0, 4, B, C, and D.

Simulates an exception by the character that is received. Executes the relevant program fragment, depending on the character you type. In these fragments, the intention is to arrange the instructions that will cause the failure or trap.

End the demo, go to the temporary code snippet, and return to DOS.

The list of procedures is as follows:

Name: ASM7. Asm
Features: Simulating exceptions and demonstrating exception handling
Compiling: Tasm ASM7. Asm
Connection: Tlink ASM7. Obj
----------------------------------------------------------------------------
INCLUDE 386SCD. INC
----------------------------------------------------------------------------
GDTSEG SEGMENT PARA USE16 Global Descriptor Data Segment (16-bit)
----------------------------------------------------------------------------
Global Descriptor Tabulation GDT
GDT LABEL BYTE
Empty descriptor
DUMMY Desc <>
Specification segment descriptor and selection sub
Normal Desc &LT;0FFFFH,,, atdw,,>
Normal_sel = Normal-gdt
Video Buffer Segment Descriptor (DPL=3) and selection sub
Videobuf Desc <0ffffh,8000h,0bh,ATDW,,>
Videobuf_sel = Videobuf-gdt
----------------------------------------------------------------------------
EFFGDT LABEL BYTE
Temporary code snippet descriptor and selection child
Tempcode Desc <0ffffh,TempCodeSeg,,ATCE,,>
Tempcode_sel = Tempcode-gdt
Demo Code Snippet descriptor and select child
DemoCode Desc
Democode_sel = Democode-gdt
Demo Task local description chart Segment descriptor and select child
Demoldt Desc
Demoldt_sel = Demoldt-gdt
Demo Task TSS segment descriptor and selection child
DEMOTSS Desc
Demotss_sel = Demotss-gdt
Buffer Data Segment descriptor and selection sub
Xbuffer Desc
Xbuffer_sel = Xbuffer-gdt
Reading keyboard tasks local description of descriptor and selector
Gkeyldt Desc
Gkeyldt_sel = Gkeyldt-gdt
Read the keyboard task TSS segment descriptor and selection sub
GKEYTSS Desc
Gkeytss_sel = Gkeytss-gdt
Display the trap handler code snippet descriptor and selection child
Echocode Desc
Echocode_sel = Echocode-gdt
Display error code Process code Snippet descriptor and Selector
Subcode Desc
Subcode_sel = Subcode-gdt
Other interrupt or exception handler code snippet descriptor and Selector
Other Desc
Other_sel = Other-gdt
----------------------------------------------------------------------------
Gdtlen = $-GDT Global Descriptor Chart length
Gdnum = ($-EFFGDT)/(SIZE Desc) Number of descriptors to be processed at the base address
----------------------------------------------------------------------------
GDTSEG ENDS Global Descriptor segment definition end
----------------------------------------------------------------------------
Idtseg SEGMENT PARA USE16 Interrupt Descriptor Data Segment (16-bit)
----------------------------------------------------------------------------
IDT LABEL BYTE Interrupt Descriptor
No. 0 # Trap Door descriptor (corresponding to division error failure)
Gate
3 trap door descriptors from 1--3
REPT 3
Gate
Endm
4th # Trap Door descriptor (corresponding to overflow trap)
Gate
6 trap door descriptors from 5--0ah
REPT 6
Gate
Endm
0BH Trap Door Descriptor (corresponding segment does not fail)
Gate
0CH Trap Door descriptor (corresponding to stack segment failure)
Gate
0DH Trap Gate Descriptor (corresponding to general protection failure)
Gate
240 Trap door descriptors from 0eh--0edh
REPT 240
Gate
Endm
corresponding to the 0feh Trap door descriptor (corresponding to the display interrupt handler)
Gate
0FFH Task Door descriptor (corresponding to read keyboard interrupt processing Task)
Gate <,GKeyTSS_Sel,,ATTaskGate,>
----------------------------------------------------------------------------
Idtlen = $-idt
----------------------------------------------------------------------------
Idtseg ENDS Interrupt Description Chart Segment definition End
----------------------------------------------------------------------------
Read Keyboard task local description chart segment
----------------------------------------------------------------------------
Gkeyldtseg SEGMENT PARA USE16
----------------------------------------------------------------------------
Gldt LABEL BYTE
Code snippet descriptor and selection child
Gkeycode Desc <0ffffh,GKeyCodeSeg,,ATCE,,>
Gkeycode_sel = Gkeycode-gldt+til
Stack segment descriptor and selection child
Gkeystack Desc
Gkeystack_sel = Gkeystack-gldt+til
----------------------------------------------------------------------------
Gkeyldnum = ($-gldt)/(SIZE Desc) The number of descriptors that need to initialize the base address
Gkeyldtlen = $ local description of the length of the chart segment
----------------------------------------------------------------------------
Gkeyldtseg ENDS
----------------------------------------------------------------------------
Read Keyboard task TSS segment
----------------------------------------------------------------------------
Gkeytssseg SEGMENT PARA USE16
DD 0 Link Word
Dd? Level 0 Stack pointer
Dw??
Dd? Level 1 Stack pointer
Dw??
Dd? Level 2 stack pointer
Dw??
DD 0 CR3
DW gkeybegin,0 EIP
DD 0 EFlags
DD 0 EAX
DD 0 ECX
DD 0 EDX
DD 0 EBX
DW gkeystacklen,0 ESP
DD 0 EBP
DD 0 ESI
DD 0 EDI
DW normal_sel,0 ES
DW gkeycode_sel,0 CS
DW gkeystack_sel,0 SS
DW normal_sel,0 DS
DW normal_sel,0 FS
DW normal_sel,0 GS
DW gkeyldt_sel,0 LDTR
DW 0 Debug Trap flag
DW $+2 offset to I/O license bitmap
DB 0FFH I/O license bitmap End byte
Gkeytsslen = $
Gkeytssseg ENDS
----------------------------------------------------------------------------
Reading the keyboard task stack segment
----------------------------------------------------------------------------
Gkeystackseg SEGMENT PARA USE16
Gkeystacklen = 1024
DB Gkeystacklen DUP (0)
Gkeystackseg ENDS
----------------------------------------------------------------------------
Reading keyboard Task Code Snippets
----------------------------------------------------------------------------
Gkeycodeseg SEGMENT PARA USE16
Assume Cs:gkeycodeseg,ds:rdataseg,es:bufferseg
----------------------------------------------------------------------------
Gkeybegin PROC FAR
Push DS
Push ES
Push FS
Push GS
MOV Ax,normal_sel
MOV Ss,ax ready to turn the real way
MOV eax,cr0
and al,11111110b
MOV Cr0,eax conversion method
JUMP16,
Getkey:mov ax,rdataseg Real Way
MOV Ds,ax
MOV ebp,esp recovery real Way part of the scene
LSS Sp,dword PTR Spvar
Lidt Qword PTR NORVIDTR
STi
MOV Dx,offset Mess
MOV ah,9
int 21H display hint information
Getkey1:mov ah,0
int 16h reading keyboard
CMP al, ' 0 '
JZ GetKey2
CMP al, ' 4 '
JZ GetKey2
and al,11011111b lowercase to uppercase
CMP al, ' B '
JB GetKey1
CMP al, ' D '
JA GetKey1 only [0,4,b,c,d] Valid
Getkey2:mov Dl,al
MOV ah,2
int 21h Displays the characters that are pressed
MOV ax,bufferseg
MOV Es,ax
mov BYTE PTR es:keyascii,dl saved to buffered data segment
CLI ready to return protection mode
Lidt Qword PTR VIDTR
MOV eax,cr0
or al,1
MOV cr0,eax
JUMP16,
Getkeyv:mov Ax,gkeystack_sel again into the protective mode
MOV Ss,ax
MOV ESP,EBP
Pop GS
Pop FS
Pop es
Pop ds
Iretd
JMP Gkeybegin
Gkeybegin ENDP
----------------------------------------------------------------------------
Gkeycodelen = $
Gkeycodeseg ENDS
----------------------------------------------------------------------------
Code snippets for other interrupts or exception handlers
----------------------------------------------------------------------------
Othercodeseg SEGMENT PARA USE16
Assume Cs:othercodeseg
----------------------------------------------------------------------------
Otherbegin PROC FAR
MOV Si,offset messother
int 0FEH display hint information
mov WORD PTR es:[0],ax
JMP $ Enter Infinite loop
Otherbegin ENDP
----------------------------------------------------------------------------
Othercodelen = $
Othercodeseg ENDS
----------------------------------------------------------------------------
Division error fault Handler code snippet
----------------------------------------------------------------------------
Divcodeseg SEGMENT PARA USE16
Assume Cs:divcodeseg
----------------------------------------------------------------------------
Divbegin PROC FAR
MOV Si,offset MESS0
MOV di,0
int 0FEH display hint information
SHR ax,1 Processing Simulated Division errors
IRETD return
Divbegin ENDP
----------------------------------------------------------------------------
Divcodelen = $
Divcodeseg ENDS
----------------------------------------------------------------------------
Overflow Trap Handler Code snippet
----------------------------------------------------------------------------
Ofcodeseg SEGMENT PARA USE16
Assume Cs:ofcodeseg
----------------------------------------------------------------------------
Ofbegin PROC FAR
MOV Si,offset MESS4
MOV di,0
int 0FEH display hint information
IRETD return
Ofbegin ENDP
----------------------------------------------------------------------------
Ofcodelen = $
Ofcodeseg ENDS
----------------------------------------------------------------------------
Segment does not have a fault handler code snippet
----------------------------------------------------------------------------
Snpcodeseg SEGMENT PARA USE16
Assume Cs:snpcodeseg
----------------------------------------------------------------------------
Snpbegin PROC FAR
MOV Si,offset MESSB
MOV di,0
int 0FEH display hint information
Pop eax eject error code
CALL16 Subcode_sel,subbegin Display error code
Pop eax
Add eax,2 No instructions for the segment caused by impersonation
Push EAX Adjust return address
Iretd
Snpbegin ENDP
----------------------------------------------------------------------------
Snpcodelen = $
Snpcodeseg ENDS
----------------------------------------------------------------------------
Stack segment fault handler code snippet
----------------------------------------------------------------------------
Ssecodeseg SEGMENT PARA USE16
Assume Cs:ssecodeseg
----------------------------------------------------------------------------
Ssebegin PROC FAR
MOV Si,offset MESSC
MOV di,0
int 0FEH display hint information
Pop eax eject error code
CALL16 Subcode_sel,subbegin Display error code
Pop eax
Add eax,4 by impersonation causing stack segment error
Push EAX instruction Adjust return address
Iretd
Ssebegin ENDP
----------------------------------------------------------------------------
Ssecodelen = $
Ssecodeseg ENDS

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.