; Listing3-4.asm
;
; A program that demonstrates caling the math_asec
; function.

            option  casemap:none
            
; Include the aoalib (ANSI strings) and aoamath
; libraries:

            include    aoalib.inc   
            include    math.inc  
            includelib aoalib.lib   
            includelib aoamath.lib  

            
            .const

; Program title:

            align   word
ttlStr      byte    "Listing 3-4", 0


            .data
            align   16
aVal        real8   -4.0
one         real8   1.0
zero        real8   0.0
aResult     real8   0.0
point5      real8   0.5


            .code

; External definitions for C/C++ atan function

            externdef atan:proc
        
; Here is the main assembly language function.

            
            public  asmMain
asmMain     proc
            push    rbx
            sub     rsp, 64 ;Shadow storage + alignment
            
; Repeat for aVal = -4.0 to +4.0 by 0.5:

            mov     rbx, 21
pgmLoop:

; Compute the assembly language result:

            math(asec) aVal
            fstp    aResult
            mov     rdx, aVal
            mov     r8, aResult
            call    print
            byte    "ASM.asec(%4.1f)=%6.3f  ", 0
            
; There is no ACSC function in C/C++, must compute
; it as acsc(x) = Atan(Sqrt(x * x - 1)
             
            movsd   xmm0, one                 
            movsd   xmm2, aVal      ;x
            mulsd   xmm2, xmm2      ;Compute x*x
            subsd   xmm2, xmm0      ;Compute x*x-1
            
; See if x*x-1 is less than zero before we try to
; compute its square root:

            comisd  xmm2, zero      ;Error if <= 0
            jb     badSqrt
            
; Compute sqrt(x*x-1):

            sqrtsd  xmm0, xmm2
                        
; Compute Atan(Sign(x) / Sqrt(x * x - 1)):
 
            call    atan
            
; Print the C result:

            movsd   aResult, xmm0
            mov     rdx, aVal
            mov     r8, aResult
            call    print
            byte    "C.asec(%4.1f)=%6.3f", nl, 0
            jmp     nextIteration

; Drop down here if we tried to take the square root
; of a negative number or divide by zero:
            
badSqrt:    mov     rdx, aVal
            call    print
            byte    "C.acsc(%4.1f)= -nan", nl, 0
            
nextIteration:
            fld     point5
            fld     aVal
            faddp
            fstp    aVal
            dec     rbx
            jnz     pgmLoop
            
            
 
allDone:    add     rsp, 64
            pop     rbx
            ret     ;Returns to caller
        
asmMain     endp
            end