Structured Exception Handling in Assembly Language

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down

Structured Exception Handling in Assembly Language

Bài gửi  admin on 12/7/2010, 3:21 pm

Windows 95 và Windows NT hỗ trợ một cách mạnh mẽ để xử lý các exceptions, được gọi là Structured Exception Handling, nó cũng đã hỗ trợ trực tiếp bằng ngôn ngữ lập trình. Một “exception” là một event (biến cố) mà chúng bất ngờ xảy ra hoặc gây gián đoạn process khi đang thực thi bình thường. Các Exceptions có thể được dò tìm ra bởi cả phần cứng và phần mềm.

Bạn có thể viết code một cách an toàn với Structured Exception Handling. Bạn phải đảm bảo rằng các nguồn tài nguyên, chẳng hạn như các khối bộ nhớ và các tập tin, được close một cách hợp lệ khi event chấm dứt bất ngờ. Một tính năng đặc biệt của Structured Exception Handling là sau khi một exception handler được cài đặt, nó có thể xử lý các exception mà các hàm khác gọi đến (gây ra exception). Vì thế, Hàm A có thể xử lý một exception được gây ra trong hàm được gọi bởi hàm A.

Macro sau đây include Structure Exception Handling trong chương trình Asm của bạn.
Code:
SEH Macros
@TRY_BEGIN MACRO Handler
    pushad                          ;;Save Current State
    mov esi, offset Handler        ;;Address of New Exception Handler
    push esi                        ;;Save Old Exception Handler
    push dword ptr fs:[0]          ;;Install New Handler
    mov dword ptr fs:[0], esp 
ENDM
@TRY_EXCEPT MACRO Handler
    jmp NoException&Handler        ;;No Exception Occured, so jump over
Handler:
    mov esp, [esp + 8]              ;;Exception Occured, Get old ESP
    pop dword ptr fs:[0]            ;;Restore Old Exception Handler
    add esp, 4                      ;;ESP value before SEH was set
    popad                          ;;Restore Old State
ENDM
@TRY_END MACRO Handler
    jmp ExceptionHandled&Handler    ;;Exception was handled by @TRY_EXCEPT
NoException&Handler:                ;;No Exception Occured
    pop dword ptr fs:[0]            ;;Restore Old Exception Handler
    add esp, 32 + 4                ;;ESP value before SEH was set. 32 for pushad and ...
ExceptionHandled&Handler:          ;;...4 for push offset Handler. (No Restore State)
                                    ;;Exception has been handled, or no exception occured
ENDM
Using SEH Macros
@TRY_BEGIN HandlerName 
    ;Code in this place will be checked for exceptions.
@TRY_EXCEPT HandlerName 
    ;Code in this place will be executed if an exception occurs.
@TRY_END HandlerName 
    ;Normal execution path
Sample Program
;Structured Exception Handling in Assembly
;(c) 2000, Rohitab Batra
;rohitab@rohitab.com
;
;To Compile this program, you need the 32-bit version of Turbo Assembler
;
;TASM32 /ml SEH
;TLINK32 SEH,SEH,,IMPORT32.LIB
 
.386p
.model flat ,stdcall
 
EXTRN ExitProcess:PROC
EXTRN MessageBoxA:PROC
 
;Define the @TRY_BEGIN, @TRY_EXCEPT and @TRY_END Macros Here
 
.data 
    szCaption      db 'SEH in Assembly', 0
    szException    db 'Exception has been handled !!', 0dh, 0ah
                    db 'Press OK to terminate gracefully', 0
    szNoException  db 'No Exception occured', 0
 
.code
 
WinMain:
 
@TRY_BEGIN Zero_Address_Access 
    mov    ebx, 0          ;Prepare to write to address 0
    mov    [ebx], ebx      ;Write to address 0 (Access Violation)
                            ;Comment the above line if to remove the exception
@TRY_EXCEPT Zero_Address_Access
    ;This code will get executed if an exception occurs.
    call MessageBoxA, 0, offset szException, offset szCaption, 0
    jmp ExitProgram
@TRY_END Zero_Address_Access
    ;Normal Execution Path.
ExitProgram: 
    call MessageBoxA, 0, offset szNoException, offset szCaption, 0
    call ExitProcess, 0
END WinMain

admin
Thiếu Úy III
Thiếu Úy III

Tổng số bài gửi: 627
Diem: 2084
Thank: 4
Join date: 24/03/2010
Đến từ: Bỉm Sơn - Thanh hóa

http://www.itprofes.com

Về Đầu Trang Go down

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang

- Similar topics

Permissions in this forum:
Bạn không có quyền trả lời bài viết