News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Need some more goasm statements converted

Started by Magnum, October 02, 2009, 08:58:55 PM

Previous topic - Next topic

Magnum

Interesting idea.

I did not see the "This program cannot be run in Dos..", is it encrpyted or not there?

Andy
Have a great day,
                         Andy

BlackVortex

It says "Win32 program!"   :toothy

EDIT: I should run one of them in DosBox btw, should be fun !

Magnum

I got it running right.

I left out two cases of push 0. :-)

Andy


; except.asm Shows a Per-Thread Exception Handler
;                 Written by Jeremy Gordon 2002 
     
.386
.MODEL FLAT, STDCALL
OPTION CASEMAP: NONE

    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\advapi32.inc
    include \masm32\include\shlwapi.inc
    include \masm32\macros\macros.asm

    includelib  \masm32\lib\kernel32.lib
    includelib  \masm32\lib\user32.lib
    includelib  \masm32\lib\advapi32.lib
    includelib  \masm32\lib\shlwapi.lib

.DATA

HappyStr db "This is a very happy ending.",0
Except   db "There was an exception - do you want me to swallow it?",0
Except1  db "Except1",0
Except2  db "Well it's all over for now.",0
Unwind   db "The system calling the handler again for more clearing up (unwinding)",0
FATALMESS DB "I thoroughly enjoyed it and I have already tidied everything up.",0
                   
.CODE

START:

ASSUME fs:NOTHING

PUSH offset FINAL_HANDLER
CALL SetUnhandledExceptionFilter
CALL PROTECTED_AREA
CALL CLEAR_UP           ;here the program clears up normally
PUSH 40h                ;exclamation sign + ok button only
push offset Except1     ;Except1
PUSH offset HappyStr    ;This is a very happy ending
push 0
CALL MessageBoxA        ;wait till ok pressed
PUSH 0                  ;code meaning a succesful conclusion
CALL ExitProcess        ;and finish with aplomb!

; PROGRAM END

PROTECTED_AREA:

push ebp                ; create the
push 0                  ; ERR structure
push 0                  ; on the stack
PUSH OFFSET SAFE_PLACE 
PUSH OFFSET HANDLER     
                       
push dword ptr fs:[0]   

mov dword ptr fs:[0],esp ;point to structure just established on the stack

; Now lets cause the exception ..

XOR ECX,ECX             ;set ecx to zero
DIV ECX                 ;divide by zero, causing exception
;cli

; Because of the exception the code never gets to here

SAFE_PLACE:             ;but the handler will jump to here ..
                       
pop dword ptr fs:[0]    ;restore original exception handler from stack

ADD ESP,14h             ;throw away remainder of ERR structure made earlier
RET

;This simple handler is called by the system when the divide by zero
;occurs.In this handler the user is given a choice of swallowing the
;exception by jumping to the safe-place, or not dealing with it at all,
;in which case the system will send the exception to the FINAL_HANDLER

HANDLER:

;save registers as required by Windows

PUSH EBX
PUSH EDI
PUSH ESI
MOV  EBX,[EBP+8]         ;get exception record in ebx
MOV  EAX,[EBX+4]         ;get flag sent by the system
TEST AL,1h              ;see if its a non-continuable exception
JNE  short nodeal
                        ;yes, so not allowed by system to touch it
TEST AL,2h              ;see if its the system unwinding
JNE  short unwind       ;yes
PUSH 24h                ;question mark + YES/NO buttons
PUSH offset Except1     ;Except1
push offset Except      ;There was an exception - do you want me to swallow it?
push 0                  ; ADDED today
CALL MessageBoxA        ;wait till button pressed
CMP  EAX,6               ;see if yes clicked..if yes, exit
JNE  short nodeal        ;

; go to SAFE_PLACE

MOV  ESI,[EBP+10h]       ;get register context record in esi
MOV  EDI,[EBP+0Ch]       ;get pointer to ERR structure in edi
MOV  [ESI+0C4h],EDI      ;insert new esp (happens to be pointer to ERR)
MOV  EAX,[EDI+8]         ;get address of SAFE_PLACE given in ERR structure
MOV  [ESI+0B8h],EAX      ;insert that as new eip in register context
MOV  EAX,[EDI+14h]       ;get ebp at safe place given in ERR structure
MOV  [ESI+0B4h],EAX      ;insert that as new ebp in register context
XOR  EAX,EAX             ;eax=0 reload context and return to system

jmp  short fin

unwind:

PUSH 40h                ;exclamation sign + ok button only
PUSH offset Except1     ;Except1
PUSH offset Unwind      ;The system calling the handler again for more clearing up (unwinding)
PUSH 0
CALL MessageBoxA        ;wait till ok pressed, then return eax=1

nodeal:

MOV  EAX,1               ;eax=1 system to go to next handler

fin:

POP  ESI
POP  EDI
POP  EBX
RET

CLEAR_UP:               ;all clearing up would be done here

RET

; Lets make our final handler which would do all clearing up if the program has to close

FINAL_HANDLER:          ;system passes EXCEPTION_POINTERS

PUSH EBX
PUSH EDI
PUSH ESI                ;save registers as required by Windows
CALL CLEAR_UP
PUSH 40h                ;exclamation sign + ok button only
PUSH offset Except2     ;Well it's all over for now.
PUSH offset FATALMESS
PUSH 0
CALL MessageBoxA        ;wait till ok pressed
MOV EAX,1               ;terminate process without showing system message box
POP ESI
pop EDI
pop EBX
RET

end START

Have a great day,
                         Andy