; Listing7-1.asm
;
; A program that demonstrates exception-handling 
; failure in a program that doesn't set up the
; stack properly.
;
; Information on exception handling can be found at:
;
; https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019
; https://docs.microsoft.com/en-us/cpp/build/prolog-and-epilog?view=vs-2019
; https://docs.microsoft.com/en-us/cpp/assembler/masm/proc?view=vs-2019
; https://blogs.msdn.microsoft.com/freik/2006/03/06/x64-abi-vs-x87-abi-aka-calling-conventions-for-amd64-em64t/
; https://blogs.msdn.microsoft.com/freik/2006/01/04/x64-unwind-information/
; https://www.tortall.net/projects/yasm/manual/html/objfmt-win64-exception.html
; https://docs.microsoft.com/en-us/cpp/assembler/masm/masm-for-x64-ml64-exe?view=vs-2019
; https://www.codeproject.com/Articles/1212332/bit-Structured-Exception-Handling-SEH-in-ASM
; https://blog.talosintelligence.com/2014/06/exceptional-behavior-windows-81-x64-seh.html
; http://www.osronline.com/article.cfm%5earticle=469.htm
; http://www.uninformed.org/?v=4&a=1&t=sumry
; http://www.nynaeve.net/?p=99
; http://www.nynaeve.net/?p=101
; http://www.nynaeve.net/?p=105
; http://www.nynaeve.net/?p=106
; http://www.nynaeve.net/?p=107
; http://www.nynaeve.net/?p=110
; http://www.nynaeve.net/?p=113


            option     casemap:none
            .nolist
            include    aoalib.inc
            includelib aoalib.lib
            .list
            
            .const

; Program title:

             align  word
ttlStr       byte   "Listing 7-1", 0



            
            
            .code


badSEHFunc  proc

; Mess with the stack and make this a non-SEH function:
 
            push    rbx
            sub     rsp, 0400h
            push    rax
            
            xor     rbx, rbx
            mov     rax, [rbx] ; cause exception (NULL ref).
            
            pop     rax
            add     rsp, 0400h
            pop     rbx
            ret
badSEHFunc  endp

        
;--------------------------------------------------
;
; Here is the main assembly language function.
            
            public  asmMain
                        
asmMain     proc
            push    rbp
            mov     rbp, rsp
            sub     rsp, 38h

            call    badSEHFunc 

            leave
            ret
        
asmMain     endp
            end