Structured Exception Handling in Assembly Language
itprofes :: Tài Liệu theo chủ đề :: MASM
Trang 1 trong tổng số 1 trang • Share •
Structured Exception Handling in Assembly Language
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.
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
itprofes :: Tài Liệu theo chủ đề :: MASM
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết





» xin tai lieu tieng viet
» Theo dõi tiến trình
» Giải pháp Bảo mật của Cisco
» Nghiên cứu và đưa ra giải pháp phòng chống tấn công DoS, DDoS (Phần 1)
» Learn to hack !
» Giải pháp hệ thống dành cho doanh nghiệp với thiết bị mạng Fortinet (Phần 1)
» Ô Long Viên (Tập II)
» những ebook về hack tiếng việt cho người mới tìm hiểu.
» Cơ hội mua Laptop SAMSUNG Intel® Core™ i3-330M Processor giá rẻ dành cho học viên VnExperts