;---------------------------------------
;
; Command line arguments via FASM & GCC (win64)
; Calculate SIN rad from prompt input
; (c)2018 S.A.R
;
; Compile: fasm sine64.asm
; Link   : gcc -m64 sine64.obj -s -o sine64.exe
; Usage  : sinr64 v1 v2 vn (single-spaced)
;
;---------------------------------------
format MS64 COFF
public main

extrn printf
extrn atof
extrn sin

section '.data' writeable
msg1 db 0ah,' Usage: sine64 v1 v2 vn (single-spaced)',0ah,0ah,0
fmt1 db 'sine(%#+.4f) = %#+.10f',0ah,0


section '.text' executable
main:
        sub     rsp,40          ;align n shadow space
        mov     rcx,msg1
        sub     rbx,1
        jnz     okay
        mov     rbx,1
        jmp     errr
  okay: mov     r15,rdx         ;RDX = Array of arg string pointers
        xor     esi,esi
        add     r15,8
  more: mov     rdi,[r15]
        call    str_lengthd
        mov     byte[rdi+rsi],0
        mov     rcx,rdi
        call    atof
        movq    xmm6,xmm0
        call    sin
        movq    r8,xmm0
        movq    rdx,xmm6
        mov     rcx,fmt1
        add     r15,8
  errr: call    printf
        sub     rbx,1
        jnz     more
        add     rsp,40
        ret

;In: RDI
;Find length of delimiter-ended string (20h)
;return length in RSI
str_lengthd:
        push    rdi rcx
        mov     al,0
        cmp     ebx,1
        je      last
        mov     al,20h
  last: mov     ecx,-1
        repne   scasb
        mov     esi,-2
        sub     esi,ecx
        pop     rcx rdi
        ret
