; Listing1-7.asm
;
; A program that demonstrates _if and _endif
; context-free macros using the _push and _pop
; macros (either implementation, version 1 or 2
; of _push and _pop will work fine here).

            option     casemap:none
            include    aoalib.inc   ;AoA library + constants
            includelib aoalib.lib   ;Link in aoalib library
            include    ctPushPop.inc

            
            .const

; Program title:

            align   word
ttlStr      byte    "Listing 1.6", 0


; _if and _endif macros-
;
; Simulate a HLL if/endif statement.
; Expression must be a register. Body
; of _if statement executes if the
; register contains a non-zero value.
;
; Context-free version that saves _if target
; address on the stack for use by _endif 

ifStk       =       0       ;Use version 2 of _push/_pop

_if         macro   register
            local   destination
            
            _push(ifStk, destination)
            test    register, register
            jz      destination
            endm
            
_endif      macro
            local   theDest
theDest     _pop(ifStk)
theDest:
            endm
            

            .data
            
            .code

        
; Here is the main assembly language function.

            
            public  asmMain
asmMain     proc

            push    rbp
            mov     rbp, rsp
            sub     rsp, 56
            push    rbx

; Outermost _if statement invoked here:

            _if     eax
            call    print
            byte    "EAX != 0", nl, 0

; Nested _if statement:

            _if     ebx
            call    print
            byte    "EBX is also non-zero", nl, 0
            _endif  ;ebx _if statement
            
; Single nop so that the target locations for the nested
; and nesting _if statements are different:

            nop
            _endif  ;eax _if statement                        

allDone:    pop     rbx
            leave
            ret     ;Returns to caller
        
asmMain     endp
            end