; Listing3-12.asm
;
; A program that demonstrates caling the math_tan
; and math_cot functions.

            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-12", 0


            .data
            align   16
aVal        real8   ?
mtwo        real8   -2.0
aResult     real8   0.0
pBy16       real8   16.0

            .code

; External definitions for C/C++ sin function

            externdef tan:proc
        
; Here is the main assembly language function.
            
            public  asmMain
asmMain     proc
            push    rbx
            sub     rsp, 64 ;Shadow storage + alignment
            
            fldpi           ;Set aVal to pi/2
            fld     mtwo
            fdivp
            fstp    aVal
            
            fldpi
            fld     pBy16
            fdivp
            fstp    pBy16
            call    print
            byte    "(x)=(ASM.tan(x),ASM.cot(x)); (C.tan(x),C.cot(x))", nl, 0
            
; Repeat for aVal = -pi/2 to +pi/2 by pi/32:

            mov     rbx, 17
pgmLoop:

; Compute the assembly language cos result:

            math(tan) aVal
            fstp    aResult
            mov     rdx, aVal
            mov     r8, aResult
            call    print
            byte    "(%4.1f)=(%10.3e,", 0
            
; Compute the assembly language csc result:

            math(cot) aVal
            fstp    aResult 
            mov     rdx, aResult
            call    print
            byte    "%10.3e);", 0
            
; Use C++ tan:
             
            movsd   xmm0, aVal
            call    tan
            movsd   aResult, xmm0
            
; Print the C tan result:

            mov     rdx, aResult
            call    print
            byte    " (%10.3e,", 0
            
; Compute and display C cot result;

            fld     aResult
            fld1
            fdivrp
            fstp    aResult
            mov     rdx, aResult
            call    print
            byte    "%10.3e)", nl, 0
            
            
nextIteration:
            fld     pBy16
            fld     aVal
            faddp
            fstp    aVal
            dec     rbx
            jnz     pgmLoop
            
allDone:    add     rsp, 64
            pop     rbx
            ret     ;Returns to caller
        
asmMain     endp
            end