|
Written by X-Calibre [Diamond]
|
;Summary: Macros for Structured Exception Handling
;Compatibility: MASM, Win32
;Notes: Demonstration code contained in SEH.ASM, below
IFNDEF RaiseException
RaiseException PROTO STDCALL dwExceptionCode:DWORD, dwExceptionFlags:DWORD ,
nNumberOfArguments:DWORD, lpArguments:PTR DWORD
ENDIF
includelib kernel32.lib
TRY MACRO
PUSHCONTEXT ASSUMES
assume fs:nothing
; Install exception handler
push @@handler
push dword ptr fs:[0]
mov fs:[0], esp
POPCONTEXT ASSUMES
ENDM
CATCH MACRO exception
LOCAL @@invokeHandler
jmp @@removeHandler
@@handler:
IFNB <exception>
mov eax, [esp+4]
cmp dword ptr [eax], exception
je @@invokeHandler
mov eax, 1
ret
@@invokeHandler:
ENDIF
ENDM
ENDC MACRO
PUSHCONTEXT ASSUMES
assume fs:nothing
; Restore state
mov esp, dword ptr fs:[0]
mov esp, [esp]
@@removeHandler:
pop fs:[0]
add esp, 4
POPCONTEXT ASSUMES
ENDM
FINALLY MACRO
@@handler:
ENDM
ENDF MACRO
LOCAL @@removeHandler
PUSHCONTEXT ASSUMES
assume fs:nothing
; Restore state
cmp esp, dword ptr fs:[0]
je @@removeHandler
mov esp, dword ptr fs:[0]
mov esp, [esp]
@@removeHandler:
pop fs:[0]
add esp, 4
POPCONTEXT ASSUMES
ENDM
THROW MACRO exception
INVOKE RaiseException, exception, 0, 0, NULL
ENDM
; ---- flags ---
EXCEPTION_INT_DIVIDE_BY_ZERO equ 0C0000094h
SEH.ASM
by X-Calibre
;Summary: Sample program for using SEH.INC
;Compatibility: MASM, Win32
.386
.Model Flat, StdCall
include windows.inc
include user32.inc
include SEH.inc
includelib user32.lib
.code
tst PROC
THROW 0E0000001h
ret
tst ENDP
start:
main PROC
TRY
sub edx, edx
mov ecx, 0
idiv ecx
CATCH(EXCEPTION_INT_DIVIDE_BY_ZERO)
.data
exceptionMsg BYTE "Exception occured",0
.code
INVOKE MessageBox, NULL, ADDR exceptionMsg, ADDR exceptionMsg, MB_OK
ENDC
main ENDP
blah PROC
TRY
call tst
FINALLY
.data
finallyMsg BYTE "In FINALLY-block",0
.code
INVOKE MessageBox, NULL, ADDR finallyMsg, ADDR finallyMsg, MB_OK
ENDF
blah ENDP
.data
finishMsg BYTE "Program finished",0
.code
INVOKE MessageBox, NULL, ADDR finishMsg, ADDR finishMsg, MB_OK
ret
end start
|